Plan A for a phone costs 20 dollars a month plus 5 dollars per gigabyte (GB) of data. Plan B costs 40 dollars a month plus 1 dollar per GB. Which is cheaper? It depends: for a light user Plan A wins, for a heavy user Plan B wins. The question "is Plan A cheaper?" is an inequality, , and its answer is true for some values of and false for others. Programs make decisions the same way. A condition is a comparison that is either True or False, and a conditional (an if statement) runs some lines only when its condition is True. Put a conditional inside a loop and the program can test an inequality for many values at once.
Comparisons are inequalities
| Math | Python | Example with x = 7 | Result |
|---|---|---|---|
| (less than) | < | x < 10 | True |
| (greater than) | > | x > 10 | False |
| (less than or equal to) | <= | x <= 7 | True |
| (greater than or equal to) | >= | x >= 8 | False |
| (equal to) | == | x == 7 | True |
| (not equal to) | != | x != 7 | False |
x = 7
print(x > 5)
print(x <= 7)
print(2 * x == 14)
print(x != 7)print(5 < 5) print?Making decisions with if, elif and else
n = -4
if n > 0:
print("positive")
elif n < 0:
print("negative")
else:
print("zero")Like a loop, each if, elif and else line ends with a colon, and the indented lines under it belong to it. Python checks the conditions from the top down. It runs the block under the first condition that is True and skips everything else in the chain. elif (short for "else if") is checked only when every condition above it was False, and else has no condition: it runs when none of the conditions above it were True. So in an if, elif, else chain exactly one block runs. Both elif and else are optional; an if on its own simply runs its block or skips it. For , the test is False, the test is True, so the program prints negative and never reaches else.
temp = 12
if temp > 25:
print("hot")
elif temp > 10:
print("mild")
else:
print("cold")n = 0
if n >= 0:
print("A")
elif n == 0:
print("B")
else:
print("C")Testing an inequality with a loop
for x in range(0, 11):
if 3 * x + 2 > 14:
print(x)The loop substitutes into and prints only the values that make true. At the left side is exactly 14, and is False, so 4 is not printed; changing > to >= would add it. Algebra gives the complete answer: subtract 2 from both sides to get , then divide by 3 to get . The two agree, but they are not the same thing. The loop tests only the whole numbers in its range, so it says nothing about 4.5 or 50, both of which also satisfy . Testing with code is a quick way to check a solution or spot a pattern; algebra describes every solution. To test a value between two bounds, such as , Python lets you write 2 <= x <= 6, or join two conditions with and: x >= 2 and x <= 6.
range(0, 11) tests and prints 5, 6, 7, 8, 9 and 10. Which of these values also makes true, but is never printed?for x in range(0, 11):
if x + 5 >= 9:
print(x)Reading and altering code: two phone plans
fee_a = 20
rate_a = 5
fee_b = 40
rate_b = 1
for gb in range(0, 9):
cost_a = fee_a + rate_a * gb
cost_b = fee_b + rate_b * gb
if cost_a < cost_b:
print(gb, cost_a, cost_b, "Plan A")
elif cost_a > cost_b:
print(gb, cost_a, cost_b, "Plan B")
else:
print(gb, cost_a, cost_b, "same")Reading code means predicting what it does before you run it. For 3 GB, and ; the first condition is True, so that line is 3 35 43 Plan A. Altering code means changing it to model a new situation, and the parameters at the top make that easy. If Plan B's monthly fee rises to 45 dollars, change one line to fee_b = 45. Now Plan A is cheaper while , that is , or : the lines for 0 to 6 GB say Plan A, 7 and 8 GB say Plan B, and no line says same, because 6.25 is not a whole number. To compare up to 20 GB, change the range to range(0, 21). Each change is a small edit to one line, and the loop and conditions do the rest.
fee_a = 20, rate_a = 5; Plan B: fee_b = 40, rate_b = 1), what line is printed when gb is 2?rate_b = 1 is changed to rate_b = 3. At how many GB do the two plans now cost the same?