Booleans

Python Booleans

Explore the concept of Python booleans, including how to create and use True and False values in variables. Understand how booleans can represent different states or conditions within data structures, and practice evaluating boolean expressions to control program flow.

Python booleans

Boolean is a fundamental data type in Python. A boolean value can be represented as either True or False.

We can set booleans to different variables to represent truthy or falsy values. For instance, let’s say you haven’t yet completed the boolean lesson. In that case, the value of our completed variable will be set to False.

completed_lesson = False print(completed_lesson)

Declaring a boolean variable in Python

By the end of this lesson, we can update it to True.

completed_lesson = True print(completed_lesson)

Updating a boolean variable in Python

Boolean variables can be added to depict different kinds of information in a class or object.

person_information = {"name": "X12", "age": 21, "python_developer": True} print(person_information["python_developer"])

Boolean attributes in Python

The person_information is a dictionary containing different information on a person, including the name, age and whether or not they’re a Python developer python_developer (i.e., a Boolean value).

Boolean type

Let’s try printing the type of the boolean values.

print(type(True)) print(type(False))

Printing a boolean's type in Python

On this page

    In This Module