Lesson 1.6 – Falsiness in Python

Warmup

What is the result of the following?

num1 = 0
num2 = 2

print(num1 and num2 > 1)
print(num1 or num2 == 0)

Warmup explanation

A common mistake is to only put the comparison in the second operant in an and or or expression.

In the previous examples, python is checking if num1 itself is True or False, not comparing it with 0.

Boolean Conversion

  • Any value in Python can be turned into True/False using bool()
  • Example:
print(bool(True))   # True
print(bool(False))  # False

Falsy Values

  • Python considers these as False:

    • False
    • None
    • 0, 0.0
    • "" (empty string)
    • [], {}, (), set() (empty containers)

Truthy Values

  • Everything else is True
  • Examples:
print(bool(42))       # True
print(bool("hello"))  # True
print(bool([1, 2]))   # True

Practice: What’s the Output?

print(bool(0))
print(bool(""))
print(bool([]))
print(bool(None))

Why It Matters

  • Falsiness is how Python decides if something “counts as False”
  • You’ll use this later in conditionals
  • For now: test with bool()

Summary

  • Falsy = False, None, 0, "", empty containers
  • Truthy = everything else
  • Use bool(value) to check