Python – Conditional Statements
Conditional statements in Python allow you to execute certain pieces of code based on whether a condition is true or false. Here are the main types of conditional statements in Python:
if Statement
The if
statement is used to execute a block of code if a specified condition is true.
Syntax:
if condition:
# code to execute if condition is true
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
In this example, the message “You are eligible to vote.” is printed because the condition age >= 18
is true.
elif Statement
The elif
(short for “else if”) statement allows you to check multiple conditions, one after another.
Syntax:
if condition1:
# code to execute if condition1 is true
elif condition2:
# code to execute if condition2 is true
elif condition3:
# code to execute if condition3 is true
...
else:
# code to execute if all conditions are false
Example:
score = 75
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
In this example, the output is “Grade: C” because the condition score >= 70
is true.
else Statement
The else
statement is used to execute a block of code if none of the preceding conditions are true.
Syntax:
if condition:
# code to execute if condition is true
else:
# code to execute if condition is false
Example:
is_raining = False
if is_raining:
print("Take an umbrella.")
else:
print("No need for an umbrella.")
In this example, the message “No need for an umbrella.” is printed because the condition is_raining
is false.
Combining if, elif, and else
You can combine if
, elif
, and else
statements to handle multiple conditions in a more complex decision-making structure.
Example:
temperature = 30
if temperature > 35:
print("It's extremely hot outside!")
elif temperature > 25:
print("It's hot outside!")
elif temperature > 15:
print("It's warm outside!")
elif temperature > 5:
print("It's cold outside!")
else:
print("It's freezing outside!")
In this example, the message “It’s hot outside!” is printed because the condition temperature > 25
is true.
Nested if Statements
You can also nest if
statements within other if
, elif
, or else
statements to create more complex conditions.
Example:
age = 20
has_voter_id = True
if age >= 18:
if has_voter_id:
print("You can vote.")
else:
print("You need a voter ID to vote.")
else:
print("You are not eligible to vote.")
In this example, the message “You can vote.” is printed because both conditions age >= 18
and has_voter_id
are true.
Summary
- if: Executes a block of code if its condition is true.
- elif: Checks another condition if the previous conditions were false.
- else: Executes a block of code if all previous conditions were false.
- Nested if: Allows for more complex decision-making by nesting one
if
statement inside another.
These conditional statements allow you to control the flow of your program and make decisions based on different conditions.