Stop 03 · Control FlowTopics · 2/8

If, Elif & Else

Part of the Python Roadmap

In short

Until now your programs ran every line, top to bottom. With if, elif and else they start making decisions: give a grade, apply a discount, show an error. This is where code begins to feel smart.

An if statement runs a block only when its condition is true. elif adds more checks and else catches everything left. Python runs only the first matching branch, so order your conditions carefully.

The jaadu stands at a hilly road junction reading a wooden signpost with three arrows, one hand shading its eyes as it picks a path.

Think of it like this

Road signs at a junction

Driving to Ooty, you reach a junction with signs: "Mysuru: left. Coimbatore: right. Everything else: straight." You read them in order and take the first one that matches. You never take two roads at once. Python's if, elif and else work exactly like those signs.

Watch it happen

Grading marks = 82

marks >= 90?
Yes: A+
No: marks >= 75?
Yes: A
No: marks >= 60?
Yes: B
No: else

1/4Python checks the if condition first. 82 >= 90 is False, so it moves on.

The big ideas

What you need to know

Checked top to bottom

Python tests each condition in order and runs only the first block whose condition is True. Put the most specific checks first.

Colon and indent

Every if, elif and else line ends with a colon, and the block under it is indented four spaces. The indent is what defines the block.

else is optional

else catches everything that didn't match. Leave it out when nothing should happen in that case.

One-line conditional

status = "Pass" if marks >= 40 else "Fail" picks between two values in a single expression.

See it in code

Walk through the code

python
1marks = 822 3if marks >= 90:4    grade = "A+"5elif marks >= 75:6    grade = "A"7elif marks >= 60:8    grade = "B"9elif marks >= 40:10    grade = "C"11else:12    grade = "Fail"13 14print(f"Marks: {marks}, Grade: {grade}")   # Marks: 82, Grade: A15 16status = "Pass" if marks >= 40 else "Fail"17print(status)                              # Pass

1/5The first check. 82 is not 90 or more, so grade = "A+" is skipped.

A grade calculator. Change marks to 95, 65 or 30 and predict the output before running it.

Save yourself the pain

Common mistakes

Oops

Ordering checks from smallest to biggest, like if marks >= 40 first, so everyone above 40 gets a C.

Do this instead

Put the strictest condition first (>= 90), then work down. The first True branch wins.

Oops

Using several separate if statements when only one should run.

Do this instead

Chain them with elif. Separate ifs are each checked, so more than one block can run.

Oops

Forgetting the colon or the indent after if.

Do this instead

Write if marks >= 90: and indent the next line by four spaces. VS Code does this for you after the colon.

Your mission

Electricity bill slabs

~25 min

Quick check

With x = 15, what does this print? if x > 10: print("A") elif x > 5: print("B") else: print("C")

Go deeper

  1. 1Write a grade calculator that turns marks into A, B, C or Fail.
  2. 2Put the most specific condition first and test the boundary values.
  3. 3Use a one-line conditional expression like status = 'adult' if age >= 18 else 'minor'.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge