2. Making Decisions & Looping
Making Decisions with if, elif, else & Logic
Teach your program to make choices step by step: understand True/False comparisons, why 4-space indentation matters, how if/elif/else picks one path, and how and/or/not work.
- Write True/False comparisons using ==, !=, >, <, >=, and <=
- Guide program flow using 4-space indentation with if, elif, and else
- Combine multiple rules cleanly with and, or, and not
Think about everyday decisions you make without noticing:
- If it is raining, take an umbrella. Otherwise, leave it at home.
- If your order is
$50or more, shipping is free. Otherwise, shipping costs$5.
Without decisions, a program can only follow one straight path. In Python, if, elif, and else let your code check conditions and choose the right path automatically.
Asking Yes/No Questions (Comparisons)
Before Python can decide, you give it a test whose answer is either True (yes) or False (no):
| Code | Question You Are Asking | Example | Answer |
|---|---|---|---|
== | Are these equal? (two equals signs!) | 5 == 5 | True |
!= | Are these different? | 5 != 3 | True |
> | Is the left bigger? | 10 > 20 | False |
< | Is the left smaller? | 10 < 20 | True |
>= | Bigger or equal? | 18 >= 18 | True |
<= | Smaller or equal? | 12 <= 10 | False |
Why two equals signs (==)? Because a single = already means "save a value into a variable." So Python uses == to ask "are these two things equal?"
Your First if Statement (And Why Spacing Matters!)
age = 20
if age >= 18:
print("You can enter the movie.")
print("Enjoy the show!")
print("Have a nice day!")Look closely at two details:
- There is a colon
:at the end of theifline. That colon tells Python: "An indented block is coming next!" - Both
printlines underifare pushed inward by 4 spaces. That spacing (called indentation) tells Python which lines belong inside theifdecision. - The last line (
print("Have a nice day!")) is not indented, so it ALWAYS runs whetherageis20or10.
Handling "Otherwise" (else) and Multiple Choices (elif)
What if you want one message when a condition is True, and a different message when it is False? Add else::
cart = 35
if cart >= 50:
print("Free shipping!")
else:
print("Shipping is $5.")And what if you have 3 or more possibilities—like grading an exam (A, B, C, or Try Again)? Use elif (short for "else if"):
score = 82
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 50:
print("Grade: C")
else:
print("Grade: Try Again")How Python Walks Through if / elif / else
When score = 82, Python checks from top to bottom, enters `score >= 75`, and skips the rest.
Python starts at the top and walks downward like checking doors:
- Is
82 >= 90? No (False). Skip it. - Is
82 >= 75? Yes (True)! Runprint("Grade: B"). - Stop checking! Even though
82 >= 50is also true, Python already found its matching door, so it skips the rest ofelifandelse.
Because Python stops at the first match, always put your highest/strictest checks at the top!
Checking Two Things at Once (and, or, not)
Suppose a rollercoaster requires you to be at least 12 years old AND at least 140 cm tall:
age = 14
height_cm = 145
if age >= 12 and height_cm >= 140:
print("You can ride!")and: Both conditions must beTrue.or: At least one condition must beTrue(great for"if it's Saturday OR Sunday, sleep in!").not: FlipsTruetoFalse, andFalsetoTrue(if not is_logged_in:).
Common Mistakes to Avoid
- Writing
=instead of==:if age = 18:fails immediately withSyntaxError. Always double-check for==. - Forgetting the colon
:at the end ofif,elif, orelse. - Wrong branch order: If you put
if score >= 50:beforeelif score >= 90:, a score of95stops at>= 50and printsCinstead ofA!
Try It Yourself in the Playground
Change cart_total (try 20, 35, and 65) and is_member (True or False) and watch which branch runs:
Click “Run Code” to execute this snippet right in your browser.
Mini-Challenge: Write an if / elif / else check for num = 15:
- If
numdivides evenly by both3and5(num % 3 == 0 and num % 5 == 0), print"FizzBuzz" - Else if
num % 3 == 0, print"Fizz" - Else if
num % 5 == 0, print"Buzz" - Else print
num
Quick Checkpoints
If score = 95, what does if score >= 50: print('Pass') elif score >= 90: print('Top') print?
When is is_weekend or is_holiday equal to False?