In the previous section, you learned how to compare values using operators like ==
(equal to), !=
(not equal to), <
(less than), and >
(greater than). These comparisons always result in a boolean value: either True
or False
.
Often, you need to check more than one condition at a time. For instance, you might want to know if a user is both logged in and has administrator privileges, or if it's either Saturday or Sunday. This is where logical operators come into play. They allow you to combine boolean values (True
or False
) to create more complex conditions. Python provides three logical operators: and
, or
, and not
.
and
OperatorThe and
operator evaluates two boolean expressions. It returns True
only if both expressions are True
. If either expression (or both) is False
, the result is False
.
Think of it like needing two forms of identification to enter a secure area. You need ID card A and ID card B. Having only one isn't enough.
Let's look at an example:
age = 25
has_ticket = True
# Is the person old enough AND has a ticket?
can_enter = (age >= 18) and (has_ticket == True)
print(f"Age is 25, has ticket: {can_enter}") # Output: True
age = 16
# Now the first condition (age >= 18) is False
can_enter = (age >= 18) and (has_ticket == True)
print(f"Age is 16, has ticket: {can_enter}") # Output: False
age = 30
has_ticket = False
# Now the second condition (has_ticket == True) is False
can_enter = (age >= 18) and (has_ticket == True)
print(f"Age is 30, no ticket: {can_enter}") # Output: False
The parentheses ()
around the comparisons (age >= 18
) are not strictly required here because and
has lower precedence than comparison operators, but they often make the code easier to read by clearly grouping the conditions.
Here's a summary of how and
works:
Condition 1 | Condition 2 | Condition 1 and Condition 2 |
---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
or
OperatorThe or
operator also evaluates two boolean expressions. It returns True
if at least one of the expressions is True
. It only returns False
if both expressions are False
.
Imagine getting a discount if you are either a student or a senior citizen. Meeting just one of the conditions is sufficient.
Consider this example:
is_weekend = False
is_holiday = True
# Is it the weekend OR a holiday?
day_off = is_weekend or is_holiday
print(f"Weekend: False, Holiday: True -> Day off: {day_off}") # Output: True
is_weekend = True
is_holiday = False
# The first condition is True
day_off = is_weekend or is_holiday
print(f"Weekend: True, Holiday: False -> Day off: {day_off}") # Output: True
is_weekend = False
is_holiday = False
# Both conditions are False
day_off = is_weekend or is_holiday
print(f"Weekend: False, Holiday: False -> Day off: {day_off}") # Output: False
Here's the summary for or
:
Condition 1 | Condition 2 | Condition 1 or Condition 2 |
---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
not
OperatorThe not
operator is simpler. It works on a single boolean expression and reverses its value. If the expression is True
, not
makes it False
. If the expression is False
, not
makes it True
.
Think of a light switch. not on
means off
, and not off
means on
.
is_logged_in = False
print(f"Is logged in: {is_logged_in}") # Output: False
# Check if the user is NOT logged in
needs_login = not is_logged_in
print(f"Needs login: {needs_login}") # Output: True
is_raining = True
print(f"Is raining: {is_raining}") # Output: True
# Check if it is NOT raining
can_go_outside = not is_raining
print(f"Can go outside: {can_go_outside}") # Output: False
Summary for not
:
Condition | not Condition |
---|---|
True |
False |
False |
True |
You can combine multiple logical operators in a single expression. Python evaluates not
first, then and
, then or
. However, just like in arithmetic, you can use parentheses ()
to control the order of evaluation or simply to make your intentions clearer.
age = 22
is_student = True
has_coupon = False
# Eligible if (age < 25 AND is a student) OR has a coupon
is_eligible_discount = (age < 25 and is_student) or has_coupon
# Evaluates as: (True and True) or False
# -> True or False
# -> True
print(f"Eligible for discount: {is_eligible_discount}") # Output: True
# Change coupon status
has_coupon = True
age = 30
is_student = False
is_eligible_discount = (age < 25 and is_student) or has_coupon
# Evaluates as: (False and False) or True
# -> False or True
# -> True
print(f"Eligible for discount (different criteria): {is_eligible_discount}") # Output: True
Logical operators are fundamental tools for building programs that can make decisions based on multiple criteria. You will see them used extensively when we discuss control flow structures like if
statements in the next chapter, allowing your programs to react differently based on whether complex conditions evaluate to True
or False
.
© 2025 ApX Machine Learning