Loading...

Python Truthiness

An empty text is not the value False, but a condition treats it as false. bool("") asks that question and writes False. bool("lida") writes True, because the text has characters. bool answers with True or False, the two values from the previous page. It does not change what is stored.

Empty text, then lida

python
print(bool(""))
print(bool("lida"))

The first line is False. The second line is True. The quotes in "" have nothing between them.

Zero is not the text 0

bool(0) writes False. The number 0 is empty of any count, so a condition treats it as false. bool("0") writes True. That one is text: a character sits between the quotes, so it is not empty. bool(2) writes True. Any number other than 0 is treated as true. This page does not turn text into a number.

The number 0, the text 0, then 2

python
print(bool(0))
print(bool("0"))
print(bool(2))

The three lines are False, then True, then True.

A space is a character

bool(" ") writes True. One space sits between the quotes. The text is not empty, so a condition treats it as true. Only quotes with nothing between them are the empty text.

A name follows the same rule

guest = "" stores an empty text. bool(guest) writes False. A later line, guest = "lida", replaces it. bool(guest) then writes True. The name is not what matters. What it holds matters.

Empty name, then lida

python
guest = ""
print(bool(guest))
guest = "lida"
print(bool(guest))

The first print is False. The second print is True.

The next page is a missing value

This page does not compare two numbers, choose a branch, or join two conditions. The next page is a value that means nothing is stored. A condition does not treat that one as true either. You meet it there, not here.

Say these yourself

  1. bool("") writes False. bool("lida") writes True.
  2. bool(0) writes False. bool("0") writes True. bool(2) writes True.
  3. bool(" ") writes True, because a space is a character.
  4. The next page is a value that means nothing is stored.

Easy to misread

  • Expecting bool("") to write an empty line
  • Treating the text "0" like the number 0
  • Treating one space as empty
  • Thinking bool changes the stored text
  • Waiting for a branch or a comparison on this page

Keep

bool asks whether a condition would treat a value as true, and answers True or False.
Empty text and the number 0 are treated as false.
"lida", the text "0", the number 2, and a single space are treated as true.
A later line can replace what a name holds, and bool follows that.
The next page is a value that means nothing is stored.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top