Logical Operators in Python
Logical operators in Python — and, or, not — explained with clear examples, truth tables, short-circuiting, and common mistakes.
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, andnot - 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 2bug
We are not covering bitwise operators like
&and|here. For normal if-statementsif / elif / else decisions, always useand/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 truex or y→ true when at least one side is truenot x→ true whenxis false
Truth table
For booleans, the outcomes are fixed:
| Expression | Result |
|---|---|
| True and True | True |
| True and False | False |
| False or True | True |
| False or False | False |
| not True | False |
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") # worldCommon mistakes
- Writing
if x == 1 or 2:— this is always truthy; useif x in (1, 2): - Using
&/|instead ofand/orin regular if-conditions - Assuming
andalways returnsTrueorFalse(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.
Programs, Interpreters & Compilers
What a program is, and how interpreters vs compilers run your code.
Comparison Operators in Python
==, !=, <, > — how Python compares values before logic kicks in.
If Statements in Python
Turn boolean results into decisions with if, elif, and else.
Booleans and Truthiness
Why empty strings and 0 behave as false in conditions.
For Loops in Python
Repeat work — and combine loops with and/or filters.
Python course hub
All free Python explainers and the path into Agentic practice.