PY-101 ยท Conditions
if, elif and else
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
An if statement runs a block of code only when its condition is True. elif checks another condition when the ones before it were False, and else runs when none of them were. Python checks from the top and runs only the first branch that matches.
- Write if, elif and else
- Know that only one branch runs
- Indent blocks correctly
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
total = 350if total >= 500:delivery = 0elif total >= 200:delivery = 40else:delivery = 80print("Delivery:", delivery)print("Pay:", total + delivery)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Delivery: 40
Pay: 390 ๐ฏ Set grade for score = 72: "A" for 90 and above, "B" for 75 and above, "C" for 50 and above, otherwise "F". Print the grade.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
Check 90 first, then 75, then 50, then else.
- The problemA shop gives free delivery over โน499, โน40 delivery over โน199, and โน80 below that. Every order needs exactly one of those answers, picked automatically.
- The Python idea
if condition:An if statement runs a block of code only when its condition is True. - In real lifeBanks use the same shape to set interest rates by balance band, and HR systems use it to pick tax slabs.
Start with a question
A shop gives free delivery over โน499, โน40 delivery over โน199, and โน80 below that. Every order needs exactly one of those answers, picked automatically.
Every program that reacts to data, from a login screen to a game, makes decisions. if/elif/else is how Python makes them.
Explain it like I'm 10
It is like a traffic light: if it is green, go; else if it is yellow, slow down; otherwise stop. You only ever do one of them.
The technical explanation
The condition after if is anything that is True or False. A colon ends the line, and the block underneath is indented by four spaces.
elif means "else if". Python tries each condition in order and stops at the first True one. The rest are skipped.
else has no condition. It catches everything that did not match. You can leave it out if nothing should happen.
Syntax
if condition:
...
elif other_condition:
...
else:
... Real-world scenario
Banks use the same shape to set interest rates by balance band, and HR systems use it to pick tax slabs.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Nested conditions โQuestions people ask
What is the difference between if and elif in Python?
Each if starts a new check that always runs. elif belongs to the if above it and only runs when everything before it in the chain was False.
Does Python have switch statements?
Python 3.10 added match/case, which works like a more powerful switch. For simple ranges, if/elif/else is still the usual choice.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is the difference between if and elif in Python?
Each if starts a new check that always runs. elif belongs to the if above it and only runs when everything before it in the chain was False.
beginner Does Python have switch statements?
Python 3.10 added match/case, which works like a more powerful switch. For simple ranges, if/elif/else is still the usual choice.
intermediate How many branches of an if/elif/else chain run?
At most one. Python runs the first matching branch and skips the rest.
intermediate What ends the if line?
A colon. Python uses a colon, then an indented block.
Quick revision
Remember
- Write if, elif and else
- Know that only one branch runs
- Indent blocks correctly
- Order matters. Put the most specific condition first: if you check
score >= 50beforescore >= 90, a score of 95 never reaches the 90 branch.
Syntax
if condition:
...
elif other_condition:
...
else:
... Official documentation
- if statements (docs.python.org)