Python bool Data Type (True or False Boolean Value)

This page explains the bool (boolean – True/False) data type in Python, its basic characteristics, typical use cases, and common operations.

Basic characteristics

The name bool is short for boolean, meaning true/false.

There are only two possible values of the bool type: True or False.

True and False are case-sensitive and start with uppercase letter. True (with capital T) is different from true (woth lowercase t), which is not a bool and can be used freely as a variable or function name (though not recommended for code readability).

True and False are not strings, so we don't write quotes around them. True is a bool, but "True" (with quotes) is just a string.

The bool data type is built-in and you don't need to import any module to use it.

Typical use cases

Like in other programming languages, the bool data type is mainly used for program logic – to control the flow of execution based on different variables being true or false.

A typical use of bool is in conditional statements: if a variable is True, do something, else (if it is False) do something else.

Which variables are True/False

The following variables are True in Python:

  • Any number other than zero (positive or negative int, float)
  • Non-empty string (even '0' is True).
  • Non-empty sequence (e.g. a list, tuple, set, or dict with at least one item)

Conversely, these are False:

  • Zero int or float
  • The number zero in another numeric type (e.g. Decimal(0), Fraction(0, 1))
  • Empty string ""
  • Empty sequence (e.g. a list, tuple, set, or dict with no items)
  • The None constant

How to create a True/False variable

Literal assignment

The simplest way to create a bool variable is to just assign it to True or False.

a = True
b = False

Remember that the first letter (T/F) is uppercase and there are no quotes (otherwise it is a string).

Comparison operators

We can also assign a variable to be the outcome of a comparison operator. For example:

limit = 10
a = 8
b = 11
a_above_limit = a > limit
b_above_limit = b > limit

In the above code, limit, a, b are integers, while a_above_limit and b_above_limit are of the bool data type. a_above_limit is False, because 8 is not greater than 10. b_above_limit is True.

Item in a sequence

Besides comparison operators, we can check whether a value is in a list, tuple, or another sequence. For example:

cities_visited = ['London', 'Paris', 'Rome', 'Berlin']
city = 'Prague'
if city in cities_visited:
    print("I have been to " + city)
else:
    print("I must visit " + city)

In the above code, cities_visited is a list of strings (city names), city is a string (a city name which may or may not be in the list). The entire condition city in cities_visited after if is in fact a bool. In this case for 'Prague' it is False, because Prague is not in the list of visited cities. For 'London' or 'Rome' it would be True.

This piece of code is simplistic, as it only prints "I have been to" / "I must visit" + city name based on the condition, but if we replace the prints with another code, we can create a program which does something for a city already visited (such as find our photos) and something else if not (such as plan itinerary).

Python bool() function

Another way to create a bool variable or to check whether a variable is True or False is the Python bool() function. For example:

>>> bool(123)
True
>>> bool(0)
False
>>> bool('Hello')
True
>>> bool('')
False

Official documentation

The bool data type:
https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values

The bool() function:
https://docs.python.org/3/library/functions.html#bool

Truth value testing:
https://docs.python.org/3/library/stdtypes.html#truth

By remaining on this website or using its content, you confirm that you have read and agree with the Terms of Use Agreement.

We are not liable for any damages resulting from using this website. Any information may be inaccurate or incomplete. See full Limitation of Liability.

Content may include affiliate links, which means we may earn commission if you buy on the linked website. See full Affiliate and Referral Disclosure.

We use cookies and similar technology to improve user experience and analyze traffic. See full Cookie Policy.

See also Privacy Policy on how we collect and handle user data.

© 2024 PyTut