Boolean Basics
Every decision your program makesβwhether to show a message, repeat a step, or validate inputβboils down to a Boolean: True
or False
. This chapter introduces Booleans and how ordinary Python values behave in conditions.
What is a Boolean?
A Boolean is a value that represents truth: True
or False
(note the capitalization). You'll often get Booleans from comparisonsβage >= 18
yields True
when the condition holdsβand you'll use them directly in control flow, e.g. if is_adult:
.
Truthiness in practice
In if
/while
conditions, Python treats many objects as "truthy" or "falsey". Empty or zero-like values are considered false; everything else is true. This lets you write natural checks such as if items:
or if name:
without extra comparisons.
Common falsey values
False
;None
;0
,0.0
;""
(empty string);- Empty containers:
[]
,()
,{}
,set()
.
Non-empty strings are truthyβeven "0"
or "False"
.
12345678910is_ready = True name = "" count = 0 if is_ready: print("Go!") # runs, because True print(bool(name)) # False β empty string print(bool(count)) # False β zero print(bool("0")) # True β non-empty string
1. Which value is falsey in Python?
2. What value will this code output?
3. Which if
will not execute its body?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about truthy and falsey values in Python?
What are some common mistakes when using Booleans in conditions?
Can you give more examples of how Booleans are used in control flow?
Awesome!
Completion rate improved to 5
Boolean Basics
Swipe to show menu
Every decision your program makesβwhether to show a message, repeat a step, or validate inputβboils down to a Boolean: True
or False
. This chapter introduces Booleans and how ordinary Python values behave in conditions.
What is a Boolean?
A Boolean is a value that represents truth: True
or False
(note the capitalization). You'll often get Booleans from comparisonsβage >= 18
yields True
when the condition holdsβand you'll use them directly in control flow, e.g. if is_adult:
.
Truthiness in practice
In if
/while
conditions, Python treats many objects as "truthy" or "falsey". Empty or zero-like values are considered false; everything else is true. This lets you write natural checks such as if items:
or if name:
without extra comparisons.
Common falsey values
False
;None
;0
,0.0
;""
(empty string);- Empty containers:
[]
,()
,{}
,set()
.
Non-empty strings are truthyβeven "0"
or "False"
.
12345678910is_ready = True name = "" count = 0 if is_ready: print("Go!") # runs, because True print(bool(name)) # False β empty string print(bool(count)) # False β zero print(bool("0")) # True β non-empty string
1. Which value is falsey in Python?
2. What value will this code output?
3. Which if
will not execute its body?
Thanks for your feedback!