Boolean

  • A Boolean value is either true or false.
  • A Boolean expression produces a Boolean value (true or false) when evaluated.

Relational operators:

  • Used to test the relationship between 2 variables, expressions, or values. These relational operators are used for comparisons and they evaluate to a Boolean value (true or false).

Ex. a == b evaluates to true if a and b are equal, otherwise evaluates to false

  • a == b (equals)
  • a != b (not equal to)
  • a > b (greater than)
  • a < b (less than)
  • a >= b (greater than or equal to)
  • a <= b (less than or equal to)

Example: The legal age to work in California is 14 years old. How would we write a Boolean expression to check if someone is at least 14 years old?

age >= 14

  • In most states, the minimum age to drive is 16. How would we write a boolean expression to check if someone is at least 16 years old?
    age >= 16
    
  • Write a boolean expression to check if the average of grade1, grade2, and grade3 is at least 70.
    (grade1 + grade2 + grade3)/3 >= 70
    
  • Write a code to check if the temperature is less than 90 degrees
    temp < 90
    

Logical operators:

Used to evaluate multiple conditions to produce a single Boolean value.

  • NOT evaluates to true if condition is false, otherwise evaluates to false
  • AND evaluates to true if both conditions are true, otherwise evaluates to false
  • OR evaluates to true if either condition is true or if both conditions are true, otherwise evaluates to false

Example: You win the game if you score at least 10 points and have 5 lives left or if you score at least 50 points and have more than 0 lives left. Write the Boolean expression for this scenario.

(score >= 10 AND lives == 5) OR (score == 50 AND lives > 0)

Example: Write a Boolean expression to check if the average of height1, height2, and height3 is at least 65 inches.

(height1 + height2 + height3) / 3 >= 65

Hacks

Review each of the sections above and produce …

  1. Look up De Morgan’s Law. What is it, and how can it apply to your code? Make a blog post about it.
  2. Create a truth table using Python (hint: These tables come up during the AP Exam). You will need to use libraries such as itertools. Here is an image of how the truth table should look:

Description of the image