Making decisions: if and else
A program that always does the same thing is not much use. Programs make decisions with if, elif and else. Run one nine-line Python program three times with different inputs and follow a token along a track: each comparison answers True or False, and exactly one path runs.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01What does 5 > 3 give in Python?
A comparison always answers with a bool. 5 is greater than 3, so the answer is True.
02In the ticket program, which price does age = 70 with member = False get?
70 < 13 is False, so the if block is skipped. Then age >= 65 is True, so the elif test is True (only one side of or needs to be True) and price = 6 runs.
03x is 3. The program is if x > 10: then the indented line print("big") then the unindented line print("done") . What is printed?
The indented line belongs to the if block, so it is skipped: 3 > 10 is False and there is no else. The unindented line is outside the block, so it runs whatever the test said.
04What is the difference between = and == ?
age = 8 stores 8 under the name age. age == 8 asks whether age equals 8. Writing if age = 8: is a SyntaxError.
05Which of these is True?
or is True when at least one side is True. and needs both sides True, and not flips True to False.
Glossary
- bool
- Python's type for yes-or-no answers. It has only two values, True and False. A condition such as age < 13 works out to a bool.
- Comparison
- An operator that compares two values and gives a bool: == (equal), != (not equal), <, >, <= and >=.
- if
- Runs the indented block under it only when its condition is True.
- elif
- Short for "else if": a further test, checked only when every test above it was False.
- else
- The block that runs when every test above it was False. It has no condition.
- Block
- The group of lines that belong together under a line ending in a colon, marked by being indented the same amount.
- Indentation
- The spaces at the start of a line. Python uses it to decide which lines are inside a block; four spaces per level is the usual style.
- and, or, not
- Combine bools: and is True only if both sides are True, or if at least one is, and not flips True and False.
Notes and sources
- Python checks the tests from the top, runs the block of the first one that is True, and skips every other block of that if statement; tests below the True one are not even evaluated.
- An if accepts any value, not only True and False. 0, 0.0, an empty string "" and None count as false; most other values count as true.
- Strictly, and and or return one of their two sides rather than a new bool: 0 or 5 gives 5. With two bools, as here, the result is always True or False.
- Python only needs the lines of a block to be indented by the same amount; four spaces per level is the standard style (PEP 8). Do not mix tabs and spaces.
- A common slip is writing if age = 13: with one equals sign. In Python that is a SyntaxError: = stores, == compares.
Quiz Unlocks after the last step.