Boolean Data Type in Python

This tutorial will teach you about the Python boolean data type, as well as falsy and truthy values.

In programming, you frequently want to check whether a condition is true or false and then take action based on the outcome.

Python provides the boolean data type to represent true and false. The technical name for a boolean value is bool.

True and False are the two values of the boolean data type.

It is worth noting that the boolean values True and False begin with capital letters (T) and (F).

The example below defines two boolean variables:

is_active = True
is_admin = False

Python returns a boolean value when two numbers are compared. As an example:

>>> 20 > 10
True
>>> 20 < 10
False

In addition, comparing two strings yields a boolean value:

>>> 'a' < 'b'
True
>>> 'a' > 'b'
False

 bool() function in Python

The bool() function is used to determine whether a value is True or False. As an example:

>>> bool('Hi')
True
>>> bool('')
False
>>> bool(100)
True
>>> bool(0)
False

As the output clearly shows, some values evaluate to True while others evaluate to False.

Falsy and Truthy values

It is true when a value evaluates to True. And a value that evaluates to False is false.

In Python, the following are false values:

The digit zero (0)
an empty string
False
None
An incomplete list []
A tuple that is empty ()
A dictionary with no entries
The truthy values are those that are not false.