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.

5 min readUpdated 2026-11-29
In This Lesson
  • 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 $50 or 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):

CodeQuestion You Are AskingExampleAnswer
==Are these equal? (two equals signs!)5 == 5True
!=Are these different?5 != 3True
>Is the left bigger?10 > 20False
<Is the left smaller?10 < 20True
>=Bigger or equal?18 >= 18True
<=Smaller or equal?12 <= 10False

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!)

python
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:

  1. There is a colon : at the end of the if line. That colon tells Python: "An indented block is coming next!"
  2. Both print lines under if are pushed inward by 4 spaces. That spacing (called indentation) tells Python which lines belong inside the if decision.
  3. The last line (print("Have a nice day!")) is not indented, so it ALWAYS runs whether age is 20 or 10.

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::

python
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"):

python
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

How Python Picks Only the First True Branch
score = 82
if score >= 90:False → Next
elif score >= 75:True → Grade: B
elif score >= 50 / elseSkipped!

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:

  1. Is 82 >= 90? No (False). Skip it.
  2. Is 82 >= 75? Yes (True)! Run print("Grade: B").
  3. Stop checking! Even though 82 >= 50 is also true, Python already found its matching door, so it skips the rest of elif and else.

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:

python
age = 14
height_cm = 145

if age >= 12 and height_cm >= 140:
    print("You can ride!")
  • and: Both conditions must be True.
  • or: At least one condition must be True (great for "if it's Saturday OR Sunday, sleep in!").
  • not: Flips True to False, and False to True (if not is_logged_in:).

Common Mistakes to Avoid

3 Classic if/else Bugs
  1. Writing = instead of ==: if age = 18: fails immediately with SyntaxError. Always double-check for ==.
  2. Forgetting the colon : at the end of if, elif, or else.
  3. Wrong branch order: If you put if score >= 50: before elif score >= 90:, a score of 95 stops at >= 50 and prints C instead of A!

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:

Python 3Try It Live: Store Shipping Calculator
Output

Click “Run Code” to execute this snippet right in your browser.

Mini-Challenge: Write an if / elif / else check for num = 15:

  • If num divides evenly by both 3 and 5 (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

Check Your Understanding

If score = 95, what does if score >= 50: print('Pass') elif score >= 90: print('Top') print?

Check Your Understanding

When is is_weekend or is_holiday equal to False?