Skip to main content

Section 2.2 Boolean Expressions

45 minutes
Apps are a lot more fun if they can respond to different user actions and decisions. For example, if you are playing a game, the app can check whether you have enough points to move to the next level. A social media app needs to ask you for your password and check that it is correct before allowing you to post a comment. Apps use Boolean expressions to make decisions based on true or false conditions, like in the flowchart below. In this lesson, you will learn how to use Boolean expressions in your code to change an app’s behavior.

Subsection 2.2.1 Boolean Expressions and Decision Making

In Python, Boolean values are represented by the keywords True and False.

Activity 2.2.1.

Try to guess what the code below will print out before you run it. Change didHomework to True and run again.
Let’s think about the pizza web app again. It lets the user choose different options, and then chooses different outputs based on the user’s choices.

Activity 2.2.2.

Interact with the pizza app above. Which of the following are Boolean expressions (True or False) that the app uses to determine its output? Select all that apply.
  • User likes pizza
  • Yes, the app needs to check whether the user likes pizza to display it and pizza images in the output.
  • User’s name is 10 characters long
  • The app doesn’t care how long the user’s name is.
  • User’s age is not less than 0
  • Yes, the app checks that the age is not less than 0.
  • User’s name is empty
  • The app doesn’t care if the user name is empty. Try it and see!

Subsection 2.2.2 Relational Operators

Relational operators are operators like less than (<), greater than (>), and equal to (==) that compare two values or expressions. They are often used to compare a variable against a constant value or another variable or an expression. For example, the following Boolean expressions (which evaluate to true or false) can be used to see whether a number is positive or negative by seeing if it is greater than 0 or less than 0:
# Test if a number is positive (greater than 0)
number > 0
# Test if a number is negative (less than 0)
number < 0
The relational operators == and != (not equal) can be used to compare values for equality. They return True or False boolean values. In Python, they can be used to compare numbers, strings, and other data types. For string comparisons like "hello" != "Hello", Python will check that each character is the same (including its case) in the two strings. Don’t confuse = and ==. One = sign changes the value of a variable. Two == equal signs are used to test if a variable holds a certain value, without changing its value!
The relational operators < and > can be used to compare values and determine whether one is less than or greater than the other. If you have trouble telling < and > apart, remember that less than sign (<) looks more like an L and always starts with the small pointy-end. To remember the correct order of the two characters in <= and >=, just write them in the same order you would say them in English: β€œless than or equal to” not β€œequal to or less than”.

Activity 2.2.3.

Activity 2.2.4.

Try to guess what the code below will print out before you run it. Note that 1 equal sign (=) is used for assigning a value and 2 equal signs (==) for testing values. Then, add 3 more lines of code that sets credit to 15 and prints out whether cost is equal to credit and whether cost is not equal to credit.

Activity 2.2.5.

Try to guess what the code below will print out before you run it. Then, set cash to 15 and print the boolean expression for whether cost is less than or equal to cash.

Activity 2.2.6.

What is printed after the following code executes?
apples = 5
bananas = 2
oranges = 3
print(apples >= oranges + bananas)
  • False
  • No, apples is 5 and oranges + bananas is 5, so they are greater than or equal to each other.
  • True
  • Correct! apples is 5 and oranges + bananas is 5, so they are greater than or equal to each other.
  • 5
  • No, the expression evaluates to a Boolean value, True or False.
  • =
  • No, the expression evaluates to a Boolean value, True or False.

Subsection 2.2.3 Logical Operators

Logical operators like and, or, and not are used to combine Boolean expressions. For example, the following Boolean expression (which evaluates to true or false) can be used to see whether a number is between 1 and 10:
# Test if a number is between 1 and 10
number > 1 and number < 10

Activity 2.2.7.

Drag or click on the blocks you need to move them from the top section into the yellow area to create a boolean expression that tests for a number between -5 and 5 (including both). Number should be greater than or equal to -5 and less than or equal to 5. There are extra blocks that you don’t need.
The operator and is used to combine two things and verify that BOTH are true. The operator or is used to verify that at least one is true. The operator not is used to see if something is not true. With numerical values, the and operator is often used to see if a number is in an range, for example to get a B grade, your grade must be between 80 and 90 (not including 90).

Activity 2.2.8.

Try to guess what the code below will print out before you run it. Then, test for one more grade range using a logical operator.

Activity 2.2.9.

Given the code below, what are the values of x that will cause the code to print True?
print( x < 0 or x >= 10)
  • 0 to 10
  • No, because x has to be either negative or 10 or greater for the expression to be true.
  • 1 to 10
  • No, because x has to be either negative or 10 or greater for the expression to be true.
  • negative values
  • That is partially correct. But what values would be true because they made ``x >= 10`` true?
  • negative values or 2+ digits
  • Correct! x has to be either negative or 10 or greater for the expression to be true. Any number 10 or greater has at least 2 digits.
In English, we often use an exclusive-or like in the sentence β€œdo you want to be player 1 or player 2?” where you can’t be both player 1 and player 2. In programming, the or-operator is an inclusive-or which means that the whole expression is true if either one or the other or both conditions are true.
For complex logical expressions, it helps to first figure out the values of each boolean expression and then put it together with the logical operators. For example, try to figure out the logical expression ((age >= 12) and (age <= 18)) or (occupation == "student") in the code below.

Activity 2.2.10.

What is printed when you run this code? Try the CodeLens button to step through the code. Then, change the value of age to 19, try to guess the result, and run to check. Change the value of occupation and run again.

Subsection 2.2.4 Unplugged Boolean Logic Project

Do the following project in a group of 4 people. If you don’t have a group, imagine that you are doing this activity with 3 other friends or family members and write down their names and ages. (Thank you to Jill Westerlund of Hoover High School and Art Lopez of Sweetwater High School for this activity suggestion).

Project 2.2.11.

  1. Draw or print a Venn diagram of 4 intersecting circles.
  2. Put each of the names of the 4 people in your group in a circle of the Venn Diagram (one name in each circle).
  3. Write down the age of each person in your group in the circles. If two or more people are the same age, put the age in the intersecting parts of their circles.
  4. Write a Boolean expression that compares the age of each person in the group using ==, <, >, and, for example Ada’s age > Alan’s age and Alan’s age == Grace’s age.
  5. Then, ask each person in your group their favorite food. If two or more people have the same favorite food, put the food in the intersecting parts of their circles.
  6. Write a Boolean expression that compares the favorite foods in the group using ==, !=, and and, for example Ada’s favorite food pizza == Alan’s favorite food pizza and Alan’s favorite food != Grace’s favorite food.
  7. Think of 1 more comparison and write it in the circles and as a Boolean expression.
  8. Share the Boolean expressions with the class and type them in below.

Subsection 2.2.5 Vocabulary Review

Activity 2.2.12.

Subsection 2.2.6 App Inventor Project Continued (Optional)

If your class started the Lights Off app in MIT’s App Inventor, you can continue to do section 2.14.3 Iteration 2 of your project in the App InventorLights Off App Lesson. The rest of the sections can be completed after the next lesson.
You have attempted of activities on this page.