Logical Operators
Learn how and, or, and not let you combine conditions to make smarter decisions in your code.
Imagine you're a bouncer at a club. You don't check just one thing — you check age AND has a ticket, or you let someone in if they're on the VIP list OR they're a member. Code works the same way. Python gives you three logical operators — and, or, and not — to combine conditions and write smarter rules.
See it in action
— step through the idea, then dive into the details below.Combine Conditions Like a Pro
Python's logical operators — and, or, and not — let you check multiple conditions at once. Think of them as the glue that turns simple true/false checks into smart, real-world rules.
#and — Both Must Be True
and returns True only when both sides are true. If either side is false, the whole thing is false. Think of two locked doors — both must be open to get through.
| Left | Right | Result | |------|-------|--------| | True | True | True | | True | False | False | | False | True | False | | False | False | False |
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("Welcome in!")
else:
print("Sorry, you can't enter.")#or and not
or returns True if at least one side is true — like having two keys and needing only one:
| Left | Right | Result | |------|-------|--------| | True | True | True | | True | False | True | | False | True | True | | False | False | False |
not simply flips a value: not True → False, not False → True.
is_member = False
is_vip = True
is_banned = False
if (is_member or is_vip) and not is_banned:
print("Access granted!")
else:
print("No access.")#Short-Circuit Evaluation
Python is smart about not doing extra work. With and, if the first part is False, Python skips the second — the result is already False. With or, if the first part is True, Python skips the second — the result is already True. This is called short-circuit evaluation.
def slow_check():
print("slow_check ran!")
return True
# False and ... — Python never calls slow_check
print(False and slow_check())
# True or ... — Python never calls slow_check
print(True or slow_check())and/or return the actual operand, not just True/False
and returns the first falsy value it hits, or the last value if all are truthy. or returns the first truthy value it hits, or the last if all are falsy. This enables a handy default pattern:
``python name = user_input or "Guest" ``
If user_input is an empty string (falsy), name becomes "Guest". Elegant!
Don't shortcut or like a list
A very common beginner mistake:
``python if color == "red" or "blue": # WRONG! ``
This does NOT check if color is "blue". It evaluates color == "red" OR the string "blue" — and since "blue" is always truthy, the whole condition is always True, no matter what color is.
Always write the full comparison on both sides:
``python if color == "red" or color == "blue": # Correct ``
age = 15
has_parent_note = True
is_registered = False
can_join = (age >= 18 or has_parent_note) and not is_registered
print(can_join)What does the following expression evaluate to? False or (True and not False)
Key takeaways
- `and` requires both conditions to be True; `or` needs just one; `not` flips a value.
- Short-circuit evaluation means Python stops early when the result is already certain — great for performance.
- `and` and `or` return one of their actual operands, enabling patterns like `value or 'default'`.
- Always write full comparisons on both sides of `or` — never `x == 1 or 2`.
- Use parentheses to make complex conditions readable and unambiguous.
What does this code print?
age = 15
has_parent_note = True
is_registered = False
can_join = (age >= 18 or has_parent_note) and not is_registered
print(can_join)This code is supposed to print 'Yes' when color is 'red' or 'blue', but it always prints 'Yes' no matter what color is. What is wrong?
color = "green"
if color == "red" or "blue":
print("Yes")
else:
print("No")Complete the code so it prints True when the user is at least 18 years old AND has a ticket.
age = 20 has_ticket = True result = age >= 18 has_ticket print(result)
Put these lines in the right order to check if someone can enter: they must be a member OR a VIP, and must NOT be banned.
is_member = False
print("Access granted!")if (is_member or is_vip) and not is_banned:
is_banned = False
is_vip = True
Write a function called can_rent_car(age, has_license, is_blacklisted) that returns True only if the person is at least 21 years old, has a license, and is NOT blacklisted. Test it with a few different inputs and print the results.
Try it live — edit the code and hit Run to execute real Python: