Beginner · Text and decisions · topic 8 of 50 · 25 min
if, elif and else
Explanation
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.
marks = 72
if marks >= 33:
print("You passed!")
print("Result checked")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.
else: the other path
marks = 25
if marks >= 33:
print("Pass")
else:
print("Fail")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.
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)Nested if
A nested if is an if inside another if. The inner block gets 8 spaces: 4 for each level.
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")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.
marks = 45
result = "Pass" if marks >= 33 else "Fail"
print(result)
n = 7
print("Even" if n % 2 == 0 else "Odd")Watch the lesson
if, elif and else · 1/7
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.
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}")Practice (0/3 done)
Write a function exam_result(marks) that returns "Pass" when marks is 33 or more, and "Fail" otherwise.
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".
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
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.