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 will 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 will do this; otherwise, I will do that!”
The if Statement: The Basic Decision
The if statement is the simplest form of decision making. It checks if a condition is True. If it is, Python executes the indented block of code. If the condition is False, the code block is simply skipped.
Syntax Layout
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 (for example: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
Let us see a live example checking if we need an umbrella:
Example
Output:
The if-else Statement: The Two-Way Path
What if you want to perform a fallback action when your condition is false? The if-else statement provides an alternative path.
Syntax Layout
if condition:
# Executes if the condition is True
else:
# Executes if the condition is FalseExample: Checking if a Number is Even or Odd
A number can only be even or odd; there is no other possibility. Let us run the check below:
Example
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 specific block and skips the rest of the chain completely.
Syntax Layout
if first_condition:
# Executes if first_condition is True
elif second_condition:
# Executes if first_condition is False and second_condition is True
else:
# Executes if none of the above conditions are TrueExample: Grading a Student’s Score
Let us assign a letter grade based on a numeric score:
Example
Output:
The order of elif statements is incredibly important! Python stops at the first True condition it encounters. 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 is perfect for checking a secondary condition after a primary condition has already been satisfied.
Example: Movie Ticket Eligibility
To watch a premium movie, you must be at least 18 years old AND hold a valid ticket:
Example
Output:
Combining Conditions: You can often simplify nested code by using logical operators like and. The nested conditions above can be rewritten 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:
PygroundTry It Out
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":
PygroundTry It Out
Verify credentials for username 'admin' and password '12345'.
Expected Output:
Access granted