🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Booleans

AI & DATA SCIENCE // booleans

A boolean (bool) represents one of two truth values, True or False, and is implemented in Python as a subclass of int.

Syntax

x = True
y = False
bool(0)
bool("")

Deep Dive Course

bool has exactly two instances, True and False, and every object in Python has an associated truthiness that bool() can compute: values like 0, 0.0, None, and empty containers are falsy, while most everything else is truthy. Because bool is a subclass of int, True and False behave as 1 and 0 in arithmetic contexts — adding True to True really does evaluate to 2.

1Understanding Booleans

bool has exactly two instances, True and False, and every object in Python has an associated truthiness that bool() can compute: values like 0, 0.0, None, and empty containers are falsy, while most everything else is truthy. Because bool is a subclass of int, True and False behave as 1 and 0 in arithmetic contexts — adding True to True really does evaluate to 2.

💡

Write `if value:` instead of `if value == True:` — the explicit comparison is redundant, slightly slower, and considered unidiomatic Python.

editor.html
print(bool(0), bool(1), bool(""), bool("hi"))
print(True + True)
localhost:3000

2Practical Example

Here is a real-world application of Booleans showing how it is used in production Python code.

editor.html
scores = [55, 90, 45, 80]
passing_count = sum(score >= 60 for score in scores)
print(passing_count)
localhost:3000

3Best Practices

Follow these guidelines when working with Booleans:

1. Rely on an object's natural truthiness (if items:) instead of comparing explicitly to True or an empty container

2. Remember bool is a subclass of int, so a boolean also counts as an instance of int — guard against that if it matters for your checks

3. Use sum() over a generator of boolean conditions to count how many items satisfy a condition, since True adds as 1

⚠️

Tip: Write `if value:` instead of `if value == True:` — the explicit comparison is redundant, slightly slower, and considered unidiomatic Python.

editor.html
print(bool(0), bool(1), bool(""), bool("hi"))
print(True + True)
localhost:3000

Examples

Example 01Basic Usage
print(bool(0), bool(1), bool(""), bool("hi"))
print(True + True)
Example 02Advanced Example
scores = [55, 90, 45, 80]
passing_count = sum(score >= 60 for score in scores)
print(passing_count)

Best Practices

  • Rely on an object's natural truthiness (if items:) instead of comparing explicitly to True or an empty container
  • Remember bool is a subclass of int, so a boolean also counts as an instance of int — guard against that if it matters for your checks
  • Use sum() over a generator of boolean conditions to count how many items satisfy a condition, since True adds as 1

Interview Question

Why does adding True and True evaluate to 2 in Python instead of raising a TypeError?

Hint: Think about bool's place in Python's type hierarchy.

bool is implemented as a subclass of int, with True defined as the integer value 1 and False as 0. Since subclasses inherit their parent's behavior, arithmetic operators work on booleans exactly as they would on the underlying integers, so adding two True values uses int addition and produces 2.

Exercises

MediumPractice using Booleans in a real scenario.
View Solution
print(bool(0), bool(1), bool(""), bool("hi"))
print(True + True)

Frequently Asked Questions

Why does adding True and True evaluate to 2 in Python instead of raising a TypeError?

bool is implemented as a subclass of int, with True defined as the integer value 1 and False as 0. Since subclasses inherit their parent's behavior, arithmetic operators work on booleans exactly as they would on the underlying integers, so adding two True values uses int addition and produces 2.

Related Functions

isinstance()integerscomparison-operators