Python Booleans

Ka Kavitha V Updated 03 Oct 2026
6 min read ·Lesson 11 of 23

Booleans

A Boolean is a data type that represents one of two logical values:

  • True
  • False

Booleans are the foundation of decision-making in programming. Every time a program asks a yes/no question — Is the user logged in? Is the password correct? Is the cart empty? Has the loop finished? — the answer is a Boolean.

In Python, the Boolean type is called bool. The values True and False are keywords and must be written with a capital first letter; true and false raise a NameError.

is_active = True
has_errors = False

print(is_active)
print(type(has_errors))

Expected output:

True
<class 'bool'>

Boolean Expressions

Any expression that involves a comparison or logical check evaluates to either True or False. Such an expression is called a Boolean expression.

Example: Comparing Values

print(5 > 3)
print(7 == 10)
print(4 <= 4)

Expected output:

True
False
True

Explanation:

  • 5 > 3 — 5 is greater than 3, so the result is True.
  • 7 == 10 — == checks equality; 7 is not equal to 10, so the result is False.
  • 4 <= 4 — 4 is less than or equal to 4, so the result is True.

Remember: = assigns a value; == compares two values.

You can store the result of a Boolean expression in a variable:

age = 20
is_adult = age >= 18
print(is_adult)

Expected output:

True

Booleans in Conditional Statements

Booleans are most commonly used in if–else statements to decide which code runs.

Example: Conditional Logic

marks = 72
passing_marks = 50

if marks >= passing_marks:
    print("Student passed the exam")
else:
    print("Student failed the exam")

Expected output:

Student passed the exam

How it works:

  1. Python evaluates marks >= passing_marks, which is 72 >= 50.
  2. The result is True.
  3. Because the condition is True, the if block runs and the else block is skipped.

If marks were 40, the condition would be False, and the else block would run instead.

Evaluating Values With bool()

The bool() function converts any value to its Boolean equivalent, which is called the value's truth value. This tells you how Python would treat the value if you used it directly in an if statement.

Example: Evaluating Direct Values

print(bool("Python"))
print(bool(0))

Expected output:

True
False

Example: Evaluating Variables

language = "Java"
version = 0

print(bool(language))
print(bool(version))

Expected output:

True
False

A non-empty string is True, and the number zero is False.

Truthy and Falsy Values

Python can use any value as a condition, not only True and False. Values that behave like True are called truthy; values that behave like False are called falsy.

Truthy Values

Most values in Python are truthy, including:

  • Non-empty strings — "Code", " " (even a single space)
  • Non-zero numbers — 99, -1, 0.5
  • Non-empty collections — lists, tuples, sets, and dictionaries with at least one item
print(bool("Code"))
print(bool(99))
print(bool({"name": "Alex"}))

Expected output:

True
True
True

Falsy Values

Only a limited set of values are falsy:

Falsy ValueDescription
FalseThe Boolean false value
NoneThe "no value" object
0, 0.0, 0jZero of any numeric type
""An empty string
[], (), {}, set()Empty collections
range(0)An empty range
print(bool(""))
print(bool([]))
print(bool(None))
print(bool(0))

Expected output:

False
False
False
False

Why Truthiness Matters

Truthiness lets you write short, readable conditions. Instead of checking the length of a list explicitly:

tasks = []

if len(tasks) == 0:
    print("No tasks")

you can write:

tasks = []

if not tasks:
    print("No tasks")

Expected output:

No tasks

Both versions work, but the second is the common Pythonic style. not tasks is True because an empty list is falsy.

A Common Surprise

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

Expected output:

True
True
  • "False" is a non-empty string, so it is truthy. Python does not read the meaning of the text.
  • [0] is a list containing one item, so it is not empty and is truthy — even though the item itself is zero.

Custom Objects and Boolean Evaluation

By default, objects created from your own classes are truthy. However, an object evaluates to False if its class defines a __len__() method that returns 0.

Example

class Cart:
    def __len__(self):
        return 0

my_cart = Cart()
print(bool(my_cart))

Expected output:

False

Even though the object exists, Python treats it as False, because __len__() reports that its length is zero. This is the same rule that makes empty lists falsy. (A class can also control its truth value directly by defining a __bool__() method.)

This is an advanced topic; you will learn more about classes and special methods in the object-oriented programming lessons.

Functions That Return Boolean Values

Functions can return Boolean results. Such functions often have names starting with is_, has_, or can_, so that a call reads like a yes/no question.

Example: A Boolean Function

def is_logged_in():
    return False

print(is_logged_in())

Expected output:

False

Example: Using a Function in a Condition

def has_access():
    return True

if has_access():
    print("Access granted")
else:
    print("Access denied")

Expected output:

Access granted

The function call is evaluated first; its return value (True) is then used as the condition.

Practical Example: A Function That Checks a Rule

Real Boolean functions usually calculate their result:

def is_even(number):
    return number % 2 == 0

print(is_even(10))
print(is_even(7))

Expected output:

True
False

number % 2 == 0 is itself a Boolean expression, so the function returns its result directly — there is no need for an if statement inside the function.

Built-in Functions That Return Booleans

Python provides many built-in functions and methods that return Boolean values. One commonly used function is isinstance(), which checks whether a value belongs to a given type.

Example: Type Checking

value = 3.14
print(isinstance(value, float))
print(isinstance(value, int))

Expected output:

True
False

This helps validate data types while the program is running.

Other examples you will meet include string methods such as isdigit() and startswith(), and the built-in functions any() and all():

scores = [45, 82, 67]

print(any(score > 80 for score in scores))   # Is at least one score above 80?
print(all(score > 40 for score in scores))   # Are all scores above 40?

Expected output:

True
True

Booleans Are a Kind of Integer

Internally, bool is a subclass of int: True behaves like 1 and False behaves like 0.

print(True + True)
print(True == 1)

Expected output:

2
True

This is occasionally useful — for example, sum() can count how many conditions are True:

answers = [True, False, True, True]
print(sum(answers))

Expected output:

3

Common Mistakes

MistakeExampleProblem
Lowercase Boolean valuesis_valid = trueNameError — use True
Using = instead of == in a conditionif age = 18:SyntaxError
Treating the string "False" as falsebool("False")It is True (non-empty string)
Comparing directly with Trueif is_valid == True:Works, but if is_valid: is cleaner
  • Python Operators — comparison and logical operators that produce Booleans
  • Python If Statements — using Boolean conditions to control program flow
  • Python None — a falsy value that represents "no value"

0 Comments

Reviewed before they appear

No comments yet.

Python
Ask about this post
AI Ask about this post

Ask questions about Python Booleans and get answers drawn from it.

Signed-in readers only.