Branching with if-else
Concepts covered: Conditionals
An if on its own answers one question: run this block or skip it. Adding else gives Python a second block to run when the condition is False. The 2 blocks are alternatives. On any single run exactly one of them executes, so Python can't skip both or run both.
age = 16
if age >= 18:
print("Adult ticket")
else:
print("Child ticket")The else line sits at the same indent as its if, ends with a colon, and carries no condition. It needs none, because it covers every case the if condition did not. Here 16 >= 18 is False, so Python skips the first block and runs the second, printing Child ticket.
A branch can do more than print
Each block can hold as many lines as the decision needs, including assignments. The animation below picks a shipping fee: an order of 50 or more ships free, and anything less pays 5. After the branch finishes, the program continues with whichever value of shipping the chosen block set, so the grand total depends on that choice.
order_total = 42
if order_total >= 50:
print("Free shipping")
shipping = 0
else:
print("Shipping fee added")
shipping = 5
grand_total = order_total + shipping
print("Grand total:", grand_total)Shipping fee added Grand total: 47
Change order_total in your head and trace it again. The table below lists 3 runs: the condition's value, the block that runs, and what prints. Look at the middle row, where the total is exactly 50. >= includes the equal case, so 50 ships free. Had the condition been order_total > 50, that same order would pay the fee.
| order_total | order_total >= 50 | Block that runs | Printed |
|---|---|---|---|
| 30 | False | else | Shipping fee added, then Grand total: 35 |
| 50 | True | if | Free shipping, then Grand total: 50 |
| 75 | True | if | Free shipping, then Grand total: 75 |
Exactly one branch, every time
- if alone
- Runs the block or skips it
- On a False condition, nothing happens
- Use when the False case needs no action
- if with else
- Runs the first block or the second
- On a False condition, the else block runs
- Use when both outcomes need code
The remainder operator from Basic Operators makes a natural 2-way decision. seat_number % 2 is what's left after dividing by 2, so it is 0 for an even number and 1 for an odd one. Comparing that remainder to 0 splits every integer into exactly 2 groups, which is what if and else need. Predict the output for seat 17.
seat_number = 17
if seat_number % 2 == 0:
print("Even seat: window side")
else:
print("Odd seat: aisle side")
print("Seat", seat_number, "assigned")Output:
Odd seat: aisle side Seat 17 assigned
Dividing 17 by 2 leaves a remainder of 1, so 1 == 0 is False, the else block runs, and the aisle line prints. The last print is outside both blocks and prints the seat number afterwards, whichever branch ran.
- Do
- Keep else at the same indent as its if
- End the else line with a colon and put nothing between else and the colon
- Put code for the False case inside else rather than after the if
- Avoid
- Write a condition after else, such as else age < 18:, which is a syntax error
- Use a second if to handle the False case, because the 2 conditions can drift apart
- Leave a block empty, because Python requires at least 1 line in it
What does this program print?
temperature = 30
if temperature > 30:
print("Hot")
else:
print("Not hot")
print("Checked")- Hot, then Checked
- Not hot on its own
- Hot, then Not hot, then Checked
- Not hot, then Checked
The most frequent else mistake is giving it a condition. A line such as else order_total < 50: makes Python reject the whole file before a single line runs, because else already means every case the if condition missed. Deleting the condition and leaving else: restores the branch.
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.