Comparison Operators
Learn how Python compares values using ==, !=, <, >, <=, and >= — and why the result is always True or False.
Imagine you're sorting a pile of cards. You pick two up and ask: Is this one bigger? Are they the same? Python does this all the time, and it uses comparison operators to answer those questions.
Every comparison gives back one of two answers: `True` or `False`. That's it. No maybes. These two values are called booleans, and they're the foundation of every decision your program will ever make.
See it in action
— step through the idea, then dive into the details below.Python Asks Yes-or-No Questions
A comparison operator puts two values side by side and asks a question. The answer is always `True` or `False` — no maybes.
#The Six Comparison Operators
Here are all six comparison operators at a glance:
==— equal to!=— not equal to<— less than>— greater than<=— less than or equal to>=— greater than or equal to
Each one sits between two values and evaluates to True or False.
print(5 == 5) # Are they the same?
print(5 != 3) # Are they different?
print(4 < 10) # Is 4 less than 10?
print(7 > 9) # Is 7 greater than 9?
print(6 <= 6) # Is 6 less than or equal to 6?
print(8 >= 5) # Is 8 greater than or equal to 5?#Comparing Numbers
Number comparisons work exactly as you'd expect from math class. You can compare integers, floats, or a mix of both.
temperature = 37.5
print(temperature > 36.6) # Above normal body temp?
print(temperature == 37.0) # Exactly 37.0?
print(temperature < 40.0) # Below dangerous level?#Comparing Strings
You can compare strings too. Equality checks are straightforward — two strings are equal only if every character matches exactly, including capitalisation.
print("apple" == "apple") # Exact match
print("Apple" == "apple") # Capital A matters!
print("cat" != "dog") # Different wordsYou can also use < and > on strings. Python compares them alphabetically (technically, by their character codes). Think of it like a dictionary: words that come earlier are "less than" words that come later.
print("apple" < "banana") # 'a' comes before 'b'
print("zebra" > "ant") # 'z' comes after 'a'
print("cat" < "cats") # shorter word comes firstThe Dictionary Shelf
Think of a bookshelf sorted alphabetically. "apple" sits before "banana", so "apple" < "banana" is True. Python walks through each letter one by one until it finds a difference — just like you would when filing cards.
#Chained Comparisons
In Python, you can chain multiple comparisons in one line. This reads almost like plain English and is great for checking whether a value falls within a range.
x = 7
print(1 < x < 10) # Is x between 1 and 10?
print(0 <= x <= 5) # Is x between 0 and 5 (inclusive)?Chaining saves you work
Instead of writing x > 1 and x < 10, you can write 1 < x < 10. Both mean the same thing, but the chained version looks more like the math you already know.
#== vs is: A Critical Difference
== checks whether two values are equal (same content). is checks whether two variables point to the exact same object in memory. For beginners, always use == when comparing values. is is for advanced identity checks — mostly used with None.
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # Same content?
print(a is b) # Same object in memory?When to use is
The most common correct use of is is if x is None:. For everything else — numbers, strings, lists — stick with ==.
#The = vs == Gotcha
Don't mix up = and ==
This is the most common beginner mistake:
=is assignment — it stores a value in a variable:x = 5==is comparison — it asks a question:x == 5
Writing if x = 5: is a syntax error in Python. If you see an unexpected error in a condition, check that you used == and not =.
score = 100 # assignment: store 100 in score
print(score == 100) # comparison: is score equal to 100?#Storing a Comparison Result
Because a comparison returns True or False, you can store that result in a variable just like any other value.
age = 20
is_adult = age >= 18
print(is_adult) # True
print(type(is_adult)) # The type is boolWhat does this code print? ```python x = 5 print(x == 5.0) ```
Key takeaways
- The six comparison operators (==, !=, <, >, <=, >=) always return True or False.
- String comparisons are case-sensitive and follow alphabetical (dictionary) order.
- You can chain comparisons like 1 < x < 10 to check ranges cleanly.
- == compares values; is compares identity — use == for almost everything.
- Never use a single = inside a condition; that's assignment, not comparison.
What does this code print?
x = 7
print(1 < x < 10)
print(0 <= x <= 5)This code has a bug. What is wrong?
score = 95
if score = 100:
print("Perfect score!")Complete the code so it stores whether age qualifies as an adult (18 or older) and prints True.
age = 20 is_adult = age 18 print(is_adult)
Put these lines in the right order to compare two strings and print the result.
print(result)
word2 = "banana"
result = word1 < word2
word1 = "apple"
Write a small program that stores your age in a variable called age. Then print three things: whether your age is exactly 18, whether your age is greater than 12, and whether your age is between 13 and 17 (inclusive) using a chained comparison.
Try it live — edit the code and hit Run to execute real Python: