Chapter 4, Lesson 3 Text URL

Chapter Four: Making Decisions

Lesson Three: Logical Operators


You can now write simple logical expressions like "score > 100" or "cost <= 120". But as you can imagine, a program may need to ask more complex questions such as "is a person's age both greater than 14 AND less than 21?". Python allows you to build longer logical expressions by joining together two or more shorter expressions. The resulting True or False can then be used normally in an "if" statement or stored in a Boolean variable.

Nested "if" Statements

Consider the question "Is your pet a dog named Chewy?" How might you write this question in Python code? There are actually two parts:

  • Is your pet a dog?
  • Is your dog named Chewy?

If we have stored the type of pet in a variable named pet and the pet's name in a variable called name, then we could write logical expressions as follows:

  • pet == "dog"
  • name == "Chewy"

The example below shows one possible way to run a block of code when both logical expressions are True. We use nested "if" statements, meaning one "if" statement is placed inside another "if" statement. That second "if" statement will only run if the first "if" expression is True. The code indented inside the second "if" statement will only run when both expressions are True.

Try It Now

When you run this example, you should see "This pet is a dog named Chewy" because both logical expressions are True. But if you change the pet variable to something other than "dog" or the name to something other than "Chewy", you will see a different output, because one or both logical expressions are now False.

Combining Expressions with Logical Operators

It is possible to join two or more logical expressions in a single "if" statement. This may simplify your code and remove the need for nested "if" logic in some cases. Logical operators are key words that join or modify logical expressions in certain ways.

Logical Operators: "and"

You may use the logical operator "and" to join two or more logical expressions. The resulting larger expression still evaluates to True or False.  When using the and operator, if both logical expressions are True then the result is True.  If either (or both) of the logical expressions are False then the overall result is False.

The example below uses "and" to join the logical expressions pet == "dog" and name == "Chewy". If both expressions are True, then the indented block of code will run, otherwise the "else" case will run. Try it and see!

Try It Now

If you change the initial value of pet or name to anything other than "dog" and "Chewy", you will get different results, because one or both logical expressions are False.

Logical Operators: "or"

The "or" logical operator works very much the same way as "and", with one important difference. "or" will produce a True result if either one of the two logical expressions are True. You will get a False result only when both logical expressions are False.

We've changed our example to use "or" instead of "and". We also changed the initial name to "Wolife" to make the second logical expression False. What happens when you run the code below; do you get the expected results?

Try It Now

Try using different input pet and name values. You should see "or" produce True results when either one or bothlogical expressions is True. If you make both expression False, you will see the message from the "else" case.

Logical Operators: "not"

Both "and" and "or" operators will join two logical expressions. The "not" operator, by contrast, will work on one logical expression. By placing "not" in front of an expression, you change the resulting Boolean value from False to True or True to False.

You could use "not" to change the result of one simple expression as shown below.

not pet == "dog"

However, you could use the "not equals" comparison operator to do exactly the same thing:

pet != "dog"

Both expressions will return True if the pet is not a "dog" and will return False if the pet is equal to a "dog".

"not" becomes more useful if you want to flip the result of a longer expression. Consider the example below.

not (pet == "dog" and name == "Chewy")

The "and" operator will first return True if both logical expressions are True. The "not" operator will then take that result and flip it. So, if the value of pet is "dog" and name is "Chewy", the results from "and" are True, but the overall result due to the "not" operator is False. If either one of the logical expressions is False, the result from "and" is False, and the overall result will be flipped to True.

Try It Now

Try changing the initial values for pet and name, and make sure you understand the resulting output from the logical expression. As you combine more operators together to form more complex expressions, the code can be harder to read and understand. So, take your time and study the results carefully!


Work With Me: Extra Lives


You are responsible for writing a small piece of logic within a computer game. Players will start with 3 lives. The logic will decide to award an extra life to a player under either of the following conditions:

  • The player's score is greater than 100 and the player's health is greater than 50
  • The player's score is greater than 1000 or the current number of powerups is greater than 0

The code below will prompt you for an initial value to store in the scorehealth and powerupsvariables. Then, a series of if / elif statements will test for each of the above conditions. When a condition is met, the number of lives will be increased by 1. When finished, the program will print the final number of lives to the screen.

Can you fill in the logical expressions (marked by ????) for each if / elif statement to meet the conditions described above?

Try It Now



When finished, test your logic by running the program 4 times and entering the sets of data shown below. Do you get the expected output in each case?

Trigger first condition with score = 150 and health = 75:

What is the score? 150
What is the health? 75
How many powerups? 0
First condition met
Final number of lives: 4

Trigger second condition with score = 2000:

What is the score? 2000
What is the health? 25
How many powerups? 0
Second condition met
Final number of lives: 4

Trigger second condition with powerups = 1:

What is the score? 50
What is the health? 100
How many powerups? 1
Second condition met
Final number of lives: 4

No logical expressions return true; number of lives unchanged.

What is the score? 50
What is the health? 100
How many powerups? 0
Final number of lives: 3

End of Lesson

Last modified: Wednesday, 8 May 2019, 8:38 AM