Programs that make decisions
Until now your programs have been a straight road: line 1, line 2, line 3, done. Real instructions have forks in them. "If it's raining, take an umbrella." The umbrella step only happens sometimes.
age = int(input("Your age: "))
if age >= 18:
print("Come on in")
else:
print("Sorry, adults only")
Type 20 and you get:
Your age: 20
Come on in
Type 15 and you get:
Your age: 15
Sorry, adults only
One of the two lines runs. Never both, never neither.
Indentation is grammar here
Look at the four spaces in front of each print. In most languages, indentation is cosmetic — the computer ignores it and it only exists to help humans read. In Python it's part of the language. The indented lines are the ones that belong to the if.
age = 15
if age >= 18:
print("A")
print("B")
Output:
B
print("A") is indented, so it belongs to the if and is skipped. print("B") is not indented, so it's outside the if and always runs. Move that one line four spaces to the right and the behavior of the program changes. That's how load-bearing whitespace is in Python.
Three rules cover it:
- The
ifline ends with a colon. Forgetting it gives youSyntaxError, and it's the most common typo there is. - The next line is indented. Use four spaces, consistently. Don't mix tabs and spaces — they look identical on screen and produce
IndentationError, which is a miserable bug to stare at. Set your editor to insert spaces when you press Tab. - When the indentation stops, the branch is over.
Comparison operators
print(3 > 5)
print(3 == 3)
print("a" != "b")
Output:
False
True
True
The full set:
a == b # equal (two equals signs)
a != b # not equal
a > b # greater than
a >= b # greater than or equal
a < b # less than
a <= b # less than or equal
Mixing up = and == is the classic beginner slip. Remember it this way: one equals sign gives an order, two equals signs ask a question. x = 5 puts 5 into x. x == 5 asks "is x currently 5?" and answers True or False.
A comparison always produces a bool, which is exactly what if needs. if runs its block when the condition is True.
More than two paths: elif
score = int(input("Score: "))
if score >= 90:
print("Excellent")
elif score >= 80:
print("Good")
elif score >= 60:
print("Pass")
else:
print("Fail")
Type 85 and you get:
Score: 85
Good
elif is short for "else if." Here's the part people get wrong: Python checks the conditions top to bottom and stops at the first one that's True. Everything below it is skipped without being looked at.
So a score of 95 prints only Excellent. It's also >= 80 and >= 60, but those lines never get evaluated, because the chain already exited.
That also means order matters enormously. Flip the chain around and put score >= 60 first, and a score of 95 prints Pass — it's the first condition that matches. When you build a chain like this, go from the most specific or most extreme case down to the most general.
Watch the boundaries too. >= 80 includes exactly 80. > 80 does not. Off-by-one at the boundary is where grading code goes wrong, so test the edges: 90, 80, 60.
Combining conditions: and / or / not
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("Let them through")
if age < 6 or age >= 65:
print("Free admission")
if not has_ticket:
print("Buy a ticket first")
Output:
Let them through
Only the first condition is True, so only that line prints.
and— strict. Both sides must be True.or— relaxed. One True side is enough.not— flips True and False.
A reliable way to keep them straight: and narrows the group of people who get through, or widens it.
Python also lets you chain comparisons the way math notation does:
age = 30
if 18 <= age < 65:
print("Working age")
Output:
Working age
18 <= age < 65 means the same as age >= 18 and age < 65, and reads closer to how you'd say it out loud.
Ten-second check
x = 5
if x > 3:
print("A")
elif x > 1:
print("B")
What prints?
Only A. Yes, 5 is also greater than 1 — but once the if matched, the whole chain is finished and the elif is never tested. If you wanted both messages, you'd write two separate if statements instead of an if/elif chain. That choice, one chain versus several independent ifs, is a real design decision you'll make often.