Lesson 1.5

Boolean Expressions and Operators

Agenda

  • Due today:
    • Python Essentials Module 1 Quiz
      • if you have joined the class (via invitation sent to your personal email) then I should be able to see your score
    • Assignment 1.5

What is a Boolean?

  • A Boolean value is either:
    • True
    • False
  • Useful for making decisions in programs

Boolean Expressions

  • Expressions that evaluate to True or False
  • Examples:
    • 5 > 3 → True
    • 10 == 7 → False
    • 2 + 2 == 4 → True

Comparison Operators

  • == equal to
  • != not equal to
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to

Quick Check

What does each evaluate to?

  • 7 > 10
  • 3 != 2
  • 4 + 1 == 5

Boolean Operators

  • Combine comparisons with:
    • and (both must be True)
    • or (at least one must be True)
    • not (flips True/False)

Example

age = 16
print(age > 14 and age < 18)   # True
print(age > 18 or age == 16)   # True
print(not(age == 16))          # False

Practice Together

  • Predict the result, then run it:
print(3 < 5 and 10 == 10)
print(7 != 7 or 2 > 1)
print(not(4 <= 4))

Why Booleans Matter

  • Used for making choices in code
  • Foundation for if statements (next lesson)

Your Turn

Write a boolean expression that:

  • Checks if a number is greater than 100
  • Checks if two numbers are equal
  • Checks if a number is not zero