Python Conditional Statements Explained: A Beginner Guide to If, Elif, and Else
In programming, we often need our code to perform different actions based on different conditions. This is known as conditional logic, and it’s a fundamental concept in Python. The tools for this are the if, elif, and else statements.
This guide will explain how to use them to control the flow of your programs.
The if Statement: The Basic Condition
The if statement is the most basic conditional statement. It checks if a condition is true. If the condition is true, a specific block of code is executed. If the condition is false, the code block is ignored.
Think of it as a simple question: “Is this condition met? If yes, then perform this action.”
Here is a basic example:
is_raining = True
if is_raining:
print("Remember to take an umbrella.")
In this code, the variable is_raining is True. The if statement checks this, and since the condition is true, it runs the print() function. If is_raining were False, the program would simply skip the print() statement.
The else Statement: Handling the Alternative
What if you want your program to do something when the if condition is not met? For this, you can use the else statement.
The else statement provides an alternative block of code that runs only when the if condition is false.
Let’s continue with our weather example:
is_raining = False
if is_raining:
print("Remember to take an umbrella.")
else:
print("The weather is clear.")
Here, is_raining is False. The if condition is not met, so the first print() statement is skipped. The program then moves to the else block and executes its code, printing “The weather is clear.”
An if/else structure guarantees that one of the two blocks of code will always run.
The elif Statement: For Multiple Conditions
Sometimes, you need to check more than two possibilities. This is where elif (short for “else if”) is useful.
The elif statement allows you to check for additional conditions if the previous if and elif conditions were false. You can use as many elif statements as you need.
Imagine a program that checks a person’s age to determine their life stage:
age = 25
if age < 13:
print("You are a child.")
elif age < 20:
print("You are a teenager.")
elif age < 65:
print("You are an adult.")
else:
print("You are a senior.")
Here’s how Python processes this:
Is age less than 13? No.
Is age less than 20? No.
Is age less than 65? Yes. It prints "You are an adult."
Once a true condition is found (the age < 65 check), its code block is executed, and the rest of the else statement is skipped entirely.
A Practical Example: User Input
Let’s see how these statements work together to respond to user input.
choice = input("Enter 'a' or 'b': ")
if choice == 'a':
print("You chose option A.")
elif choice == 'b':
print("You chose option B.")
else:
print("Invalid selection.")
This program asks the user for input and provides a different response for each valid option. The else block acts as a catch-all for any input that is not ‘a’ or ‘b’.
Key Points to Remember
-
if: Starts the check. Runs if its condition is true. -
elif: Checks another condition if the previous if or elif was false. -
else: Runs if none of the preceding if or elif conditions were true.
Important Note on Indentation: Python uses indentation (the spaces at the beginning of a line) to define which code belongs to which statement. Incorrect indentation will cause an error.
# Correct Indentation
if True:
print("This line is inside the if block.")
# Incorrect Indentation
# if True:
# print("This will cause an error.")
Here’s a fun challenge to test your skills!
Challenge: The Magical Vending Machine
You’ve discovered an ancient, magical vending machine. It doesn’t accept money, only a glowing keypad to type a single “word of power.”
Your goal is to write a Python program that simulates this machine.
The Rules
-
Ask the user to enter a magic word.
-
Based on their input, the machine will dispense a different item:
-
If the user types “abracadabra”, your program should print: You receive a sparkling diamond! 💎
-
If the user types “hocus pocus”, your program should print: A talking frog hops out! 🐸
-
If the user types “shazam”, your program should print: A lightning bolt of pure energy crackles in your hand! ⚡️
-
For any other word, your program should print: The machine hums for a moment… then nothing happens.
Hint: You’ll need to use input() to get the user’s word and then an if/elif/else structure to check what they typed.