Python's `and` Operator: Requiring Multiple Conditions to be True
Real decisions are rarely based on a single fact. A user can log in only if their username and password are correct. A discount applies only if the cart total is above $50 and the user is a member. Python's and operator lets you express exactly these kinds of multi-condition rules — cleanly and intuitively.
How `and` Works: The Truth Table
and returns True only when both sides are True. If either side is False, the whole expression is False:
print(True and True) # True — both true
print(True and False) # False — right side is false
print(False and True) # False — left side is false
print(False and False) # False — both false
Think of it as a strict requirement: every condition must pass.
Using `and` in if Statements
age = 25
has_license = True
if age >= 18 and has_license:
print("You can drive.")
else:
print("You cannot drive.")
# You can drive.
# Change one condition:
has_license = False
if age >= 18 and has_license:
print("You can drive.")
else:
print("You cannot drive.")
# You cannot drive. ← one False makes the whole thing False
Real-World Examples
# Login validation — both must match:
username = "alex"
password = "secure123"
if username == "alex" and password == "secure123":
print("Access granted.")
else:
print("Access denied.")
# Discount eligibility — two conditions required:
cart_total = 75
is_member = True
if cart_total >= 50 and is_member:
discount = cart_total * 0.10
print(f"10% discount applied: ${discount:.2f} off")
else:
print("No discount available.")
# Age range check:
age = 20
if age >= 18 and age <= 25:
print("Young adult pricing applies.")
Chaining Multiple `and` Conditions
You can chain as many and conditions as you need. All of them must be True for the block to run:
def can_graduate(gpa, credits_earned, fees_paid):
if gpa >= 2.0 and credits_earned >= 120 and fees_paid:
return "Eligible to graduate!"
return "Requirements not met."
print(can_graduate(3.5, 130, True)) # Eligible to graduate!
print(can_graduate(3.5, 130, False)) # Requirements not met.
print(can_graduate(1.8, 130, True)) # Requirements not met.
Short-Circuit Evaluation: Python Stops Early
Python evaluates and expressions left to right and stops as soon as it finds a False. This is called short-circuit evaluation and it matters for performance and safety:
my_list = []
# Safe: if list is empty, the second condition never runs (no IndexError):
if len(my_list) > 0 and my_list[0] == 42:
print("First item is 42")
else:
print("List is empty or first item isn't 42")
Put the cheapest or most likely to fail condition first — Python won't bother evaluating the rest.
Common Mistakes to Avoid
- Confusing
andwithor:andrequires ALL conditions;orneeds just ONE. - Writing
if x == 1 and 2:instead ofif x == 1 and x == 2:— The second form always evaluates2as truthy. Repeat the variable name for each comparison.
x = 1
# BUG — "and 2" is always True (non-zero integer):
if x == 1 and 2:
print("This runs — but probably not what you meant")
# CORRECT:
if x == 1 and x == 2:
print("This never runs because x can't be both 1 and 2")
# Probably intended:
if x == 1 or x == 2:
print("x is 1 or 2") # This runs
Key Takeaways
andreturnsTrueonly when both sides areTrue.- One
Falseanywhere in anandchain makes the whole expressionFalse. - Python short-circuits — it stops evaluating as soon as it finds a
False. - Use
andwhen a condition must satisfy multiple independent requirements simultaneously.
What to Learn Next
Now that you know and, explore the or operator (True if at least one condition is True) and the not operator (inverts a boolean). Together, these three logical operators let you express any decision your program needs to make.
