Booleans, Truthiness & None

Booleans — True and False — are the answers to every yes/no question in a program. Python also treats every other value as "truthy" or "falsy" in conditions, and has a special value, None, for "nothing here".

True and False

PY bools.py
is_logged_in = True
has_errors = False

print(is_logged_in, type(has_errors))
print(5 > 3, 5 == 3)
print(isinstance(True, int), True + True)
Output
True <class 'bool'>
True False
True 2

Capitalisation matters: True, not true. Because bool is a subclass of int, True equals 1 — which makes counting easy: sum(x > 0 for x in values) counts positive values.

Truthiness

Any value can be used as a condition. These are falsy:

  • False and None
  • Zero of any numeric type: 0, 0.0, 0j
  • Empty collections and strings: "", [], (), {}, set(), range(0)

Everything else is truthy.

PY truthy.py
values = [0, 1, -1, "", "0", " ", [], [0], {}, None, 0.0]

for v in values:
    print(f"{v!r:>6} -> {'truthy' if v else 'falsy'}")
Output
     0 -> falsy
     1 -> truthy
    -1 -> truthy
    '' -> falsy
   '0' -> truthy
   ' ' -> truthy
    [] -> falsy
   [0] -> truthy
    {} -> falsy
  None -> falsy
   0.0 -> falsy

Using truthiness well

The Pythonic way to check for an empty collection is to use it directly as the condition:

PY idiom.py
cart = []

if not cart:
    print("Your cart is empty")

name = "  ".strip()
print("Hello", name or "stranger")
Output
Your cart is empty
Hello stranger

if not cart: is preferred over if len(cart) == 0:. The or trick supplies a default when the left side is falsy.

Careful: truthiness treats 0 and "" as missing. If zero is a valid value — a quantity, a score, a temperature — check explicitly with is None instead of relying on truthiness.

and, or, not return values

and and or don't just return True/False; they return one of their operands, and stop evaluating as soon as the answer is known (short-circuiting):

PY short_circuit.py
print(0 or "default")
print("value" or "default")
print("" and "never")
print("first" and "second")
print(not "text", not 0)
Output
default
value

second
False True

The third line is an empty string. a or b returns a if it's truthy, otherwise b. a and b returns a if it's falsy, otherwise b.

None

None represents the absence of a value. Functions without a return statement return it, and it's the conventional default for optional arguments:

PY none.py
def greet(name=None):
    if name is None:
        return "Hello!"
    return f"Hello, {name}!"

def log(message):
    print(message)

print(greet())
print(greet("Ana"))
result = log("logging…")
print(result)
Output
Hello!
Hello, Ana!
logging…
None

is None, not == None

PY is_none.py
value = None

print(value is None)
print(value is not None)
print(type(None))
Output
True
False
<class 'NoneType'>

None is a singleton — there's exactly one None object — so the identity check is is both correct and fast. Style guides and linters require it. Use == for comparing values, is only for None (and the other singletons True/False in rare cases).

any() and all()

PY any_all.py
scores = [72, 88, 95, 61]

print(any(s >= 90 for s in scores))
print(all(s >= 60 for s in scores))
print(all([]), any([]))
Output
True
True
True False

any asks "is at least one true?"; all asks "are they all true?". On an empty iterable, all is true and any is false.

Try it yourself

Try it yourself
Console output