⚡0 XP🔥0

if, elif, else (Decision Making)

Lesson 9 of 33 ⏱ 5 min read ✎ 3 exercises ❓ 5-question quiz ▶ Runs Python

Make decisions in your programs with if, if-else, if-elif-else, nested conditions, the ternary expression and match-case.

Why do we need decisions?#

Real programs must behave differently in different situations:

  • If the password is correct → log in, otherwise → show an error.
  • If the bill is above Rs. 1000 → give a discount.
  • If marks are below 33 → result is "Fail".

Python makes decisions using if, elif and else.

The if statement#

if condition:
    # code that runs only when condition is True
  • condition is any expression that gives True or False (usually a comparison).
  • The colon : is required.
  • The code inside must be indented (4 spaces).
▶ Try it YourselfPython
Ctrl + Enter to run

Change temperature to 25 and run again - the two indented lines are skipped.

The if...else statement#

else gives an alternative when the condition is False:

▶ Try it YourselfPython
Ctrl + Enter to run

Exactly one of the two blocks runs - never both, never neither.

The if...elif...else ladder#

When there are more than two possibilities, use elif ("else if"):

▶ Try it YourselfPython
Ctrl + Enter to run

Python checks the conditions from top to bottom and runs only the first block whose condition is True. The rest are skipped.

हिंदी में समझें

if-elif-else को एक सीढ़ी (ladder) की तरह समझें। Python ऊपर से नीचे एक-एक condition जाँचता है। जैसे ही कोई condition True मिलती है, उसी का block चलता है और बाकी सभी conditions छोड़ दी जाती हैं।

ऊपर के example में marks = 68 है। पहली condition (68 >= 75) False है, दूसरी (68 >= 60) True है - इसलिए "First division" print होता है, और नीचे की conditions check ही नहीं होतीं।

इसीलिए क्रम (order) बहुत ज़रूरी है - सबसे बड़ी range पहले लिखें।

Common Mistake

Order matters. This version is wrong:

if marks >= 33:
    print("Third division")
elif marks >= 75:
    print("Distinction")    # never reached for 80, because 80 >= 33 is already True

Several ifs vs if-elif#

x = 15
if x > 10: print("big")
if x > 5:  print("medium")     # separate if - also checked

This prints both lines. With elif, only the first match would print. Use separate ifs when the checks are independent, and elif when they are alternatives.

Combining conditions#

Use and, or, not:

▶ Try it YourselfPython
Ctrl + Enter to run

Nested if#

An if inside another if:

▶ Try it YourselfPython
Ctrl + Enter to run
Tip

Deep nesting (3-4 levels) becomes hard to read. Often you can flatten it with and or with elif.

Truthy and falsy conditions#

The condition doesn't have to be a comparison. Any value works - empty/zero values are treated as False:

▶ Try it YourselfPython
Ctrl + Enter to run

Conditional expression (one-line if-else)#

value_if_true if condition else value_if_false
▶ Try it YourselfPython
Ctrl + Enter to run

The pass placeholder#

A block can't be empty. If you haven't written it yet, use pass:

if score > 100:
    pass    # TODO: handle bonus later

match-case (Python 3.10+)#

When you compare one value against many fixed options, match is cleaner than a long elif chain:

▶ Try it YourselfPython
Ctrl + Enter to run

case _ is the default (like else). | means "or".

Practical example: electricity bill#

▶ Try it YourselfPython
Ctrl + Enter to run

Summary#

  • if condition: runs a block only when the condition is True.
  • else: runs when it is False. elif checks further conditions - only the first True branch runs.
  • Combine conditions with and, or, not; nest ifs when needed.
  • Empty/zero values are falsy.
  • One-line form: x if condition else y. For many fixed options, use match-case.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Positive, negative or zero

Read an integer and print Positive, Negative or Zero.

Test input:
-15
Expected output:
Negative
2

Grade calculator

Read the marks (0-100) and print the grade:

Marks Grade
90 and above A+
75 to 89 A
60 to 74 B
33 to 59 C
below 33 Fail
Test input:
74
Expected output:
B
3

Leap year

Read a year and print Leap year or Not a leap year.

Rule: a year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. (So 2000 is a leap year, 1900 is not.)

Test input:
1900
Expected output:
Not a leap year

Quick Quiz

Q1.

What is printed?

x = 7
if x > 10:
    print("A")
elif x > 5:
    print("B")
else:
    print("C")
Q2.

How many elif blocks can an if statement have?

Q3.

What does result = "Pass" if marks >= 33 else "Fail" do when marks = 50?

Q4.

What is printed?

name = ""
if name:
    print("Hi", name)
else:
    print("No name")
Q5.

Which line has a mistake?