Python bool() Function

Converts a value to a boolean value

Usage

The bool() function converts a value to a boolean value i.e. one of True or False.

If the value is not specified, the method returns False.

Syntax

bool(value)

Python bool() function parameters
ParameterConditionDescription
valueOptionalAny object (like Number, List, String etc.),
Any expression (like x > y, x in y).

Falsy Values

In Python, everything is considered True, except following values.

  • Constants defined to be false: None and False.
  • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • Empty sequences and collections: '', (), [], {}, set(), range(0)

Examples

Here are some examples that will shed some light on how this function works.

# bool() on falsy values

print(bool(0))			# Prints False

print(bool([]))			# Prints False

print(bool(0.0))		# Prints False

print(bool(None))		# Prints False

print(bool(0j))			# Prints False

print(bool(range(0)))	# Prints False
# bool() on truthy values

print(bool(1))			# Prints True

print(bool([0]))		# Prints True

print(bool([1, 2]))		# Prints True

print(bool(10))			# Prints True

print(bool(3+4j))		# Prints True

print(bool(range(2)))	# Prints True