Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔁 Unit 2 – Flow of Control & Functions  ·  Lesson 15

if, elif, else (Selection)

What is selection?

So far our programs ran every line from top to bottom (sequence). Real programs must choose: show "Pass" or "Fail", give a discount only above ₹1000, allow login only if the password matches. Choosing between paths based on a condition is called selection or decision making, and Python uses the keywords if, elif and else.

StartInput nn % 2 == 0 ?YesNoPrint "odd"Print "even"Stop
An if-else decision

Indentation

Python uses indentation (spaces at the start of a line) to show which statements belong to a block. The line with if ends with a colon :, and the lines inside are indented by the same amount (usually 4 spaces). Other languages use { } for this.

Python
if 10 > 5:
print('yes')
IndentationError: expected an indented block after 'if' statement on line 1

Simple if

Syntax
if condition:
    statement(s)      # run only when condition is True
Python
age = int(input('Enter your age: '))
if age >= 18:
    print('You can vote.')
print('Thank you!')            # not indented: always runs
Enter your age: 19 You can vote. Thank you!

if-else

Exactly one of the two blocks runs.

Python
n = int(input('Enter a number: '))
if n % 2 == 0:
    print(n, 'is even')
else:
    print(n, 'is odd')
Enter a number: 37 37 is odd

if-elif-else ladder

When there are many choices, use elif ("else if"). Python checks conditions top to bottom and runs only the first block whose condition is True.

Python
marks = float(input('Enter percentage: '))
if marks >= 90:
    grade = 'A'
elif marks >= 75:
    grade = 'B'
elif marks >= 60:
    grade = 'C'
elif marks >= 33:
    grade = 'D'
else:
    grade = 'E (needs improvement)'
print('Grade:', grade)
Enter percentage: 78.5 Grade: B
marks >= 90 ?False → check nextmarks >= 75 ?True → grade = B, skip the restmarks >= 60 ?not checkedmarks >= 33 ?not checked
For 78.5, Python stops at the second condition
⚠️ Order matters

If you write marks >= 33 first, every passing student gets grade D. Put the strictest condition first.

Nested if

An if inside another if is a nested if. Example: largest of three numbers.

Python
a, b, c = 12, 45, 29
if a >= b:
    if a >= c:
        print('Largest is', a)
    else:
        print('Largest is', c)
else:
    if b >= c:
        print('Largest is', b)
    else:
        print('Largest is', c)
Largest is 45

The same can be written more simply with and: if a >= b and a >= c: … or just max(a, b, c).

Solved programs

1. Leap year

Python
year = int(input('Enter year: '))
if year % 400 == 0:
    print(year, 'is a leap year')
elif year % 100 == 0:
    print(year, 'is not a leap year')
elif year % 4 == 0:
    print(year, 'is a leap year')
else:
    print(year, 'is not a leap year')
Enter year: 1900 1900 is not a leap year

2. Electricity bill with slabs

First 100 units ₹3/unit, next 100 units ₹5/unit, above 200 units ₹7/unit.

Python
units = int(input('Units consumed: '))
if units <= 100:
    bill = units * 3
elif units <= 200:
    bill = 100 * 3 + (units - 100) * 5
else:
    bill = 100 * 3 + 100 * 5 + (units - 200) * 7
print('Electricity bill = Rs.', bill)
Units consumed: 260 Electricity bill = Rs. 1220
Bill for 260 units = 300 + 500 + 4200200400600₹0–100101–200201–260300500420
How the slab bill adds up

3. Menu-driven calculator

Python
print('1. Add  2. Subtract  3. Multiply  4. Divide')
choice = int(input('Your choice: '))
x = float(input('First number: '))
y = float(input('Second number: '))
if choice == 1:
    print('Result =', x + y)
elif choice == 2:
    print('Result =', x - y)
elif choice == 3:
    print('Result =', x * y)
elif choice == 4:
    if y == 0:
        print('Cannot divide by zero')
    else:
        print('Result =', x / y)
else:
    print('Invalid choice')
1. Add 2. Subtract 3. Multiply 4. Divide Your choice: 4 First number: 22 Second number: 8 Result = 2.75

One-line if (conditional expression)

Python
temp = 31
status = 'Hot' if temp > 30 else 'Pleasant'    # one-line if-else
print(status)
Hot

Summary & practice

  • if runs a block when the condition is True; else when it is False.
  • elif checks many conditions; only the first true block runs.
  • Colon : at the end and consistent indentation are compulsory.
  • Nested if = if inside if.
✅ Practice

Write programs to: (1) check whether a character is a vowel, (2) find the absolute value of a number without abs(), (3) give 10% discount if the bill is above ₹2000, otherwise 5%.

Hint: (1) if ch.lower() in 'aeiou': (2) if n < 0: n = -n

Frequently Asked Questions

What is the difference between if-elif and many separate if statements?
In an if-elif ladder Python stops at the first true condition and skips the rest. With separate if statements every condition is checked, so more than one block may run.
How many spaces should I use for indentation?
Four spaces is the standard. Any consistent number works, but every line of the same block must have exactly the same indentation.
Is else compulsory after if?
No. else is optional. Use it only when something must happen when the condition is false.
← Class 11 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicfor & while Loops with range() Your progress: 15 / 29 (52%) · Lesson 16 / 29 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free