Lesson 04
bool() — Convert to Boolean
Convert values with bool() using the non-zero / zero rule for numbers and the non-empty / empty rule for strings.
bool() converts a value into either True or False.
The most important rule in Python is simple: non-zero numbers are True, zero is False. Strings follow a different rule — they depend on length, not the characters they contain.
What you'll learn
- Convert values into
TrueorFalse - Apply the non-zero / zero rule for numbers
- Understand why
bool("0")is True - Learn why only the empty string is False
What does bool() do?
bool() converts one possible type of value into a boolean.
result = bool(value)There are two rules to memorize:
- Numbers and complex: non-zero →
True, zero →False - Strings: non-empty →
True, empty →False
Example 1 — Integer → Boolean
Every non-zero integer becomes True.
print(bool(100))
print(bool(0))
print(bool(-123))
print(bool(0b0000))True
False
True
False0b0000 is simply another way to write the integer 0, so it becomes False.
Example 2 — Float → Boolean
The same rule applies to floating-point numbers.
print(bool(1.2))
print(bool(0.5))
print(bool(0.0))True
True
FalseEven an extremely tiny number is still non-zero.
tiny = 1e-62
print(bool(tiny))TrueTiny number walkthrough
A tiny float can eventually become 0.0 through nested casting.
| Step | Expression | Result |
|---|---|---|
| 1 | int(1e-62) |
0 |
| 2 | bool(0) |
False |
| 3 | float(False) |
0.0 |
tiny = 1e-62
result = float(bool(int(tiny)))
print(result)0.0int() is the step that turns the tiny value into zero.
Example 3 — Complex → Boolean
Unlike int() and float(), bool() accepts complex numbers.
| Value | bool() |
|---|---|
2+3j |
True |
0+0j |
False |
The value 0+0j is the zero complex number, so it becomes False.
Example 4 — String → Boolean
bool() does not read the numeric meaning of a string.
It only asks one question: Is the string empty?
| String | Length | bool() |
|---|---|---|
"123" |
3 | True |
"0" |
1 | True |
"False" |
5 | True |
" " |
3 | True |
"" |
0 | False |
Key takeaway: "0" is not zero. It is one character, so it is True.
Interactive playground
Choose a source type and watch how bool() evaluates it.
Choose the source type
Python simulation
bool(100)
Result
True bool
bool conversion playground
Why is bool("0") True?
This is one of Python’s most famous beginner questions.
| Call | Result | Why | |
|---|---|---|---|
| Numeric zero | bool(0) |
False |
Zero value |
String "0" |
bool("0") |
True |
Length = 1 |
Python evaluates strings by length, not by their visible characters.
String indexing example
A complex string behaves differently from a complex number.
| Index | Character | Expression | Result |
|---|---|---|---|
0 |
"2" |
bool(a[0]) |
True (non-empty string) |
2 |
"3" |
bool(float(a[2])) |
True (non-zero number) |
a = "2+3j"
print(bool(a[0])) # True
print(bool(float(a[2]))) # Truea[0] is the character "2", not the number 2. Indexing is string work.
Complete conversion map
| Source | Example | Result |
|---|---|---|
| Non-zero int | bool(100) |
True |
| Zero int | bool(0) |
False |
| Non-zero float | bool(0.5) |
True |
| Zero float | bool(0.0) |
False |
| Non-zero complex | bool(2+3j) |
True |
| Zero complex | bool(0+0j) |
False |
| Non-empty string | "0", "False" |
True |
| Empty string | "" |
False |
Practice exercises
Exercise 1 — Predict the output before running it.
bool("0")
bool(0)One is True, the other is False.
Exercise 2 — Compare these carefully.
bool("False")
bool(False)Why does only one become False?
Exercise 3 — Explain each step.
tiny = 1e-62
float(bool(int(tiny)))Which conversion changes the value into zero?
Quick knowledge check
-
What is the rule for numbers?
Non-zero →True; zero →False. -
Is
bool("0")False?
No. The string contains one character, so it isTrue. -
When is a string False?
Only when its length is0, meaning it is exactly"".
Summary
bool()converts values intoTrueorFalse.- Numbers and complex values follow the non-zero / zero rule.
- Strings follow the non-empty / empty rule.
"0"and"False"are bothTruebecause they are not empty.- Nested casting can reduce a tiny float into
0.0throughint()→bool()→float().
Loading playground…