if, elif, and else let your program choose different paths based on conditions.
Python decides which block to run by testing each expression in order.
score = 87
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "D"
print(f"Score {score} earns grade {grade}")- Only one branch runs.
- Blocks are defined by indentation (four spaces is standard).
A shorter “one-liner” form is called a conditional expression:
is_premium_member = True
discount_rate = 0.20 if is_premium_member else 0.05
print(f"Discount: {discount_rate * 100}%")The expression before if is the result when the condition is True; after else is the result when it is False.
-
Create a file called
Task_11.py. -
Ask the user for the outside temperature with
input("Temperature in °C: "), convert it toint, and store it intemp. -
Write an
if-elif-elsechain that prints:"Hot day"iftempis 30 or above"Warm day"iftempis between 20 and 29"Cool day"otherwise
-
Ask the user for their age, convert it to
int, and use a conditional expression to setaccess_msgto"You can vote"or"You cannot vote yet". Printaccess_msg. -
Run the script with several input combinations to see each branch in action.
Good indentation and clear conditions make your code easy to read and maintain.