Python Logical Operators: and, or, not
Updated 6 min read Aimerscrew
Real decisions rarely depend on just one thing. You can board a flight if you have a ticket and a passport. You get free dessert if it's your birthday or you're a loyalty member. Python logical operators, and, or and not, let your code combine conditions like these. In this lesson, you'll learn their truth tables, how Python takes shortcuts when checking them, and a surprising trick: they don't always return True or False.
🌍 Real-life example
A coffee shop runs a deal: "Get 20% off if you're a student or it's before 9 a.m." A second rule says: "Free refill only if you bought a large and you kept your cup." Most rules in daily life are built from and, or and not, and Python uses the very same words.
What are logical operators in Python?
Logical operators combine or flip boolean conditions. You had a short preview in Python booleans; now let's look at them properly.
| Operator | Meaning | Example | Result |
|---|---|---|---|
and | True only if both sides are true | True and False | False |
or | True if at least one side is true | True or False | True |
not | Flips the value | not True | False |
Unlike many languages that use &&, || and !, Python spells these operators as plain English words.
Truth tables for and, or and not
A truth table lists every possible combination of inputs and the result. With two sides that can each be True or False, there are only four combinations, so these tables are easy to learn:
and needs everything true, or needs just one thing true, and not flips the value.print(True and True)
print(True and False)
print(False or True)
print(False or False)
print(not False)
True
False
True
False
True
How does the and operator work?
Use and when every condition must be met. Here's a simple login check: the username must match and the password must match.
saved_user = "maya"
saved_password = "sunset42"
typed_user = "maya"
typed_password = "sunset24"
can_log_in = typed_user == saved_user and typed_password == saved_password
print(can_log_in)
False
The username is right, but the password isn't, so the whole and is False. Notice that you don't need brackets around each comparison: comparison operators run before and. (More on this in operator precedence.)
How does the or operator work?
Use or when any one condition is enough. A store gives a discount to students, seniors (65+), or anyone spending over $100:
is_student = False
age = 70
order_total = 45
gets_discount = is_student or age >= 65 or order_total > 100
print(gets_discount)
True
Only the middle condition is true, and for or, one is all it takes.
How does the not operator work?
not works on a single value and flips it. It's useful when your variable describes the opposite of what you want to check:
is_sold_out = False
print("Can buy:", not is_sold_out)
is_weekend = True
alarm_on = not is_weekend
print("Alarm on:", alarm_on)
Can buy: True
Alarm on: False
Combining and, or and not
You can mix all three in one condition. Python evaluates not first, then and, then or. When you mix and with or, add parentheses so the meaning is obvious to anyone reading:
has_ticket = True
has_passport = True
is_banned = False
can_board = has_ticket and has_passport and not is_banned
print(can_board)
is_member = False
coupon_code = "SAVE10"
is_holiday_sale = True
gets_deal = (is_member or coupon_code == "SAVE10") and is_holiday_sale
print(gets_deal)
True
True
What is short-circuit evaluation?
Python is lazy in a clever way. It checks the left side first and stops as soon as the answer is certain. This is called short-circuit evaluation:
A and B: ifAis false, the result must be false, so Python never looks atB.A or B: ifAis true, the result must be true, so Python never looks atB.
You can prove it: the right side below would crash with a division by zero, but Python never runs it.
guests = 0
print(guests > 0 and 60 / guests > 10)
print(guests == 0 or 60 / guests > 10)
False
True
This is a real, everyday pattern: put the safety check first (guests > 0) so the risky part only runs when it's safe.
💡 Tip
Put the cheapest or most likely-to-decide condition first. With and, put the condition most likely to be false first; with or, the one most likely to be true. Your program does less work.
Do and and or always return True or False?
Surprisingly, no. and and or return one of their operands, the actual value that decided the answer. They use truthy and falsy rules to decide:
orreturns the first truthy value, or the last value if none are truthy.andreturns the first falsy value, or the last value if all are truthy.
print("" or "Guest")
print("Sam" or "Guest")
print(0 and 99)
print(5 and 99)
print(not "hello")
Guest
Sam
0
99
False
Only not always returns a real boolean. The or behavior gives a popular shortcut for default values:
nickname = ""
display_name = nickname or "Anonymous player"
print("Welcome,", display_name)
Welcome, Anonymous player
If the user left their nickname empty (falsy), the default is used instead.
Real-world example: an online store checkout
cart_total = 74.50
is_member = True
in_stock = True
address = "221B Baker Street"
free_shipping = cart_total >= 50 or is_member
can_checkout = in_stock and address != ""
needs_address = not address
print("Free shipping:", free_shipping)
print("Can check out:", can_checkout)
print("Needs address:", needs_address)
Free shipping: True
Can check out: True
Needs address: False
Common mistakes with logical operators
1. Writing x == "a" or "b"
size = "small"
print(size == "large" or "medium")
medium
This looks like "is size large or medium?", but Python reads it as (size == "large") or "medium". The left side is False, so or returns "medium", which is truthy, so this check would always pass. Repeat the comparison: size == "large" or size == "medium".
2. Using &&, || or ! from other languages
print(True && False)
SyntaxError: invalid syntax
Python uses the words and, or and not.
3. Mixing and with or without parentheses
a or b and c means a or (b and c), because and runs first. If you meant (a or b) and c, write the parentheses.
Practice exercise
🏋️ Try it yourself
A theme park ride allows riders who are at least 120 cm tall and not pregnant. Riders under 120 cm may still ride if an adult comes with them. Store height = 112, is_pregnant = False and with_adult = True, then print whether the person can ride.
Show solution
height = 112
is_pregnant = False
with_adult = True
can_ride = (height >= 120 or with_adult) and not is_pregnant
print("Can ride:", can_ride)
Can ride: True
What's next?
You can now combine conditions like a pro. Next, meet two special kinds of operators: is, which checks whether two names point to the same object, and in, which checks whether something is inside a collection, in identity and membership operators.
Key takeaways
- Python has three logical operators written as words: and, or and not.
- and is True only if both sides are true; or is True if at least one side is true; not flips a value.
- Short-circuit evaluation: Python stops as soon as the result is known, so the right side may never run.
- and and or return one of their operands, not always True or False; only not always returns a bool.
- value or "default" is a common shortcut for falling back to a default when a value is empty.
- Precedence is not, then and, then or. Use parentheses when mixing and with or.
Frequently asked questions
What are the logical operators in Python?
Python has three: and, or and not. They are written as English words instead of symbols like && and || used in other languages.
What is short-circuit evaluation in Python?
It means Python stops evaluating a logical expression as soon as the answer is known. In A and B, if A is false, B is never checked. In A or B, if A is true, B is never checked.
Why does "" or "Guest" return "Guest" in Python?
The or operator returns the first truthy operand. An empty string is falsy, so Python moves on and returns "Guest". This is a common way to set a default value.
Which runs first in Python, and or or?
and has higher precedence than or, so a or b and c is evaluated as a or (b and c). not runs before both of them.
Can I use && and || in Python?
No. Writing && or || causes a SyntaxError. Use the keywords and and or. The single & and | symbols exist, but they are bitwise operators with different behavior.