1.2.8Introduction to Programming (Python)

Logical operators — and, or, not, short-circuit evaluation

1,944 words9 min readdifficulty · medium2 backlinks

WHY do logical operators exist?

Real decisions depend on multiple conditions at once. "Let the user in IF they have a ticket AND they are over 18." One boolean isn't enough; you need glue between conditions. Logical operators are that glue.


WHAT each operator does (truth tables, derived)

Instead of memorizing, build the tables from the meaning.

and means both. So it is true ONLY in the single case where both are true:

A B A and B Why
T T T both true → both → yes
T F F B failed
F T F A failed
F F F both failed

or means at least one. So it is false ONLY when both fail:

A B A or B Why
T T T at least one
T F T A is enough
F T T B is enough
F F F nothing true

not just flips: not TrueFalse, not FalseTrue.

Figure — Logical operators — and, or, not, short-circuit evaluation

HOW short-circuit evaluation works (the core idea)

The surprising consequence: and/or return one of the operands, not necessarily True/False.

>>> "hello" and 42      # "hello" is truthy → return the second one
42
>>> 0 and 42            # 0 is falsy → return 0 immediately, 42 never seen
0
>>> "" or "default"     # "" is falsy → fall through to second
'default'
>>> "name" or "default" # "name" is truthy → return it, stop
'name'

Forecast-then-Verify

Recall Predict the output BEFORE reading the answer
print(3 and 0)        # ?
print(3 or 0)         # ?
print(not "")         # ?
print([] or "x" or 9) # ?
print(1 and 2 and 3)  # ?

Answers: 0 (3 truthy → return second), 3 (3 truthy → return first), True (empty string falsy → not flips), "x" ([] falsy, "x" truthy → stop), 3 (all truthy → returns last).


Common mistakes (Steel-manned)


The 80/20 takeaway

  1. and = all true; or = any true; not = flip.
  2. Short-circuit: it stops as soon as the answer is known and returns an operand.
  3. Use x or default for fallbacks, and safe_check and risky_check to guard.

Flashcards

What does and return when its first operand is falsy?
That first (falsy) operand, immediately — the second is never evaluated (short-circuit).
What does or return when its first operand is truthy?
That first (truthy) operand, immediately — the second is skipped.
List Python's falsy values.
False, 0, 0.0, "", [], {}, (), None.
What is 3 and 0?
0 (3 is truthy so it returns the second operand).
What is 3 or 0?
3 (3 is truthy so it returns the first operand and stops).
State De Morgan: not (A and B) equals?
(not A) or (not B).
Why is if user and user.name: safe when user is None?
and short-circuits on the falsy None, so user.name is never accessed → no AttributeError.
Why is if x == 1 or 2: a bug?
It parses as (x==1) or 2; 2 is truthy so it's always True. Use x == 1 or x == 2.
Difference between and and &?
and is logical (short-circuits, returns operand); & is bitwise on integers, no short-circuit.
What does [] or "x" or 9 return?
"x"[] is falsy so skip, "x" is truthy so stop and return it.

Recall Feynman: explain to a 12-year-old

Imagine a bouncer at a party. For AND he wants you to pass two tests — ticket AND age. The second you fail the first test, he stops checking and says "no." For OR he's nice: pass any one test and you're in — so once test one passes he doesn't bother with test two. NOT is the mirror: it just says the opposite. And Python's bouncer is lazy on purpose — he stops the instant he knows the answer, which saves time and avoids touching things that might explode.

Connections

Concept Map

includes

includes

includes

combine

generalized to

derives

derives

relates via

linked by

linked by

uses

uses

checks

causes

Logical operators

and — both true

or — at least one

not — flip

Boolean True/False

Truthiness — truthy/falsy

Truth tables

De Morgan laws

Short-circuit evaluation

Returns an operand

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, logical operators ka kaam hai do ya zyada conditions ko jodna. and ka matlab hai "dono sach hone chahiye" — agar ek bhi false hua toh poora false. or thoda generous hai — "koi ek bhi sach ho toh kaafi hai". Aur not simply ulta kar deta hai: True ko False, False ko True. Real life mein decision aksar multiple conditions pe depend karta hai, jaise "ticket hai AND umar 18+ hai", isiliye ye operators bahut kaam aate hain.

Ab asli interesting cheez: Python lazy (aalsi) hai — isko short-circuit evaluation kehte hain. and mein agar pehli value already falsy hai, toh answer toh decided hai (false), isliye Python doosri value ko dekhta hi nahi. Same or mein, pehli value truthy hui toh bas, ruk gaya. Iska ek mast fायda: if user and user.name: likho toh agar user None hai toh user.name chhua hi nahi jayega, error nahi aayega. Order important hai — safety check pehle rakho.

Ek trick yaad rakho: Python ke and/or True/False nahi, balki operand return karte hain. 3 and 0 ka answer 0 hai, aur "" or "Guest" ka answer "Guest". Isliye default value dene ka shortcut hota hai: name = name or "Guest". Galti se if x == 1 or 2: mat likhna — wo hamesha True hoga kyunki 2 truthy hai; sahi hai x == 1 or x == 2. Aur and/or (shabd) use karo, &/| (bitwise) nahi — wo alag cheez hai.

Test yourself — Introduction to Programming (Python)

Connections