The if Statement
Concepts covered: Conditionals
Every program you have written so far runs each line exactly once, from the first to the last. An if statement adds a decision: it holds a condition, and the lines under it run only when that condition is True. When the condition is False, Python skips those lines and carries on below them.
balance = 120
if balance > 100:
print("Balance above 100")Python reads this in 3 steps. First, it compares the number stored in balance with 100. Second, that comparison produces a boolean, here True. Third, it uses that boolean to decide: True runs the indented print, and False skips it. Had balance been 90, the comparison would produce False, the print would never run, and the program would print nothing at all.
What a condition is made of
A condition is any expression whose value is a boolean. You met that type in Data Types, where its only values were True and False. The 6 comparison operators below each take 2 values and produce one boolean, which makes them the usual way to build a condition. With balance = 120, each row shows what the comparison returns.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `==` | equal to | `balance == 120` | True |
| `!=` | not equal to | `balance != 120` | False |
| `>` | greater than | `balance > 200` | False |
| `<` | less than | `balance < 200` | True |
| `>=` | greater than or equal to | `balance >= 120` | True |
| `<=` | less than or equal to | `balance <= 100` | False |
A single = stores a value, and a double == asks whether 2 values are equal. Writing if balance = 100: is a syntax error, and Python refuses to run the file. When a condition tests equality it must use ==, because a single = is an assignment and is not allowed in an if header.
The colon and the indent
The header line ends with a colon. Every line that belongs to the if is indented by 4 spaces underneath it, and together those lines are the statement's block. The block ends at the first line that returns to the left margin. That line runs no matter what the condition was, because it sits outside the block. Leave the indent out and Python stops before running anything, with the message IndentationError: expected an indented block.
Watching a True condition
The animation below stores 21 in age and tests age >= 18. The comparison produces True, so both indented prints run in order. The final print is back at the left margin, so it belongs to the program rather than to the if, and it runs after the block finishes. Step through it and watch the highlight move into the block and out again.
age = 21
if age >= 18:
print("You are an adult")
print("You can vote")
print("This always runs")You are an adult You can vote This always runs
Watching a False condition
This run stores 15 in temperature and tests temperature > 30. The comparison produces False, so Python jumps over the 2 indented lines without touching them, and the first thing it prints is the line after the block. Nothing inside a skipped block has any effect, which is the whole point of putting it inside an if.
temperature = 15
if temperature > 30:
print("It's hot outside")
print("Turn on the AC")
print("Weather check complete")Weather check complete
Several if statements in a row
Each if statement is checked on its own, so a program can hold any number of them, and each decides only about its own block. Below, 3 checks run against the same order. Predict which lines print before you reveal the output: the total is 85 and the coupon code is SAVE10.
cart_total = 85
coupon = "SAVE10"
if cart_total > 50:
print("Free shipping applied")
if coupon == "SAVE20":
print("20 percent off")
if cart_total > 200:
print("Gift wrap included")
print("Order total:", cart_total)Output:
Free shipping applied Order total: 85
The first condition, 85 > 50, is True, so the shipping line prints. The coupon test compares "SAVE10" with "SAVE20", 2 different strings, so == returns False and the discount line is skipped. The third condition fails too, because 85 is below 200. The final print has no indent, so it runs regardless, and the order total appears last.
What does this program print?
score = 70
if score >= 70:
print("Pass")
if score >= 90:
print("Distinction")
print("Done")- Pass, then Distinction, then Done
- Pass, then Done
- Done on its own
- Pass on its own
When you write a header yourself, check 2 things. The operator must point the way the rule means: a rule that warns above 30 needs >, because < would warn on cool days and == would warn only at exactly 30. And the line must end with a colon, which marks where the indented block begins.
About this lesson
This section is part of the Control Flow: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.