In Python, the bool() function is used to convert a value to a boolean value (True or False) to check the truth according to the standard.
If the object has a value of None, False or equal to 0, the bool() function will return false. Except for all of the above, the bool() function will return true.
The following article is a detailed guide on how to use the bool() function in Python as we go through the section below.
x = [] print(bool(x)) y = 1 print(bool(y))
Output:
False
True
The bool() function can return 2 values: True and False.
If object is empty like (), [], {}, the function will return False.
bool(object)
Parameter Values:
object: any object. For example: number, string, list…
Example 1: Basic bool() function
x = 1 y = 2 print(bool(x == y))
Output:
False
Example 2: Parity check program
def check(x): return (bool(x % 2 == 0)) x = 4 if (check(x)): print("Correct") else: print("Incorrect")
Output:
Correct
Hope you understand our tutorial on how to use the bool() function in Python.
Thanks for reading!
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.
The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…
Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…
Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…
Focal Fossa will reach the End of Standard Support in May 2025, also known as…
Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…
Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…