Using Conditional Statements in Python
In this article, we’ll learn about conditional statements.
By using conditional logic, you can control the flow of your program and implement more complex behavior.
Boolean Values and Comparison Operators
Before diving into conditional statements, let’s first look at boolean values and comparison operators.
What Are Boolean Values?
A boolean value is a data type that represents only two possible states: true or false.
- True means something is correct or valid.
- False means something is incorrect or invalid.
You can think of it like a true/false quiz:
⭕ correct → True, ❌ incorrect → False.
In conditional statements, these boolean values are used to make decisions.
What Are Comparison Operators?
Comparison operators are symbols used to compare two values.
The result of a comparison is always a boolean value: True or False.
Conditional statements use these results to decide whether a block of code should run.
Here are the most common comparison operators:
| Operator | Example | Meaning |
|---|---|---|
| == | a == b | True if a and b are equal |
| != | a != b | True if a and b are not equal |
| < | a < b | True if a is less than b |
| <= | a <= b | True if a is less than or equal to b |
| > | a > b | True if a is greater than b |
| >= | a >= b | True if a is greater than or equal to b |
Using Conditional Statements
Now let’s look at how to write conditional statements in Python.
Using the if Statement
Conditional logic in Python is written using the if keyword.
Indentation is very important—it defines the block of code that runs only when the condition is True.
if condition:
code_that_runs_when_condition_is_true
Here’s a simple example using a comparison operator.
age = 18
if age >= 18:
print("You are an adult!")
# Output: You are an adult!
If we change age to 17, the condition becomes False, and nothing is printed.
age = 17
if age >= 18:
print("You are an adult!")
# Output: nothing
Using the else Statement
The else keyword lets you define what happens when the condition is False.
age = 18
if age >= 18:
print("You are an adult!")
else:
print("You are a minor!")
# Output: You are an adult!
If we change age to 17, the else block runs instead.
age = 17
if age >= 18:
print("You are an adult!")
else:
print("You are a minor!")
# Output: You are a minor!
Using the elif Statement
The elif keyword (short for else if) is used when you want to check multiple conditions.
If the first if condition is False, Python checks the elif condition next.
age = 17
if age >= 18:
print("You are an adult!")
elif age >= 15:
print("You are a teenager!")
else:
print("You are a child!")
# Output: You are a teenager!
If all conditions are False, the final else block runs.
age = 14
if age >= 18:
print("You are an adult!")
elif age >= 15:
print("You are a teenager!")
else:
print("You are a child!")
# Output: You are a child!
You can use as many elif statements as you like.
Just remember that conditions are checked from top to bottom, so the order matters.
age = 14
if age >= 18:
print("You are an adult!")
elif age >= 15:
print("You are a teenager!")
elif age >= 6:
print("You are a child!")
else:
print("You are a toddler!")
# Output: You are a child!
Logical Operators
Logical operators allow you to combine multiple conditions into a single decision.
| Operator | Example | Meaning |
|---|---|---|
| not | not a | Reverses the condition |
| or | a or b | True if either condition is True |
| and | a and b | True only if both conditions are True |
Using not
age = 18
if not age >= 18:
print("You are a minor!")
Using or
age = 25
if age < 18 or age >= 65:
print("You are a minor or a senior! (Discount applied!)")
Using and
age = 25
if age >= 18 and age <= 65:
print("You are an adult! (No discount available!)")
What’s Next?
Thank you for reading!
In the next article, we’ll cover “Working with Multiple Data Types (Part 1)”.
Stay tuned!
Top comments (0)