if, elif, and else: Making Decisions in Python
In programming, we often need our code to make decisions and take different actions based on certain conditions. This is the core of what makes software interactive and intelligent. Python’s conditional statements—if, elif (short for “else if”), and else—are the tools you’ll use to build this logic.
Think of it like giving your program a brain. Instead of just following a straight path, it can now evaluate a situation and decide, “If this is true, I’ll do this; otherwise, I’ll do that.”
The if Statement: The Basic Decision
The if statement is the simplest form of a conditional. It checks if a condition is True, and if it is, it executes a block of code. If the condition is False, the code block is simply skipped.
Basic Syntax
if condition:
# This code block executes only if the condition is True
# It must be indentedcondition: An expression that evaluates to eitherTrueorFalse. This is often a comparison (e.g.,age > 18,name == "Alice").:: The colon is mandatory and signifies the start of the code block.- Indented Block: The code that belongs to the
ifstatement. Python uses indentation (typically 4 spaces) to define blocks.
Example: Checking the Weather
Pyground
Write a program that prints 'Remember to take an umbrella!' only if the weather is 'rainy'.
Expected Output:
Remember to take an umbrella! Have a nice day!
Output:
The if-else Statement: The Two-Way Path
What if you want to do something else when the condition is false? The if-else statement provides an alternative path.
Basic Syntax
if condition:
# Executes if the condition is True
else:
# Executes if the condition is FalseExample: Checking if a Number is Even or Odd
This is a classic example that perfectly illustrates the if-else structure. A number can only be even or odd; there’s no other possibility.
Pyground
Write a program that checks if the number 10 is even or odd.
Expected Output:
10 is an even number.
Output:
The if-elif-else Chain: Handling Multiple Conditions
Sometimes you have more than two possibilities. The if-elif-else chain lets you check multiple conditions in sequence. Python checks them one by one until it finds one that is True. Once it finds a true condition, it executes that block and skips the rest of the chain.
Basic Syntax
if first_condition:
# Executes if first_condition is True
elif second_condition:
# Executes if first_condition is False and second_condition is True
elif third_condition:
# And so on...
else:
# Executes if none of the above conditions are TrueExample: Grading a Student’s Score
Pyground
Assign a grade based on a score of 85. A (>=90), B (>=80), C (>=70), D (>=60), F (<60).
Expected Output:
A score of 85 gets a grade of B.
Output:
The order of elif statements matters! Python stops at the first True condition. If we had checked for score >= 70 before score >= 80, a score of 85 would have incorrectly received a “C”.
Nested if Statements
You can place if statements inside other if statements. This is called nesting. It’s useful for checking a secondary condition after a primary condition has already been met.
Example: Movie Ticket Eligibility
To watch a certain movie, you must be at least 18 years old AND have a ticket.
Pyground
Check if a person aged 20 with a ticket can enter the movie.
Expected Output:
Age requirement met. Ticket confirmed. You may enter.
Output:
Nesting is useful, but sometimes you can simplify your code using logical operators like and. The above example could also be written as:
if age >= 18 and has_ticket:
Practice Problems
Problem 1: Positive, Negative, or Zero?
Write a program to check if a number is positive, negative, or exactly zero.
Pyground
Check if the number -5 is positive, negative, or zero.
Expected Output:
The number is negative.
Output:
Problem 2: Simple Login System
Create a simple login check. If the username is “admin” and the password is “12345”, print “Access granted”. Otherwise, print “Access denied”.
Pyground
Check if a user with username 'admin' and password '12345' can log in.
Expected Output:
Access granted