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

Example: bool() on falsy values

print(bool(0))			# False
print(bool([]))			# False
print(bool(0.0))		# False
print(bool(None))		# False
print(bool(0j))			# False
print(bool(range(0)))	# False

Example: bool() on truthy values

print(bool(1))			# True
print(bool([0]))		# True
print(bool([1, 2]))		# True
print(bool(10))			# True
print(bool(3+4j))		# True
print(bool(range(2)))	# True