Reference · module 2
Making decisions
Everything this module explains, on one page.
True and False
Compare two values and read what comes back.
- 3 < 5 is not a question, it is an expression with a value: True. Python has exactly two of these, True and False, and both are written with a capital letter.
- The comparisons are ==, !=, <, >, <= and >=. Note the first one: a single equals stores a value, a double equals asks whether two are the same. Mixing them up is the classic first bug.
- True and False are a type of their own, called bool, and they can be stored like anything else: is_ready = 3 < 5. A name holding a bool reads well in a condition later.
The mistake you are about to make
if x = 5:
if x == 5:
In maths an equals sign asks a question. In Python a single = stores and a double == asks. Almost everybody writes it once, and Python catches it immediately.
if and else
Run one piece of code or another.
- if x > 10: runs the lines under it only when the condition is True. The colon at the end of the line is not optional, and the lines under it are indented — usually four spaces.
- The indentation IS the structure. Other languages use braces; Python uses the shape of the text, so a line moved left or right changes which block it belongs to and therefore when it runs.
- else covers everything the if did not, and elif is a second question asked only when the first was False. The first branch that fits wins, and the rest are skipped entirely.
The mistake you are about to make
print under the if with no indent
print under the if, indented four spaces
In other languages braces mark the block and the indent is cosmetic. In Python the indent is the block: move a line left and it starts running every time.
and, or, not
Put two conditions together.
- and needs both sides to be True. or needs one. not turns whichever it is into the other. Those three words are written out in Python rather than punched in as symbols.
- So x > 0 and x < 10 means between them, and x < 0 or x > 10 means outside them. Reading them aloud usually settles which one you wanted.
- Python also allows 0 < x < 10, chained the way it is written in mathematics. It means exactly the and version and is easier to read, so prefer it where it fits.
The mistake you are about to make
if x > 0 and < 10:
if x > 0 and x < 10:
Said out loud you name the thing once. Python reads each side of and as a condition in its own right, so both sides need the x. Or write 0 < x < 10.
Empty is false
Read a condition that is not a comparison.
- A condition does not have to be a comparison. if x: works, and Python decides for itself whether x counts as true. Most things do.
- The ones that do not are the empty ones: 0, the empty string "", the empty list [], and None. Everything else — any other number, any text with something in it — counts as true.
- This is used constantly, because if name: reads as if there is a name, which is usually the question you actually had. It is shorter than if name != "" and says the same thing.
The mistake you are about to make
if count:
if count is not None:
if count: reads as "if there is a count". Zero is falsy too, so a real zero disappears along with a missing value. Where zero means something, ask directly.
Reading an error
Know what the four commonest errors are telling you.
- An error is not a punishment, it is the most precise message you will get all day. Read the last line first: it names the kind of error and usually says exactly what was wrong.
- NameError means you used a name nothing has defined — a typo, or a line that has not run yet. TypeError means the types do not fit, like "5" + 5 from the first module.
- SyntaxError means Python could not read the line at all — a missing colon or bracket. IndentationError is its cousin: the line is readable but is in the wrong place.
The mistake you are about to make
reading the traceback from the top
reading the traceback from the bottom
A traceback prints downwards, so the eye starts at the top. The useful part is the last line: the kind of error and what went wrong. Above it is only how Python got there.