HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

9) Operators and Booleans Lesson

Python Boolean Operators

5 min to complete · By Martin Breuss

In the previous section, you asked Python whether an element is inside a collection, and Python diligently answered either with True when it was in there or False when it wasn't.

What are Booleans in Python

Python is very direct in that sense, and any time that you ask it a well-formulated question, it will answer with one of these two truth values. They are also called Boolean values and represent their own data type (bool) in Python.

Boolean Definition

This data type is the fundamental building block of all computer systems. The type bool stands for Boolean values, which can have one of two possible states:

  1. True
  2. False

True and False represent a binary concept. An electronic device is either ON or OFF. All or nothing. In digital computer terms, you can think of it as 1 or 0:

0s and 1s in green digital lettering

How to Use Boolean Logic

Booleans can either be assigned to variables as a result of their answer to a question or as a boolean value directly.

You've seen that Python likes to respond to you with these truth values. You can collect Python's answer and assign it to a variable.

Assign a Boolean Statement to a Variable

Think back to the substring example from your previous lesson. Instead of just letting Python respond, you'll collect its response and save it to a variable:

text = "Hello Coder!"
contains_hello = "Hello" in text
print(contains_hello)  # True

By using the assignment statement, you save Python's response to the question "Hello" in text in the variable contains_hello. Now, you can use the truth value of this response for other operations.

Boolean values come in handy when working with conditional statements, which you'll look at a little later in this course.

Assign a Boolean Value to a Variable

But you don't need to ask Python a question in order to get access to a Boolean value. You can also directly assign them to variables just like you would with any other values:

yes = True
no = False
print(yes)
print(no)

Assigning a Boolean value to a variable allows you to use its truth value for further operations. One common example is to specify a flag with a truth value. Your code can make decisions depending on the true value of that flag. Other parts of your code may change the flag when a specific condition occurs.

Colorful illustration of a light bulb

Note: Boolean values in Python are capitalized. You will run into an error if you accidentally write them in lowercase.

Allowing your code to make decisions based on a truth value, whether it comes as an answer from a question you asked Python or it's a Boolean value you defined yourself, is an effective way to build functionality into your programs.

You will learn more about how to do this in the next section of this course, where you'll practice working with conditionals and loops.

Colorful illustration of a light bulb

Additional Resources

Summary: Python Boolean Operators

  • Python boolean values can be either True or False and represent their own data type called bool
  • Boolean operators are essential building blocks of computer logic and useful to allow your programs to make decisions
  • Booleans can be stored to variables as a statement or directly