Remember
bool() converts a value to True or False according to truth testing. Zero and empty collections are false.
bool() converts a value to True or False according to truth testing. Zero and empty collections are false.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = 3
print(bool(number))Top-level scope
3bool() converts a value to True or False according to truth testing. Zero and empty collections are false.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 3
print(bool(number))
|
v
DISPLAYED RESULT
True
FINAL BINDINGS
number --> object 1 (int)
object 1: 3Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = 3
print(bool(number))Top-level scope
3The example has finished. Compare the final output with the values and references above.
True
What do you expect this program to print?
number = 3
print(bool(number))bool() FunctionThe bool() function in Python is used to convert a value to a Boolean – either True or False. It’s a very useful function when you want to check if a value is considered "truthy" or "falsy".
bool(value)
value – Any Python object (optional)True or False depending on the "truthiness" of the objectprint(bool(10)) # True
print(bool(0)) # False
print(bool("hello")) # True
print(bool("")) # False
print(bool([1, 2, 3])) # True
print(bool([])) # False
print(bool({})) # False
print(bool(None)) # False
These are treated as False when passed to bool():
00.0"" (empty string)[] (empty list){} (empty dict)set() (empty set)NoneEverything else is considered True.
user_input = input("Enter your name: ")
if bool(user_input):
print("Hello,", user_input)
else:
print("You didn't enter anything!")
"False" (string) is False – it’s actually True!None is always FalseUnderstanding what values are considered falsy is important in conditions, loops, and clean code. Python often uses if some_list: instead of if len(some_list) > 0:
bool() converts a value to True or False0, "", [], {}, NoneWrite a program to check if the user entered any text. If not, ask them to try again.
text = input("Enter something: ")
if bool(text):
print("You entered:", text)
else:
print("Please enter some text.")
Connect what you noticed in the example with the idea below.
bool() converts a value to True or False according to truth testing. Zero and empty collections are false.
Every non-empty value is False.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.