</>
Skip to content
Python lessons (11/45)

Python — Booleans

What are Booleans?

Booleans represent one of two values: True or False:

is_active = True
is_deleted = False

Note the capital T and F — Python booleans are capitalized.

Creating Booleans

# Direct values
a = True
b = False

# From comparisons
x = 5 > 3      # True
y = 10 == 5    # False
z = 5 != 3     # True

# From function returns
is_even = lambda n: n % 2 == 0
is_even(4)     # True

Comparison Operators

These return booleans:

OperatorMeaningExample
==Equal5 == 5 → True
!=Not equal5 != 3 → True
>Greater than5 > 3 → True
<Less than5 < 3 → False
>=Greater or equal5 >= 5 → True
<=Less or equal5 <= 3 → False
x = 10
print(x > 5)    # True
print(x == 10)  # True
print(x != 5)   # True

Logical Operators

OperatorMeaningExample
andBoth must be TrueTrue and False → False
orAt least one TrueTrue or False → True
notFlips the valuenot True → False

and

age = 25
has_id = True

if age >= 21 and has_id:
    print("Entry allowed")

or

is_weekend = False
is_holiday = True

if is_weekend or is_holiday:
    print("Day off!")

not

is_logged_in = False

if not is_logged_in:
    print("Please log in")

Truthiness and Falsiness

Python treats certain values as True or False:

Falsy Values

False
0
0.0
0j          # complex zero
""          # empty string
[]          # empty list
()          # empty tuple
{}          # empty dict
set()       # empty set
None

Truthy Values

True
1           # any non-zero number
-1
"hello"     # any non-empty string
[1, 2]      # non-empty list
{"a": 1}    # non-empty dict

Using Truthiness

# Check if list has items
items = [1, 2, 3]
if items:
    print("List is not empty")

# Check if string is not empty
name = ""
if not name:
    print("Name is empty")

# Check if number is non-zero
count = 0
if count:
    print("Count is non-zero")

bool() Function

Convert any value to boolean:

bool(0)          # False
bool("")         # False
bool(None)       # False
bool([])         # False

bool(1)          # True
bool("hello")    # True
bool([1, 2])     # True
bool({"a": 1})   # True

Short-Circuit Evaluation

Python stops evaluating as soon as the result is determined:

# and — returns first falsy value
result = "" and "hello"  # "" (empty string is falsy)
result = "hi" and "hello"  # "hello"

# or — returns first truthy value
result = "" or "default"  # "default"
result = "hello" or "world"  # "hello"

# Practical use: default values
name = user_name or "Anonymous"

Ternary Conditional

# condition_if_true if condition else condition_if_false
status = "adult" if age >= 18 else "minor"

# Nested ternary (avoid for readability)
grade = "A" if score >= 90 else "B" if score >= 80 else "C"

Common Patterns

# Check membership
if "apple" in fruits:
    print("Found apple")

# Guard clauses
if not user:
    return redirect("/login")

# Boolean flag
is_running = True
while is_running:
    # game loop
    if should_quit:
        is_running = False

# Toggle
lights_on = not lights_on

Best Practices

  • Use is for None comparisons: if x is None
  • Use == for value comparisons: if x == 5
  • Don't compare booleans to True/False explicitly
    • Bad: if is_active == True
    • Good: if is_active
  • Use truthiness for checking emptiness: if items: not if len(items) > 0
  • Use not for negation: if not items:

Mini Practice

  1. Create variables and compare them with all comparison operators
  2. Write a condition using and, or, and not
  3. Test truthiness of different values with bool()
  4. Use short-circuit evaluation to set default values
  5. Write a ternary expression to classify a number

Up Next

Next: Strings →

Related Topics

Frequently Asked Questions about Booleans

What is Booleans in Python?

Booleans is a fundamental concept in Python. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Booleans?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Booleans.

Why is Booleans important in Python?

Booleans is essential for Python development. Understanding this concept will help you write better code and solve real-world problems more effectively.