In Python, if tests (also known as conditional statements) allow you to make decisions in your code. They help the program decide which action to take based on whether a condition is true or false.
The basic format of an if test is:
if condition:
# action if the condition is true
Components of an If Statement:
- Condition: A test (like checking if a number is greater than 10).
- Action: The code that runs if the condition is true.
Step-by-Step Explanation
1. Simple If Test
Let’s start with a simple example:
age = 18
if age >= 18:
print("You are an adult.")
Explanation:
age = 18
: We are checking if the person is 18 years or older.if age >= 18
: This checks whether theage
is greater than or equal to 18.print("You are an adult.")
: If the condition is true (the person is 18 or older), it will print “You are an adult.”
Sample Output:
You are an adult.
2. Else Statement: Running Code if Condition is False
An else
statement is used to run code when the condition is false. Here’s how you can use it:
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are not an adult yet.")
Explanation:
- If the person is not 18 or older (i.e., if the condition is false), the code under
else
will run.
Sample Output:
You are not an adult yet.
3. Elif Statement: Checking Multiple Conditions
Sometimes, you might want to check more than two conditions. You can use elif
(which stands for “else if”) to check additional conditions:
age = 20
if age >= 65:
print("You are a senior citizen.")
elif age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Explanation:
- The program checks if the person is a senior citizen (65+).
- If the person is not a senior, it checks if they are an adult (18+).
- If neither condition is true, it prints that the person is a minor.
Sample Output:
You are an adult.
4. Nested If Statements
You can also put one if
statement inside another to check more complex conditions. This is called a nested if statement.
age = 25
income = 5000
if age >= 18:
if income >= 3000:
print("You are an adult and have a good income.")
else:
print("You are an adult, but your income is low.")
else:
print("You are a minor.")
Explanation:
- First, the program checks if the person is 18 or older.
- Then, inside that check, it checks if the person’s income is greater than or equal to 3000.
Sample Output:
You are an adult and have a good income.
5. Using Logical Operators (and, or, not)
Sometimes, you want to check multiple conditions at once. This is where logical operators like and
, or
, and not
come into play.
and
: Returns True if both conditions are true.or
: Returns True if at least one condition is true.not
: Reverses the result of the condition (True becomes False, and False becomes True).
Here’s an example using and
and or
:
age = 20
has_permission = True
if age >= 18 and has_permission:
print("You are allowed to enter.")
else:
print("You are not allowed to enter.")
Explanation:
- The person must be at least 18 years old and have permission.
- The code will only print “You are allowed to enter” if both conditions are true.
Sample Output:
You are allowed to enter.
Now, let’s use or
:
age = 16
has_permission = True
if age >= 18 or has_permission:
print("You are allowed to enter.")
else:
print("You are not allowed to enter.")
Explanation:
- The person can enter if they are 18 or older, or if they have permission.
Sample Output:
You are allowed to enter.
6. Using the not
Operator
You can use not
to reverse a condition:
age = 15
if not age >= 18:
print("You are not an adult yet.")
else:
print("You are an adult.")
Explanation:
- This checks if
age
is not greater than or equal to 18 and prints the corresponding message.
Sample Output:
You are not an adult yet.