Python if-elif-else Statement
The if-elif-else statement is used when a program needs to
check multiple conditions. Python checks the conditions from
top to bottom and executes the block belonging to the first condition
that is True.
The elif keyword means "else if". It allows us to
test additional conditions after the initial if condition.
Syntax of if-elif-else
if condition1:
statement1
elif condition2:
statement2
else:
statement3
The if condition is checked first. If it is false, the
elif condition is checked. If all conditions are false,
the else block is executed.
Simple if-elif Example
marks = 75
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Grade A
Since marks >= 90 is false, Python checks
marks >= 75. This condition is true, so the remaining
conditions are not checked.
if-elif with User Input
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
elif marks >= 40:
print("Grade C")
else:
print("Fail")
Enter your marks: 82 Grade A
Checking Positive, Negative and Zero
number = int(input("Enter a number: "))
if number > 0:
print("Positive number")
elif number < 0:
print("Negative number")
else:
print("Zero")
Enter a number: -10 Negative number
Three different possibilities are handled using one
if, one elif and one else.
Checking Even and Odd Numbers
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
elif number % 2 != 0:
print("Odd number")
Enter a number: 15 Odd number
Finding the Greatest of Three Numbers
a = 25
b = 40
c = 30
if a >= b and a >= c:
print("a is greatest")
elif b >= a and b >= c:
print("b is greatest")
else:
print("c is greatest")
b is greatest
Checking Age Group
age = int(input("Enter your age: "))
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior citizen")
Enter your age: 25 Adult
if-elif with Strings
day = input("Enter a day: ")
if day == "Monday":
print("Start of the week")
elif day == "Friday":
print("Last working day")
elif day == "Sunday":
print("Holiday")
else:
print("Regular day")
Enter a day: Sunday Holiday
Checking Vowels
ch = input("Enter a character: ")
if ch == "a":
print("Vowel")
elif ch == "e":
print("Vowel")
elif ch == "i":
print("Vowel")
elif ch == "o":
print("Vowel")
elif ch == "u":
print("Vowel")
else:
print("Not a vowel")
Enter a character: e Vowel
if-elif with Logical Operators
age = 25
salary = 40000
if age < 18:
print("Not eligible")
elif age >= 18 and salary >= 30000:
print("Eligible")
else:
print("Conditions not satisfied")
Eligible
Multiple elif Conditions
Python allows multiple elif statements in a single
conditional structure.
marks = 68
if marks >= 90:
print("Excellent")
elif marks >= 80:
print("Very Good")
elif marks >= 70:
print("Good")
elif marks >= 60:
print("Average")
elif marks >= 40:
print("Pass")
else:
print("Fail")
Average
if-elif without else
The else block is optional. An if-elif
structure can be used without an else.
number = 10
if number == 5:
print("Number is 5")
elif number == 10:
print("Number is 10")
Number is 10
First True Condition is Executed
When multiple conditions are true, Python executes only the first
true condition and skips the remaining elif and
else blocks.
number = 20
if number > 0:
print("Positive")
elif number > 10:
print("Greater than 10")
else:
print("Other")
Positive
Both conditions are true, but only the first if block
is executed.
Order of Conditions
The order of conditions is important in an if-elif-else
statement. More specific or higher-priority conditions should
generally be checked before broader conditions.
marks = 85
if marks >= 90:
print("A+")
elif marks >= 75:
print("A")
elif marks >= 60:
print("B")
else:
print("C")
A
Practical Example: Electricity Bill
units = int(input("Enter units consumed: "))
if units <= 100:
print("Low consumption")
elif units <= 200:
print("Medium consumption")
elif units <= 500:
print("High consumption")
else:
print("Very high consumption")
Enter units consumed: 250 High consumption
Practical Example: Calculator
a = 20
b = 5
operator = "*"
if operator == "+":
print(a + b)
elif operator == "-":
print(a - b)
elif operator == "*":
print(a * b)
elif operator == "/":
print(a / b)
else:
print("Invalid operator")
100
Practical Example: Student Result
marks = int(input("Enter marks: "))
if marks >= 75:
print("Distinction")
elif marks >= 60:
print("First Division")
elif marks >= 50:
print("Second Division")
elif marks >= 40:
print("Pass")
else:
print("Fail")
Enter marks: 72 First Division
Difference between if, elif and else
| Keyword | Purpose |
|---|---|
if |
Checks the first condition. |
elif |
Checks another condition when the previous condition is false. |
else |
Executes when all preceding conditions are false. |
if-elif-else vs Nested if
| if-elif-else | Nested if |
|---|---|
| Checks multiple alternative conditions. | Places one decision inside another decision. |
| Conditions are checked sequentially. | The inner condition depends on the outer condition. |
| Useful for selecting one option from several alternatives. | Useful when a second decision is required after the first decision. |
Important Points
elifstands for else if.- Multiple
elifblocks can be used. - The
elseblock is optional. - Conditions are checked from top to bottom.
- Only the first true condition is executed.
- Once a condition is true, the remaining conditions are skipped.
- Proper indentation is required.
- The colon
:is required afterif,elifandelse.
if-elif-else statement is used when a program needs
to choose one action from multiple possible conditions. Python
executes the block belonging to the first condition that evaluates
to True.