Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner25 min

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.

Logical operators combine conditions

What you'll learn

  • Truth tables for and, or, and not
  • Short-circuit evaluation
  • Why not 10 is False but not 0 is True
  • Why 100 and 200 returns 200

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:

text
RelExpr1 and RelExpr2

Truth table:

Left Right Result
True True True
True False False
False True False
False False False

Interactive `and` truth table

Left
Right

true and false = false

Interactive `or` truth table

Left
Right

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:

python
print(True and False)
print(True and True)

print(10 > 2 and 20 > 4)
print(100 > 200 and 200 > 100)
text
False
True
True
False

Short-circuit evaluation (and)

Python evaluates from left to right. If the first condition is False, the remaining conditions are skipped.

Second test skipped when first and operand is False
python
100 > 200 and 200 > 100

Since 100 > 200 is already False, Python immediately returns False without evaluating 200 > 100.

2. or operator

Syntax:

text
RelExpr1 or RelExpr2

Truth table:

Left Right Result
True True True
True False True
False True True
False False False

Examples:

python
print(True or False)
print(False or False)

print(10 > 2 or 30 > 40)
print(100 > 200 or 40 > 50)
text
True
False
True
False

Short-circuit evaluation (or)

If the first condition is True, Python skips the rest.

Second test skipped when first or operand is True
python
10 > 2 or 30 > 40

The 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:

python
print(not True)
print(not False)

print(not 10)
print(not 0)

print(not "Python")
print(not "")
text
False
True
False
True
False
True

not 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.

  • and returns the first falsy value, otherwise the last value.
  • or returns the first truthy value.
python
print(100 and 200)      # 200
print(0 and 120)        # 0

print(100 or 200)       # 100
print(0 or 200)         # 200

More examples

python
print(120 and 234 and 450)
print(True and 300)

print("Python" and "Java" and "Data Sci")
text
450
300
Data Sci

Python keeps evaluating until it finds a falsy value. If none exists, it returns the last operand.

Combined expressions

python
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:

python
100 > 200 and 200 > 100

Will Python evaluate the second comparison? (No — short-circuit.)

Exercise 2 — What will these print?

python
not ""
not "False"

Remember: non-empty strings are truthy.

Exercise 3 — Explain the difference:

python
100 and 200
100 or 200

Quick knowledge check

  1. When does and short-circuit?
    When the first operand is False.

  2. What is not 0?
    True

  3. What is 100 and 200?
    200 (the last truthy operand)

Summary

  • and, or, and not combine logical conditions.
  • Short-circuit: and stops on False, or stops on True.
  • not reverses truthiness (0, "", and False become True with not).
  • With non-boolean values, and and or return one of the operands, not necessarily True or False.

Next lesson: Bitwise Operators — integers bit by bit with <<, >>, &, |, ^, and ~.

Loading playground…