Making Decisions with if
Programs get useful when they can decide. if / elif / else let your code do different things depending on the situation โ the branching logic behind every app.
A program that always does exactly the same thing isn't very useful. The power comes from making decisions: do this if that's true, otherwise do something else. In Python, that's if, elif and else, driven by conditions that are True or False. This branching is at the heart of every interactive program.
if / elif / else
An if runs a block only when its condition is True. elif (else-if) and else handle the other cases.
age = 18
if age >= 18:
print('You can vote.')
elif age >= 16:
print('Almost there.')
else:
print('Not yet.')if - the indented lines run when the condition is true. This isn't decoration; it's how Python knows the structure. Be consistent (4 spaces is standard). It reads cleanly once you're used to it, and it's a common source of beginner errors, so mind your indentation.Conditions and boolean logic
Conditions are expressions that evaluate to True or False, using comparisons and logic.
| Operator | Means |
|---|---|
| == != | equal to / not equal to |
| < > <= >= | less/greater than (or equal) |
| and or not | combine/negate conditions |
| in | is it contained? (e.g. 'a' in word) |
Note == (compare) vs = (store) - a classic beginner mix-up. You can combine conditions: if age >= 18 and has_id: ...
Decide well
- if runs a block when a condition is True; elif/else cover the rest.
- Indent the lines that belong to each branch (4 spaces).
- == compares, = stores - don't mix them up.
- Combine conditions with and/or/not for richer logic.
๐ฏ Key takeaways
- if runs a block only when its condition is True; elif and else handle the other cases.
- Indentation (4 spaces) defines which lines belong to a branch - it's structural, not decorative.
- Conditions use comparisons (==, !=, <, >) and logic (and, or, not, in) and evaluate to True/False.
- == compares while = stores - a classic mix-up to watch for.
Frequently asked
What's the difference between = and ==?
One stores, the other compares - and confusing them is one of the most common beginner bugs. A single = assigns: x = 5 puts 5 into x. A double == compares: x == 5 asks 'is x equal to 5?' and gives back True or False. You use = to set variables and == inside conditions (like if x == 5:). If you accidentally write if x = 5: Python will give an error, which is actually helpful because it flags the mistake. Remember: = does something (stores), == asks something (compares).
Why does Python care so much about indentation?
Because Python uses indentation instead of braces or keywords to define which lines belong together - the indented block under an if is what runs when the condition is true. In many languages that grouping is shown with { } and indentation is just for readability; in Python the indentation IS the grouping, so it's structurally required and must be consistent. This makes Python code clean and readable (you can't have messy, misleading indentation), but it means inconsistent or wrong indentation causes errors. Stick to a consistent 4 spaces per level and it becomes second nature quickly.
Related lessons
Variables and Values
Everything in a program is data, and variables are how you store and name it. Master this โฆ
Python Fundamentals (Hands-On)Repeating with Loops
Computers are brilliant at doing the same thing many times. Loops let you repeat actions wโฆ
Learn to Code (from Zero)How Programs Think
Almost all programs are built from a few simple building blocks - values, decisions and reโฆ