Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner22 min

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.

Non-zero values are True; zero values are False

What you'll learn

  • Convert values into True or False
  • 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.

python
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.

python
print(bool(100))
print(bool(0))
print(bool(-123))
print(bool(0b0000))
text
True
False
True
False

0b0000 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.

python
print(bool(1.2))
print(bool(0.5))
print(bool(0.0))
text
True
True
False

Even an extremely tiny number is still non-zero.

python
tiny = 1e-62
print(bool(tiny))
text
True

Tiny 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
python
tiny = 1e-62

result = float(bool(int(tiny)))
print(result)
text
0.0

int() 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)
python
a = "2+3j"

print(bool(a[0]))         # True
print(bool(float(a[2])))  # True

a[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.

python
bool("0")
bool(0)

One is True, the other is False.

Exercise 2 — Compare these carefully.

python
bool("False")
bool(False)

Why does only one become False?

Exercise 3 — Explain each step.

python
tiny = 1e-62
float(bool(int(tiny)))

Which conversion changes the value into zero?

Quick knowledge check

  1. What is the rule for numbers?
    Non-zero → True; zero → False.

  2. Is bool("0") False?
    No. The string contains one character, so it is True.

  3. When is a string False?
    Only when its length is 0, meaning it is exactly "".

Summary

  • bool() converts values into True or False.
  • Numbers and complex values follow the non-zero / zero rule.
  • Strings follow the non-empty / empty rule.
  • "0" and "False" are both True because they are not empty.
  • Nested casting can reduce a tiny float into 0.0 through int() → bool() → float().

Loading playground…