Please enable JavaScript to use CodeHS

Booleans and Logical Operators in Python

By Ryan Hart

As programmers, we often need to be able to control when specific lines of code are executed. A common way to do this is by using logic statements, also called boolean expressions. These expressions are a series of conditions that evaluate to True or False depending on the state of the conditions and the logic involved.

Let’s take a simple first example: the statement it is currently raining is either True or False. Evaluating this will determine if we need to grab an umbrella or not. In order to implement this sort of statement into our programs, we use a specific type of variable, called a boolean or bool for short.

Introducing booleans

A boolean is a type of variable that can hold the value of either True or False, and nothing else. Compare this to an integer variable that can hold any whole number, or a string variable that can hold any text.

Take a look at the example below where we use a boolean to implement the rainy situation from above.

  • Try changing the value of the boolean, is_raining and see what happens (remember, a boolean can only be one of two values)!
  • Notice there are NO quotes around the values and they have to be capitalized. This is really important, as we’ll see in future examples.

More complex boolean expressions

In many cases, our condition is as simple as the one above (if one condition is true, then perform one action), however, there are also many situations where there is more than one boolean variable involved and we need to evaluate them together.

Extending our current example, we know if it is raining we need to grab an umbrella… but

  • what if it is raining AND it is also cold? Both conditions are True, so we’ll need to grab a raincoat as well.
  • what if it is cold AND NOT raining? One condition is True AND one condition is NOT True. In this case, we will grab a sweatshirt instead of a raincoat.
  • what if it is sunny? We might actually still want to grab an umbrella to provide shade. So if we have rain OR sun we need the umbrella!

You can imagine that this can get quite complicated! The key here is understanding our three Logical Operators that create our boolean expressions:

and – evaluates to True only if both variables are True, and evaluates to False otherwise
or – evaluates to True if at least one of the variables is True, and evaluates to False if both variables are False
not – evaluates to True if the variable is False, and evaluates to False if the variable is True. Essentially it takes the opposite value

Python makes it super easy to use these operators in our code – we just use the lowercase words! Using them with the situations above, we’d get:

Raining and cold? –> Raincoat
Cold and not raining? –> Sweatshirt
Raining or sunny? –> Umbrella

Check out the program below to see this in action. Try changing the values of the booleans at the top and see how it affects the output!

Boolean logic practice

As you’re beginning to see, setting up and evaluating boolean expressions can range from very simple to rather complex, depending on the needs of your program. Let’s do some extra practice so you can perfect your skills!

The example below is set up with a series of initialized booleans. Before running the program, read through each boolean expression and try to determine if it will evaluate to True or False. Type your answers in the comment lines immediately below, then run it and see how you did! Hint: similar to order of operations in math, you’ll want to evaluate the expressions inside the parentheses first.


How’d you do?? For the next practice, take the below statements and write them as boolean expressions (this is NOT a runnable program, just a free response). You can create whatever boolean variables you need. I’ve done the first one for you.

  1. You drive to the ice cream shop if you have enough money, there is enough gas in the car, and you are over 16.
  2. A duck flies south for the winter if the temperature is not warm and it has energy to do so.
  3. To make pasta, you need to have white or wheat flour and fresh eggs.
  4. You will go build a snowman if you there is enough snow outside, the temperature is above 20F or you have a hat, and your friend will join you.