Skip to Content
Nextra 4.0 is released 🎉
VariablesTypes of VariablesPrimitive/Basic TypesBooleans

Booleans

What is a boolean?

A boolean is a data type that can hold one of two values: True or False.

In Python, booleans are often used to represent whether a condition is true. (We’ll learn more about conditions and how to use them later) For example:

is_raining = True has_umbrella = False

In this example, is_raining is a boolean variable that indicates whether it is raining, and has_umbrella indicates whether you have an umbrella.

It’s raining and we don’t have an umbrella!

Creating boolean variables

You can create a boolean variable by assigning it either True or False. Here are some examples:

# Example of boolean variables is_sunny = True is_weekend = False

You can also create boolean variables based on comparisons. For example:

# Example of boolean variables based on comparisons age = 20 is_adult = age >= 18 # is_adult will be True because 20 is greater than 18 is_teenager = age < 18 # is_teenager will be False because 20 is not less than 18

Using boolean variables

Refer to the section on Control Flow later in this course to learn how to use boolean variables in your code to make decisions and control the flow of your program.

For now just remember that boolean variables can only hold the values True or False, and they are often used to represent conditions in your code.

Last updated on