Getting Started with Python (Part 7): Conditional Statements
Source: Dev.to
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
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.
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.
| 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 greater than b |
>= | a >= b | True if a is greater than or equal to b |
Using Conditional Statements
Using the if Statement
Conditional logic in Python is written using the if keyword. Indentation defines the block of code that runs only when the condition is True.
if condition:
code_that_runs_when_condition_is_true
Example
age = 18
if age >= 18:
print("You are an adult!")
# Output: You are an adult!
If the condition is false, nothing is printed:
age = 17
if age >= 18:
print("You are an adult!")
# Output: (nothing)
Using the else Statement
The else keyword defines 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!
When the condition is false:
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) checks additional conditions if the previous if (or elif) is false.
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 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 chain multiple elif statements; they are evaluated from top to bottom:
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 let you 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 = 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!