Lesson 05
Logical Operators
Combine relational tests with and, or, and not. Learn truth tables, short-circuit evaluation, and why 100 and 200 returns 200.
Logical operators combine two or more relational expressions into a logical expression (compound test). When the operands are booleans, the result is True or False. With non-boolean values, and and or can return one of the original values.
What you'll learn
- Truth tables for
and,or, andnot - Short-circuit evaluation
- Why
not 10isFalsebutnot 0isTrue - Why
100 and 200returns200
The three logical operators
| Operator | Purpose |
|---|---|
and |
True only if both conditions are True |
or |
True if at least one condition is True |
not |
Reverses the truth value |
1. and operator
Syntax:
RelExpr1 and RelExpr2Truth table:
| Left | Right | Result |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
Interactive `and` truth table
true and false = false
Interactive `or` truth table
true or false = true
Interactive truthiness (`not`)
Same rules as `bool()`.
bool(100) = True
not 100 = False
Why `100 and 200` returns `200`
`and` and `or` return one of the operands, not always a boolean.
100 and 200 = 200
100 or 200 = 100
`and` returns the first falsy value, otherwise the last. `or` returns the first truthy value.
Examples:
print(True and False)
print(True and True)
print(10 > 2 and 20 > 4)
print(100 > 200 and 200 > 100)False
True
True
FalseShort-circuit evaluation (and)
Python evaluates from left to right. If the first condition is False, the remaining conditions are skipped.
100 > 200 and 200 > 100Since 100 > 200 is already False, Python immediately returns False without evaluating 200 > 100.
2. or operator
Syntax:
RelExpr1 or RelExpr2Truth table:
| Left | Right | Result |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Examples:
print(True or False)
print(False or False)
print(10 > 2 or 30 > 40)
print(100 > 200 or 40 > 50)True
False
True
FalseShort-circuit evaluation (or)
If the first condition is True, Python skips the rest.
10 > 2 or 30 > 40The first comparison is already True, so Python never evaluates 30 > 40.
3. not operator
not reverses the truth value.
| Value | not |
|---|---|
| True | False |
| False | True |
Examples:
print(not True)
print(not False)
print(not 10)
print(not 0)
print(not "Python")
print(not "")False
True
False
True
False
Truenot follows the same truthiness rules you learned with bool().
Why 100 and 200 returns 200
This is a common interview question. and and or do not always return booleans — they return one of the operands.
andreturns the first falsy value, otherwise the last value.orreturns the first truthy value.
print(100 and 200) # 200
print(0 and 120) # 0
print(100 or 200) # 100
print(0 or 200) # 200More examples
print(120 and 234 and 450)
print(True and 300)
print("Python" and "Java" and "Data Sci")450
300
Data SciPython keeps evaluating until it finds a falsy value. If none exists, it returns the last operand.
Combined expressions
print(10 and 20 or 30)
print(20 or 30 and 40)| Expression | Output |
|---|---|
10 and 20 or 30 |
20 |
20 or 30 and 40 |
20 |
and has higher precedence than or.
Practice exercises
Exercise 1 — Predict before running:
100 > 200 and 200 > 100Will Python evaluate the second comparison? (No — short-circuit.)
Exercise 2 — What will these print?
not ""
not "False"Remember: non-empty strings are truthy.
Exercise 3 — Explain the difference:
100 and 200
100 or 200Quick knowledge check
-
When does
andshort-circuit?
When the first operand is False. -
What is
not 0?
True -
What is
100 and 200?
200(the last truthy operand)
Summary
and,or, andnotcombine logical conditions.- Short-circuit:
andstops on False,orstops on True. notreverses truthiness (0,"", andFalsebecomeTruewithnot).- With non-boolean values,
andandorreturn one of the operands, not necessarilyTrueorFalse.
Next lesson: Bitwise Operators — integers bit by bit with <<, >>, &, |, ^, and ~.
Loading playground…