Lesson 13 of 4033%

(Progress persistence is disabled until Phase 9)

Python
Beginner

Comparison & Logical Operators

Combine and compare multiple conditions.

Comparison and Logical Operators

What You’ll Learn

You’ll learn how to compare values and combine multiple conditions together.

Comparison Operators

These compare two values and return True or False:

  • == Equal to (5 == 5 is True)
  • != Not equal to (5 != 3 is True)
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to

Logical Operators

These combine multiple comparison statements:

1. and

Returns True if both statements are true.

age = 25
has_license = True

if age >= 18 and has_license:
    print("You can rent a car.")

2. or

Returns True if at least one of the statements is true.

day = "Saturday"

if day == "Saturday" or day == "Sunday":
    print("It's the weekend!")

3. not

Reverses the result (returns False if the result is true).

is_raining = False

if not is_raining:
    print("Let's go for a walk.")

Common Mistakes

  • Confusing = and ==: A single = is for assignment (setting a variable). A double == is for comparison (checking if two things are equal).

Lesson Progress

Take QuizBuild a Python Project