Logical Operators
Concepts covered: Boolean Operators
Real-world decisions are rarely simple. "Can this user access the file?" might depend on multiple factors: Are they logged in? Do they have permission? Is the file not locked? Logical operators let you combine multiple conditions into one decision.
The Three Logical Operators
Python has three logical operators. Think of them as ways to connect yes-or-no questions:
- and: Both conditions must be True for the result to be True
- or: At least one condition must be True for the result to be True
- not: Flips True to False, and False to True
The "and" Operator
The and operator requires BOTH conditions to be True. If either one is False, the whole expression is False. Think of it as a strict rule: everything must pass.
Imagine a nightclub that requires guests to be 21+ AND have valid ID. Both conditions must be met:
age = 25
has_id = True
can_enter = age >= 21 and has_id
print(can_enter)Output:
True
can_enter is True because BOTH conditions are True.
print(True and True)
print(True and False)
print(False and True)
print(False and False)Output:
True False False False
Only the first is True. All others are False because at least one side is False.
The "or" Operator
The or operator requires AT LEAST ONE condition to be True. It's less strict than "and". Think of it as: if any option works, we're good.
Imagine a building where you can enter if you're an employee OR a registered visitor:
is_employee = False
is_registered_visitor = True
can_enter = is_employee or is_registered_visitor
print(can_enter)Output:
True
can_enter is True because at least one condition is True.
print(True or True)
print(True or False)
print(False or True)
print(False or False)Output:
True True True False
Only the last is False. All others are True because at least one side is True.
The "not" Operator
The not operator flips a boolean value. True becomes False, and False becomes True. It's like saying "the opposite of..."
If a user is blocked, you might want to check if they're NOT blocked:
is_blocked = False
can_post = not is_blocked
print(can_post)Output:
True
Since is_blocked is False, "not False" becomes True.
age = 15
is_adult = age >= 18
is_minor = not is_adult
print(is_adult)
print(is_minor)Output:
False True
is_adult is False (15 is not >= 18), so is_minor is True.
Combining Operators
You can combine multiple logical operators to create complex conditions. But be careful about the order Python evaluates them:
- not: Evaluated first. Flips a single boolean value before anything else.
- and: Evaluated second. Combines two conditions with a strict "both must pass" rule.
- or: Evaluated last. The most lenient check: only one side needs to be True.
This order can lead to unexpected results:
a = True
b = False
c = True
a or b and cBecause and has higher priority, this evaluates as a or (b and c), not (a or b) and c.
Use parentheses to make your intent clear:
(a or b) and c
a or (b and c)These give different results! Parentheses make your intention explicit.
A Real-World Example
Let's say a user can access a premium feature if they're a paying subscriber AND not banned, OR if they're an administrator:
is_subscriber = True
is_banned = False
is_admin = False
can_access = (is_subscriber and not is_banned) or is_admin
print(can_access)Output:
True
The user can access the feature because they're a paying subscriber who isn't banned.
Logical operators form the backbone of every decision your program makes. Whether you are controlling access, validating input, or branching logic, you will reach for and, or, and not constantly.
Using parentheses when combining operators is always the right call. They cost nothing in performance and make the intent of complex conditions immediately clear.
About this lesson
This section is part of the Python Expressions: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.