Python bool()

Python bool() function converts a value into its Boolean equivalent (True or False). It helps you determine the “truthiness” of any data, which is essential for decision-making in your code using if statements and loops.

bool() Syntax

truth_value = bool(value)
  • truth_value: A variable to store the resulting Boolean value (True or False).
  • bool(): Built-in Python function for Boolean conversion.
  • value: Any Python object or value you want to check for truthiness.

Example 1: Python bool()

result = bool("Hello")
print(result)

Code Explanation

  • Line 1: The non-empty string “Hello” is considered True, so result is assigned True.
  • Line 2: Prints the Boolean value (True) to the console.

Output

True


Example 2: Python bool() with True Arguments

print(bool(385))
print(bool(32.21))
print(bool('Read Python bool() carefully'))
print(bool(True))

Code Explanation

  • Non-zero numbers (integers or floats) are True.
  • Non-empty strings are True.
  • The Boolean value True itself is obviously True.

Output

True
True
True
True


Example 3: Python bool() with False Arguments

print(bool([]))
print(bool(0))
print(bool(None))
print(bool(False))

Code Explanation

  • Empty sequences (like lists) are False
  • The number 0 is False
  • The special value None is False
  • The Boolean value False itself is False.

Output

False
False
False
False


Example 4: Python bool() with Empty vs Non-Empty Objects

empty_list = []
non_empty_list = [1, 2, 3]

print(bool(empty_list))
print(bool(non_empty_list))

Code Explanation

  • Lines 1 and 2: Creates an empty and a non-empty list
  • Lines 4 and 5: Demonstrates that empty objects are False, while non-empty ones are True

Output

False
True


Also Read

Python bin()

Python bytearray()