Sythra

Logical Operators in Python

Logical operators in Python — and, or, not — explained with clear examples, truth tables, short-circuiting, and common mistakes.

Sythra··8 min read
ShareXLinkedIn
Logical Operators in Python — cover

Want to understand logical operators in Python? This guide covers booleansTrue or False values, andCombine conditions: both must be true, orCombine conditions: either can be true, and notFlip a condition — with examples you can run today.

Technical words are colored — hover for a plain-English tip. Soft green is beginner syntax, warm orange is a bit more technical, cool blue is deeper.

What you will learn

By the end you can:

  • Read and write conditions with and, or, and not
  • Use a simple truth table without guessing
  • Understand short-circuit evaluationPython stops evaluating once the answer is known
  • Avoid the classic if x == 1 or 2 bug

We are not covering bitwise operators like & and | here. For normal if-statementsif / elif / else decisions, always use and / or / not.

Intuition with a real example

Imagine a club door. You enter only if you are 18+ and you have an ID. A sale applies if you are a student or a senior. A tip banner hides when it is not dismissed. These conditions usually sit next to comparison operators and if-statements in real programs.

  • Door opens only if key works and alarm is off → and
  • Alarm triggers if window or door is open → or
  • Show tip when not dismissed → not

The three operators

Keep this mental model nearby until it feels automatic:

  • x and y → true only when both sides are true
  • x or y → true when at least one side is true
  • not x → true when x is false

Truth table

For booleans, the outcomes are fixed:

ExpressionResult
True and TrueTrue
True and FalseFalse
False or TrueTrue
False or FalseFalse
not TrueFalse

Short-circuiting

Python is lazy in a useful way. With and, if the left side is already false, the right side never runs. With or, if the left side is already true, the right side never runs.

That matters when the right side is expensive or unsafe — for example, checking that a value exists before reading a key.

user = {"name": "Ada"}

# Right side runs only if user is truthy
if user and user.get("name"):
    print(user["name"])

Python implementation

A small runnable example you will actually write:

age = 20
has_id = True
is_banned = False

can_enter = age >= 18 and has_id and not is_banned
print(can_enter)  # True

# Truthiness: and/or return an operand
print("" or "fallback")    # fallback
print("hello" and "world")  # world

Common mistakes

  • Writing if x == 1 or 2: — this is always truthy; use if x in (1, 2):
  • Using & / | instead of and / or in regular if-conditions
  • Assuming and always returns True or False (it may return an operand because of truthinessNon-empty values count as true; 0, '', None count as false)
  • Skipping parentheses in a and b or c — group intentionally

Common questions

What are the logical operators in Python?

Python has three logical operators: and, or, and not. They combine or invert boolean conditions in expressions and if-statements.

What is short-circuit evaluation?

Python stops evaluating a logical expression as soon as the result is decided. For and, if the left side is false, the right side is skipped. For or, if the left side is true, the right side is skipped.

Can I use and/or with numbers and strings?

Yes. Python uses truthiness: 0, empty strings, empty lists, and None are falsy. and/or return one of the operands, not always True/False.

What's the difference between & and and?

and is the logical operator for conditions. & is a bitwise operator (and also used with some libraries like pandas). For if-conditions, use and.

Explore

Related topics

Keep going — these sit next to this concept in a real learning path.

Browse all python explainers →