Conditional Statements (if, elif, else)
On this page
Theif StatementThe else StatementThe elif StatementCombining Conditions with Logical OperatorsThe and OperatorThe or OperatorThe not OperatorNested if ConditionsThe pass StatementMini Project — Movie Ticket Price CalculatorIn everyday life, decisions drive our actions: If it rains, take an umbrella; if it’s sunny, wear sunglasses. In programming, conditional statements let Python do the same — execute different code depending on whether a condition is True or False. This gives your programs logic and adaptability.
The if Statement
The if statement tests a condition. If it is True, Python executes the indented block beneath it. Parentheses around the condition are optional, but indentation is required.
temperature = 30
if temperature > 25:
print("It’s a hot day!")Output
It’s a hot day!
Here, the program checks if temperature > 25. Since the condition is true, the print() statement runs. If the condition were false, Python would skip the block entirely.
The else Statement
The else clause adds an alternative — it runs when the condition in if is false.
temperature = 18
if temperature > 25:
print("It’s a hot day!")
else:
print("It’s a cool day.")Output
It’s a cool day.
Now Python checks the first condition; since it’s false, it executes the else block instead.
The elif Statement
When multiple conditions are possible,elif (short for else if) helps create a chain of checks. Python evaluates each condition in order, and once one is true, it skips the rest.
temperature = 12
if temperature > 25:
print("It’s hot outside.")
elif temperature > 15:
print("It’s warm outside.")
else:
print("It’s cold outside.")Output
It’s cold outside.
This example has three possibilities, but only one block will run.
Combining Conditions with Logical Operators
In many real situations, decisions depend on more than one condition. For example, you might let someone enter a concert only if they are old enough and have a ticket, or send a reminder if it is late or a deadline is approaching.
Python’s logical operators —and,or, and not — allow you to combine or modify conditions to reflect these real-world decisions.
The and Operator
The and operator connects two or more conditions and returns True only if all conditions are true. If any of them is false, the entire expression becomes false.
This is useful when multiple requirements must be met at once.
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("You may enter the concert.")
else:
print("Access denied.")Output
You may enter the concert.
Here, both conditions are true, so the program allows entry. If the person did not have a ticket, the second condition would fail, and access would be denied.
The or Operator
The or operator returns True if at least one of the conditions is true. It only becomes false when all conditions are false.
This is helpful when any one condition is enough to take action.
rainy = False
cold = True
if rainy or cold:
print("Better take a jacket.")
else:
print("The weather looks fine.")Output
Better take a jacket.
Even though it isn’t raining, it’s cold — and that is enough for the program to print the message.
The not Operator
The not operator reverses a condition. If something is true,not makes it false, and vice versa. This is often used when you want to act only if something is not the case.
logged_in = False
if not logged_in:
print("Please log in to continue.")
else:
print("Welcome back!")Output
Please log in to continue.
not can also be combined with comparisons:
temperature = 12
if not (temperature > 15):
print("It’s cold outside.")Output
It’s cold outside.
This example reads almost like English: “If not temperature greater than 15” — meaning the temperature is 15 or below.
Nested if Conditions
Sometimes one decision depends on another. In such cases, you can place an if statement inside another if. This is called nesting. It helps express multi-level logic, where one condition must be true before another is checked.
age = 25
student = True
if age < 30:
if student:
print("You qualify for the young student discount.")
else:
print("You are young, but not a student.")
else:
print("You do not qualify for the discount.")Output
You qualify for the young student discount.
Here, Python first checks whether the age is under 30. Only if that’s true does it check whether the person is a student. Nested conditions help control complex decisions, but they should be used carefully to keep the code readable.
The pass Statement
Sometimes you may need a conditional structure before you’ve decided what to put inside it. Python does not allow an empty block, so the pass statement acts as a placeholder — telling Python to do nothing for now.
age = 18
if age >= 18:
pass # logic will be added later
else:
print("Underage user.")Without pass, leaving the if block empty would cause an error. It’s often used as a temporary placeholder while developing or when a condition is needed syntactically but not functionally.
Mini Project — Movie Ticket Price Calculator
Write a program that determines the ticket price based on age.
Ask the user for their age and use conditions to decide the price:
- Children under 12 pay 5 euros
- People aged 12–17 pay 7 euros
- Adults (18 and above) pay 10 euros
Example Run:
Enter your age: 10
Ticket price: 5 euros
Enter your age: 15
Ticket price: 7 euros
Enter your age: 30
Ticket price: 10 eurosThis exercise reinforces decision-making logic and prepares you to build interactive programs that respond to user input.