Making decisions with if
So far every program has run straight down the page. if lets it choose.
Your first decision
age = 15
if age >= 13:
print("You can have an account")
You can have an account
Three things to notice, and the third is the one that bites people:
ifis followed by something that is either true or false- The line ends with a colon
- The next line is indented — that indent is what tells Python which code
belongs to the
if
Change age to 10 and run it again. Nothing prints, because the condition
was false and Python skipped the indented block entirely.
Otherwise: else
age = 10
if age >= 13:
print("You can have an account")
else:
print("Ask a parent to set one up")
Ask a parent to set one up
else catches everything the if didn't. Exactly one of the two blocks runs —
never both, never neither.
More than two paths: elif
score = 74
if score >= 90:
print("Excellent")
elif score >= 70:
print("Good")
elif score >= 50:
print("Pass")
else:
print("Needs work")
Good
Python checks each condition in order and stops at the first true one. That ordering is doing real work here: 74 is also greater than 50, but Python never gets that far. Write your conditions from most specific to least, or you'll get answers that look almost right.
Comparing things
| You write | It means |
|---|---|
a == b |
a equals b |
a != b |
a does not equal b |
a > b |
a is greater than b |
a >= b |
a is greater than or equal to b |
a < b |
a is less than b |
a <= b |
a is less than or equal to b |
print(5 == 5)
print(5 != 5)
print("cat" == "Cat")
True False False
That last line matters: comparing text is case-sensitive. "cat" and
"Cat" are different strings, so a login check that ignores this rejects
people who capitalised their username.
The classic mistake: = versus ==
One equals sign stores a value. Two equals signs ask a question.
score = 10 # store 10 in score
print(score == 10) # ask: is score 10?
True
Writing if score = 10: is a syntax error — Python stops and complains, which
is the kindest thing it could do. In some other languages that same typo
silently assigns and runs anyway, causing bugs people hunt for hours.
Combining conditions
age = 15
has_permission = True
if age >= 13 and has_permission:
print("Welcome")
if age < 13 or not has_permission:
print("Cannot continue")
Welcome
and needs both sides true. or needs at least one. not flips a value.
These read almost like English, which is deliberate.
Indentation is not decoration
In most languages, indenting is a courtesy to the reader. In Python it is the actual grammar:
temperature = 30
if temperature > 25:
print("It is warm")
print("Drink water")
print("This always prints")
It is warm Drink water This always prints
The first two prints are inside the if. The third is not — it sits at the
left margin, so it belongs to the program, not the decision. Set temperature
to 10 and run it: only the last line appears.
Use four spaces. Mixing tabs and spaces produces errors that are genuinely hard to see, because the two look identical on screen.
Prefer learning with a teacher?
WasiLearn Academy runs small-group classes covering this material, online and in Karachi.
See the classes