LEARN Python - 2.3 Elif When Python Needs More Than Two Choices - zerotopyhero.com

Learn Python – 2.3: Elif – When Python Needs More Than Two Choices

Because Life Is Rarely Just Yes or No

So far, Python has learned how to choose between two paths.

  • If this is true → do this

  • Else → do that

That already makes your programs much smarter.
But real life rarely works in neat pairs.

Sometimes you don’t want:

“Yes or no?”

You want:

“If this… or this… or maybe that…”

That’s where elif comes in.

Why else Isn’t Always Enough

Let’s say you’re checking someone’s score:

  • 90 or above → Excellent

  • 70 to 89 → Good

  • 50 to 69 → Okay

  • Below 50 → Needs improvement

You could try to force this into one if and one else,
but it would get messy fast.

What you really want is a way to say:

“Otherwise… if this is true…”

That’s exactly what elif means.

What elif Means (In Plain English)

elif is short for “else if”.

Python reads it like this:

  • If this is true, do this

  • Else if this is true, do this

  • Else if this is true, do this

  • Otherwise, do this

Only one of these paths will run.

Python checks them from top to bottom and stops as soon as it finds a match.

Your First elif Example

				
					score = int(input("Enter your score: "))

if score >= 90:
    print("Excellent!")
elif score >= 70:
    print("Good job!")
elif score >= 50:
    print("Not bad!")
else:
    print("Needs improvement.")

				
			

Read it like a conversation:

  • If the score is 90 or more → “Excellent!”

  • Otherwise, if it’s 70 or more → “Good job!”

  • Otherwise, if it’s 50 or more → “Not bad!”

  • Otherwise → “Needs improvement.”

Clean. Clear. Human.

Order Matters (A Lot)

This part is important.

Python checks conditions in order, top to bottom.

Once one condition is true, Python runs that block and skips the rest.

For example:

				
					age = 25

if age >= 18:
    print("Adult")
elif age >= 13:
    print("Teen")
else:
    print("Child")
				
			

This works correctly.

But this would not:

				
					if age >= 13:
    print("Teen")
elif age >= 18:
    print("Adult")

				
			

Why?

Because age >= 13 is already true for adults.
Python never even checks the second condition.

Rule of thumb:
Always put the most specific or highest conditions first.

You Can Have Many elifs (But Only One if and else each)

A full decision chain looks like this:

				
					if condition:
    ...
elif condition:
    ...
elif condition:
    ...
else:
    ...

				
			

You can have:

  • One if

  • As many elifs as you need

  • At most one else (at the end)

You can also skip else if it doesn’t make sense — but when you do use it, it should catch everything left over.

A Friendly Example

				
					mood = input("How are you feeling today? ").lower()

if mood == "happy":
    print("Love that for you!")
elif mood == "tired":
    print("Coffee might help.")
elif mood == "stressed":
    print("Deep breath. You’ve got this.")
else:
    print("Thanks for sharing. Hope your day gets better!")
				
			

Different input.
Different response.
Same program.

That’s the power of elif.

Thinking It Through Before Coding

Before writing if / elif / else, ask yourself:

  • What are all the possible situations?

  • Which ones should be checked first?

  • What should happen if none match?

If you can answer those questions in plain English, your code will follow naturally.

What You’ve Learned

You now know how to:

  • Handle more than two outcomes

  • Use elif for additional checks

  • Control the order of decisions

  • Build clean decision chains

  • Avoid confusing nested logic

This is real programming territory now.

Mini Quiz

Try these:

  1. What does elif stand for?

  2. How many elifs can an if statement have?

  3. Why does the order of conditions matter?

  4. Will Python ever run more than one if / elif / else block in the same chain?

  5. What will this code print?

				
					number = 15

if number > 20:
    print("Big")
elif number > 10:
    print("Medium")
else:
    print("Small")

				
			
ZeroToPyHero