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

Essential Knowledge 2.2.1 2.2.A.1.

Apps often check conditions that have only two outcomes, such as whether a user is logged in, whether a checkbox is selected, or whether a password is correct.

Essential Knowledge 2.2.2 2.2.A.2.

Programs make decisions by evaluating conditions that result in a Boolean value. A Boolean value has two possible values: true or false.
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.

Essential Knowledge 2.2.3 2.2.A.3.

Apps can use Boolean expressions to decide what happens next, such as showing a message, allowing access, or changing what a user sees. A Boolean expression is an expression that evaluates to either true or false.

Essential Knowledge 2.2.4 2.2.A.4.

Many Boolean expressions use comparisons, such as checking whether a score is high enough or whether an input meets a requirement like password length.
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? Try different input to see if the app checks for these conditions. 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

Essential Knowledge 2.2.5 2.2.B.1.

Apps and programs often compare values to enforce rules, personalize experiences, and prevent mistakes. Relational operators allow programs to evaluate questions such as whether a value is too low or whether one value matches another.
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

Essential Knowledge 2.2.6 2.2.B.2.

Relational operators compare two values and produce a Boolean result.
  • Equals (==): The expression a == b evaluates to true when the values of a and b are equal, and evaluates to false otherwise.
  • Greater than (>): The expression a > b evaluates to true when the value of a is greater than the value of b, and evaluates to false otherwise.
  • Less than (<): The expression a < b evaluates to true when the value of a is less than the value of b, and evaluates to false otherwise.
  • Greater than or equal to (>=): The expression a >= b evaluates to true when the value of a is greater than or equal to the value of b, and evaluates to false otherwise.
  • Less than or equal to (<=): The expression a <= b evaluates to true when the value of a is less than or equal to the value of b, and evaluates to false otherwise.
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.

Essential Knowledge 2.2.7 2.2.B.3.

The result of a relational expression can be determined by substituting the current values, including variable values, and evaluating whether the comparison is true or false.

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

Essential Knowledge 2.2.8 2.2.C.1.

Apps and programs often combine multiple conditions to make decisions that feel predictable and personalized. Logical operators allow programs to answer questions such as whether all conditions are true, whether at least one condition is true, or whether a condition is not true. Logical operators can also support user-centered rules, such as allowing an action only when multiple conditions are met, like when all required fields are complete and the user agrees to terms.

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.

Essential Knowledge 2.2.9 2.2.C.2.

Logical operators combine or modify Boolean values and produce a Boolean result.
  • The expression condition1 and condition2 evaluates to true when both conditions are true, and evaluates to false otherwise.
  • The expression condition1 or condition2 evaluates to true when at least one condition is true, and evaluates to false when both conditions are false.
  • The expression not condition evaluates to true when the condition is false, and evaluates to false when the condition is true.
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.

Essential Knowledge 2.2.10 2.2.C.3.

The result of a logical expression can be determined by evaluating each condition and then applying not, and, and or to determine whether the overall expression is true or false. Logical expressions can include multiple conditions. Parentheses can be used to group conditions and make the intended order of evaluation clear when combining not, and, and or.
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 Learning Objectives Review

After completing this lesson, you should be able to:

Learning Objective 2.2.11 2.2.A.

Identify the use of Boolean expressions to control an app’s behavior as you interact with the app.

Learning Objective 2.2.12 2.2.B.

Develop code using relational operators, and determine the result of relational expressions.

Learning Objective 2.2.13 2.2.C.

Develop code using logical operators, and determine the result of logical expressions.

Subsection 2.2.6 Vocabulary Review

Activity 2.2.12.

Subsection 2.2.7 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.