Skip to content

Beginner · Text and decisions · topic 8 of 50 · 25 min

if, elif and else

Explanation

An if statement runs some lines only when a condition is True. else gives a second path for when it is False, and elif lets you check several conditions one after another. The lines inside each part must be indented by 4 spaces.

Making decisions

So far every line of your programs ran. Real programs must choose: pass or fail, free or paid, discount or no discount. An if statement runs a group of lines only when a condition is True. A condition is any expression that gives True or False.

python
marks = 72
if marks >= 33:
    print("You passed!")
print("Result checked")
The condition is True, so both lines print. With marks = 20 only "Result checked" would print.

Look at the shape. The if line ends with a colon. The lines that belong to the if are indented, which means they start with 4 spaces. This indented group is called a block. The first line that is not indented is outside the if and always runs.

Think of it like this: An if statement is like a fork in the road. A signboard says "If you are going to Pune, turn left". Drivers going to Pune take the left road; everyone else continues straight. Afterwards, both roads join again.

else: the other path

python
marks = 25
if marks >= 33:
    print("Pass")
else:
    print("Fail")
Shows Fail. Exactly one of the two blocks runs, never both.

elif: checking more conditions

elif means "else if". Python checks the conditions from top to bottom and runs the block of the FIRST one that is True. It skips all the rest. If none is True, the else block runs.

python
marks = 78
if marks >= 90:
    grade = "A"
elif marks >= 75:
    grade = "B"
elif marks >= 60:
    grade = "C"
elif marks >= 33:
    grade = "D"
else:
    grade = "F"
print("Grade:", grade)
Shows Grade: B. 78 is not >= 90, but it is >= 75, so Python stops there.
Careful: Order matters in an elif chain. If you check marks >= 33 first, then 95 marks also matches it and gets grade D, because Python stops at the first True condition. Put the strictest condition (the highest marks) first.

Nested if

A nested if is an if inside another if. The inner block gets 8 spaces: 4 for each level.

python
age = 67
has_id = True
if age >= 60:
    if has_id:
        print("Senior ticket: Rs 120")
    else:
        print("Please show an ID card")
else:
    print("Normal ticket: Rs 200")
Shows Senior ticket: Rs 120. The inner if is checked only for people aged 60 or more.

The conditional expression

For a simple two-way choice there is a one-line form: value_if_true if condition else value_if_false. It is called a conditional expression.

python
marks = 45
result = "Pass" if marks >= 33 else "Fail"
print(result)
n = 7
print("Even" if n % 2 == 0 else "Odd")
Shows Pass and then Odd.
Careful: Forgetting the colon after if, elif or else gives a SyntaxError. Mixing up spaces (for example 3 spaces on one line and 4 on the next) gives an IndentationError. Always use 4 spaces for each level.
Tip: Use one if with elif branches when only one choice should win. Several separate if statements are all checked, so more than one block can run.

Watch the lesson

if, elif and else · 1/7

if · elif · else

In this lesson your programs will start making decisions with if, elif and else.

Prefer a video by a teacher? Find free videos on “python if elif else tutorial for beginners” on YouTube ↗

In real life: Choosing a GST rate

A billing program picks the GST rate from the type of item, then adds the tax to the price. The rates here are simplified for practice: real GST rates depend on the exact item and can change, so always check the official rate before using it for real bills.

python
category = "standard"
price = 1000
if category == "essential":
    rate = 0
elif category == "basic":
    rate = 5
elif category == "standard":
    rate = 18
else:
    rate = 40
tax = price * rate / 100
print(f"GST {rate}%: Rs {tax:.2f}, total Rs {price + tax:.2f}")
Shows GST 18%: Rs 180.00, total Rs 1180.00.

Practice (0/3 done)

Task 1 · Code · Python

Write a function exam_result(marks) that returns "Pass" when marks is 33 or more, and "Fail" otherwise.

Task 2 · Code · Python

The function grade(marks) below has its checks in the wrong order, so 95 marks gets "D". Fix it so that: 90 or more is "A", 75 or more is "B", 60 or more is "C", 33 or more is "D", and below 33 is "F".

Task 3 · Code · Python

A zoo charges by age: under 5 years is free (0), under 12 is Rs 100, 60 or more is Rs 120, and everyone else pays Rs 200. Write ticket_price(age) that returns the price as a number.

Quick quiz

Question 1

1.Which character must come at the end of an if line?

Question 2

2.x = 15. Then: if x > 10: print("big") elif x > 5: print("medium"). What is printed?

Question 3

3.How many spaces are normally used for one level of indentation in Python?

Question 4

4.What does "Even" if 7 % 2 == 0 else "Odd" give?

Question 5

5.When does the else block of an if / elif / else run?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Booleans and comparisons