Learn Without Walls
← Module 4 Home Lesson 1 of 4 Next Lesson →

Lesson 4.1: Comparison Operators

What You'll Learn

What Are Comparison Operators?

Comparison operators let you compare two values and get a Boolean result — either True or False. They are the foundation of decision-making in Python. Every time your program needs to choose what to do, it starts with a comparison.

Comparison Operator: A symbol that compares two values and returns True if the comparison holds, or False if it does not.

Think of comparisons as asking yes-or-no questions:

# Simple comparisons
print(5 > 3)
print(10 == 10)
print("hello" == "world")
True True False

The Six Comparison Operators

Python provides six comparison operators. Here they all are:

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
<Less than3 < 5True
>Greater than5 > 3True
<=Less than or equal to5 <= 5True
>=Greater than or equal to7 >= 10False

Real-World Example: Checking Age

age = 18

print("Is adult:", age >= 18)
print("Is teenager:", age >= 13 and age <= 19)
print("Is child:", age < 13)
Is adult: True Is teenager: True Is child: False
# More examples with variables
x = 10
y = 20

print(x == y)    # False - 10 is not equal to 20
print(x != y)    # True  - 10 is not equal to 20
print(x < y)     # True  - 10 is less than 20
print(x > y)     # False - 10 is not greater than 20
print(x <= 10)   # True  - 10 is less than or equal to 10
print(x >= 15)   # False - 10 is not greater than or equal to 15
False True True False True False

== vs = (A Very Common Mistake!)

One of the most common beginner mistakes is confusing = (assignment) with == (comparison). They look similar but do very different things:

= (single equals): Assignment — stores a value in a variable.
== (double equals): Comparison — checks if two values are equal.
# = is for assignment (storing a value)
score = 95         # Put 95 into the variable score

# == is for comparison (checking equality)
print(score == 95)  # Ask: "Is score equal to 95?"
print(score == 100) # Ask: "Is score equal to 100?"
True False

If you accidentally use = where you meant ==, Python will often give you a SyntaxError. This is actually helpful — it catches the mistake for you!

# This would cause a SyntaxError:
# if score = 95:   # WRONG - this is assignment, not comparison
# if score == 95:  # CORRECT - this is comparison

Comparing Strings

You can compare strings too! Python compares strings character by character using their Unicode values (essentially alphabetical order, with some rules).

# String equality
name = "Alice"
print(name == "Alice")    # True
print(name == "alice")    # False - case matters!
print(name != "Bob")      # True
True False True
Important: String comparison is case-sensitive. "Alice" and "alice" are NOT equal. To compare without caring about case, convert both strings to the same case first.
# Case-insensitive comparison
user_input = "YES"
print(user_input.lower() == "yes")  # True

# Alphabetical comparison
print("apple" < "banana")    # True - 'a' comes before 'b'
print("cat" < "dog")       # True - 'c' comes before 'd'
print("zebra" > "apple")   # True - 'z' comes after 'a'
True True True True

When comparing strings, uppercase letters come before lowercase letters. This means "Z" < "a" is True!

# Uppercase vs lowercase ordering
print("Z" < "a")    # True - uppercase letters come first
print("A" < "a")    # True
print(ord("A"))      # 65 - Unicode value of 'A'
print(ord("a"))      # 97 - Unicode value of 'a'
True True 65 97

Comparing Different Types

Python allows comparing integers and floats (they work as you would expect), but comparing unrelated types like strings and numbers may give unexpected results or errors.

# Comparing int and float works fine
print(5 == 5.0)      # True - same value
print(3 < 3.5)       # True
print(10.0 >= 10)    # True

# Comparing string and int with == returns False
print("5" == 5)      # False - different types!

# Comparing string and int with < causes an error
# print("5" < 5)      # TypeError!
True True True False

Common Gotcha: User Input Is Always a String

Remember, input() always returns a string. You need to convert it before comparing with numbers:

# If the user types "18", input() gives you the STRING "18"
age_str = "18"         # This is what input() returns

# This won't work as expected:
print(age_str >= 18)    # TypeError!

# Convert to int first:
age = int(age_str)
print(age >= 18)        # True - now it works!

Storing Comparison Results

Since comparisons return True or False, you can store the result in a variable. This is useful for making your code more readable.

# Store comparison results in variables
temperature = 35

is_hot = temperature > 30
is_freezing = temperature <= 0

print("Is it hot?", is_hot)
print("Is it freezing?", is_freezing)

# You can use these variables later in if statements
# (you'll learn about if statements in the next lesson!)
Is it hot? True Is it freezing? False
# Practical example: checking a password
stored_password = "python123"
entered_password = "Python123"

passwords_match = stored_password == entered_password
print("Access granted:", passwords_match)
Access granted: False

Try It Yourself!

Open Python and try these comparisons. Before running each one, predict whether it will be True or False:

print(7 > 7)
print(7 >= 7)
print("abc" == "ABC")
print(0 == 0.0)
print("" == " ")
print(1 != True)

Were any results surprising? The last one is tricky — Python treats True as 1 and False as 0!

Check Your Understanding

What will this code print?

x = 10
y = 20
print(x + y == 30)
print(x * 2 != y)
print("10" == x)
True   # 10 + 20 is 30, and 30 == 30 is True
False  # 10 * 2 is 20, and 20 != 20 is False
False  # "10" (string) is not equal to 10 (integer)

Key Takeaways

← Module 4 Home Lesson 1 of 4 Next: if, elif, else →