OperatorsBeginner8 min15 / 63

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 operatorsand, or, and not — to combine conditions and write smarter rules.

See it in action

Visual walkthrough1 / 6
1

Combine Conditions Like a Pro

Python's logical operatorsand, 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.

These three keywords are the backbone of almost every decision your code will ever make.

#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 |

Both conditions must be True for the if-block to run.
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 TrueFalse, not FalseTrue.

is_vip makes the or True; not is_banned stays True — so entry is allowed.
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.

slow_check() is never called in either line — Python short-circuits both times.
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())
Tip

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!

Common mistake

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 ``

A realistic access check combining and, or, and not in one clean expression.
age = 15
has_parent_note = True
is_registered = False

can_join = (age >= 18 or has_parent_note) and not is_registered
print(can_join)
Quick check

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.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
age = 15
has_parent_note = True
is_registered = False

can_join = (age >= 18 or has_parent_note) and not is_registered
print(can_join)
Fix the bug#2

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?

fix-bug
color = "green"

if color == "red" or "blue":
    print("Yes")
else:
    print("No")
Fill in the blank#3

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)
Reorder the lines#4

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.

1
is_member = False
2
    print("Access granted!")
3
if (is_member or is_vip) and not is_banned:
4
is_banned = False
5
is_vip = True
Your turn
Practice exercise

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:

solution.py · editable