Code of the Day
BeginnerData and logic

Boolean logic

Master comparison and logical operators, truthy/falsy values, and short-circuit evaluation.

PythonBeginner9 min read
Recommended first
By the end of this lesson you will be able to:
  • Use comparison operators to produce True or False
  • Combine conditions with and, or, not
  • Explain truthy and falsy values in Python
  • Apply short-circuit evaluation and chained comparisons

Every if and while statement relies on a condition that is either True or False. Understanding how Python builds and evaluates those conditions gives you much finer control over your programs' behaviour.

Comparison operators

Six operators compare two values and always return a :

x = 7

x == 7    # True   — equal to
x != 5    # True   — not equal to
x > 3     # True   — greater than
x < 10    # True   — less than
x >= 7    # True   — greater than or equal to
x <= 6    # False  — less than or equal to

A common mistake is writing = (assignment) where == (comparison) was meant. Python will raise a SyntaxError in most of those cases, but it's still worth internalising the distinction early.

Logical operators

Three keywords combine boolean values:

ExpressionMeaning
a and bTrue only if both are true
a or bTrue if at least one is true
not aFlips True to False and vice versa
age = 20
has_ticket = True

age >= 18 and has_ticket    # True  — both conditions met
age < 18 or has_ticket      # True  — one is enough
not has_ticket              # False — flipped

Truthy and falsy values

Python's if statement doesn't require a literal True or False — it accepts any expression and evaluates it as "truthful" or "falsy". These values are always falsy:

  • False and None
  • Zero: 0, 0.0
  • Empty containers: "", [], {}, set()

Everything else is truthy. This lets you write natural-sounding guards:

name = input_value   # could be an empty string

if name:
    print(f"Hello, {name}")
else:
    print("No name provided")

That if name: is more idiomatic than if name != "": and says the same thing.

Python — editable, runs in your browser

Short-circuit evaluation

Python evaluates and and or lazily — this is : it stops as soon as the result is determined:

  • a and b: if a is falsy, Python never looks at b (the result is already False).
  • a or b: if a is truthy, Python never looks at b (the result is already True).

This is useful as a guard pattern:

text = ""
length = text and len(text)   # text is falsy → short-circuits to ""
                               # avoids calling len() on None, etc.

And for providing defaults:

display_name = user_name or "Anonymous"

Short-circuit evaluation isn't just a performance detail — it's a safety feature. The pattern obj and obj.method() avoids calling a method on None because the right side is never evaluated when obj is falsy.

Chained comparisons

Python lets you chain comparisons the way mathematicians write them, which reads far more naturally than the equivalent and:

x = 7

1 < x < 10          # True  — x is between 1 and 10 (exclusive)
0 <= x <= 100       # True  — x is in the 0–100 range

# Equivalent but more verbose:
1 < x and x < 10

Each chained comparison evaluates each value once and short-circuits, so 1 < x < 10 is both readable and efficient.

Is a teenager?Python

Write is_teen(age) that returns True if age is between 13 and 19 inclusive, False otherwise. Use a chained comparison.

is_teen(16)Trueis_teen(20)False

Don't confuse is with ==. is tests object identity (same object in memory); == tests value equality. Use == for almost everything. The idiomatic exception: x is None (not x == None) because None is a singleton and identity is exactly what you want to check.

Where to go next

You can now write precise conditions for any branch or loop. Next: reading and writing files — applying conditions when processing real data from disk.

Finished reading? Mark it complete to track your progress.

On this page