Conditional Statements Part - 2
M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 7 min read
3. If - elif - else Statements:-
The if-elif statement is a shortcut for chaining multiple if-else conditions. While using if-elif statement at the end else block is added which is performed if none of the above if-elif statement is true.
elif (else if) : elif stands for "else if" and is used in conditional statements to handle multiple conditions efficiently. It acts as an intermediate step between if and else, allowing Python to check additional conditions if the initial if statement evaluates to False.
Syntax:
if condition1:
# Code block if condition1 is True
elif condition2:
# Code block if condition2 is True
elif condition3:
# Code block if condition3 is True
…..
…….
elif condition N:
# Code block if conditionN is True
else:
# Code block if none of the conditions are True
NOTE:
- The if block is evaluated first.
- The elif blocks are evaluated in order, only if the previous conditions were False.
- The else block is optional and runs only if all previous conditions are False.
Example 1: Write a program to get the temperatures of City and print appropriate massage
according the following-
temperature greater 30 --à it’s Hot outside
temperature greater 20 --à It's warm."
temperature greater 10 --à It's cool
otherwise --à print it’s cold
temp = int(input(“plz enter the temperature of your city”))
if temp > 30:
print("It's hot outside.")
elif temp > 20:
print("It's warm.")
elif temp > 10:
print("It's cool.")
else:
print("It's cold.")
4. Nested if - else Statements
A nested if...else statement in Python is when you place one if...else block inside another. It's used to test multiple conditions in a hierarchical or dependent manner.
Syntax :
if condition1:
if condition2:
# code block if both condition1 and condition2 are True
else:
# code block if condition1 is True but condition2 is False
else:
# code block if condition1 is False
When to Use:
- When one condition logically depends on another.
- To create multi-level decision trees.
Example:
x = 25 or x=int(input(“Plz enter the number”))
if x > 10:
print("x is greater than 10")
if x > 20:
print("x is also greater than 20")
else:
print("x is greater than 10 but not more than 20")
else:
print("x is 10 or less")
Example 1: Write a program to accept price and quantity of item from user and gives the discount as per the following slabs Structure:
- 20% on amount exceeding 10000,
- 10% for amount between 5000 To10000,
- 5% if it is between 1000 To 5000.
- no discount if amount<1000
quan=int(input("Plz enter the quantity of an items"))
price=int(input("Plz enter the price of an items"))
amount = quan*price
print('Amount = ',amount)
if amount > 10000:
discount = amount * 20 / 100
else:
if amount > 5000:
discount = amount * 10 / 100
else:
if amount > 1000:
discount = amount * 5 / 100
else:
discount = 0
print ('Payable amount = ', amount - discount)
Nested if statements: An if statement inside another if.
OR
A nested if statement in Python means placing one if statement inside another. It's used when you want to make a decision only after another condition is met.
Example: Write a Python program that checks if a person is allowed to enter to vote based on their age and whether they have a valid ID.
age = 20
v_id = True
if age >= 18:
if v_id:
print("You are allowed to enter.")
else:
print("You need a valid ID to enter.")
else:
print("You must be at least 18 to enter.")
Example: Write a Python program to evaluate a student's exam result and attendance status: If the score is 60 or higher, print "You passed the exam. "Then, check if the attendance is 75 or higher:
print "You also met the attendance requirement." If not, print "But you did not meet the attendance requirement." And If the score is less than 60, print "You failed the exam."
score = 85
attendance = 90
if score >= 60:
print("You passed the exam.")
if attendance >= 75:
print("You also met the attendance requirement.")
else:
print("But you did not meet the attendance requirement.")
else:
print("You failed the exam.")
Example: Write a Python program that takes an integer input and do the following:
- Check if the number is positive.
- If it is positive, check whether it is even or odd.
- If it is even, further check whether it is greater than 10 or not.
- Print appropriate messages based on each condition.
num = 15
if num > 0:
print("Positive number")
if num % 2 == 0:
print("Even number")
if num > 10:
print("Greater than 10")
else:
print("10 or less")
else:
print("Odd number")
else:
print("Not a positive number")
Ternary Conditional Statements
A ternary conditional statement in Python is a shorthand way of writing an if-else condition in a single line.
Syntax:
result = Statement1 if condition else Statement 2
Example:
age = 18
status = "Adult" if age >= 18 else "Minor"
print(status) # Output: Adult
Example:
x = 8
result = "x is even" if x % 2 == 0 else "x is odd"
print(result)
Example:
a = 5
b = 10
result = "a is smaller" if a < b else "b is smaller or equal"
print(result)