Booleans

Booleans represent one of two values: True or False.

Syntaxis_ready = True

A Boolean is either True or False. Note the capital letters — Python is case-sensitive. Booleans are the basis of every decision your program makes.

Truthy and falsy

Any value can be tested as a condition. The values treated as falsy are: False, 0, 0.0, '' (empty string), None, and empty collections like [] or {}. Everything else is truthy.

Example

Example · python
is_ready = True
print(10 > 5)
print(bool(''))
print(bool('hi'))
print(bool(0))

# Output:
# True
# False
# True
# False

When to use it

  • A login system stores an is_authenticated flag that gates access to protected pages.
  • A form validator checks multiple boolean conditions before enabling the submit button.
  • A game loop uses a running variable set to False to exit cleanly when the player quits.

More examples

Boolean from comparison

A comparison expression produces a bool value stored in a descriptive variable.

Example · python
age = 20
is_adult = age >= 18
print(is_adult)        # True
print(type(is_adult))  # <class 'bool'>

Truthy and falsy values

Shows which built-in values are treated as False (falsy) or True (truthy) in conditions.

Example · python
values = [0, '', None, [], 1, 'hi', [1]]
for v in values:
    print(repr(v), '->', bool(v))

# Output:
# 0 -> False
# '' -> False
# None -> False
# [] -> False
# 1 -> True
# 'hi' -> True
# [1] -> True

Boolean in if statement

Combines two bool variables with logical operators to control program flow.

Example · python
is_logged_in = True
has_permission = False

if is_logged_in and not has_permission:
    print('Access denied: insufficient permissions')

# Output:
# Access denied: insufficient permissions

Discussion

  • Be the first to comment on this lesson.