Python Comparison Operators (==, !=, >)

Suppose you are in a theme park, and you want to ride a ride called "Python Coaster". There is a board outside the ride:

"Your height must be at least 140cm for this ride."

Now you think - "Am I 140cm or above?"

If yes, you can ride. If not, you have to sit outside.

The same thing happens in programming. When we do programming, such questions are asked very often:

  • Is the user's password correct?
  • Is the player out of health?
  • Is the product in stock?

Python uses Comparison Operators to ask such questions. And the answer is always simple - True or False.

What are Comparison Operators?

Comparison operators are symbols that allow you to compare two values. When you compare, the result is always a Boolean value True or False.

These operators are the basis for making decisions in your code.

A Practical Example

Now let's take an example:

my_age = 25
required_age = 18

Now look below, what the different comparison operators do:

OperatorNameExampleResultWhat does it mean? (What is the question asking?)
==Equal tomy_age == required_ageFalseAre my_age and required_age the same?
!=Not equal tomy_age != required_ageTrueAre my_age and required_age different?
>Greater thanmy_age > required_ageTrueIs my_age greater than required_age?
<Less thanmy_age < required_ageFalseIs my_age less than required_age?
>=Greater than or equal tomy_age >= required_ageTrueIs my_age greater than or equal to required_age?
<=Less than or equal tomy_age <= required_ageFalseIs my_age less than or equal to required_age?

Most Common Beginner Mistake: The Difference Between = and ==

This is one of the most important things to learn at the beginning. If you mix up = and ==, you can have bugs in your program that are very hard to catch.

= (Single Equal Sign): Assignment

This is used for assignment. Meaning - "put this value into this variable." It is a kind of command.

Example:

my_age = 25

It means: put the value 25 into a variable named my_age.

== (Double Equal Sign): Comparison

This is used for comparison. Meaning - "Are these two values the same?" It is a question that can be answered with either True or False.

Example:

print(my_age == 25)

It means: "Is the value in my_age equal to 25?" If yes, Python will print True.

Comparing More Than Just Numbers: Strings

Comparison operators (such as ==, <, >) work not just for numbers, but also for other data types, such as strings (text).

When you compare two strings in Python, Python checks each character one by one — in alphabetical order.

Example:

print("apple" == "apple")   # True, they are exactly the same
print("apple" == "Apple")   # False, because 'a' and 'A' are different (Python is case-sensitive)
print("apple" < "banana")   # True, because 'a' comes before 'b' in the alphabet

This is the method Python uses when you want to sort a list of names.

Frequently Asked Questions (FAQs)

Q: What happens if I compare a number to a string, such as 5 == "5"?

A: The answer is always False. When Python looks at 5 and "5", it sees an integer on one side and a string on the other. To us humans, they may look the same, but to Python, the data types are different — so they are not equal.

Q: Can I do multiple comparisons in Python?

A: Yes. In Python, you can chain comparisons together — and this is a very easy and efficient way to do this that many other languages don't offer.

Example:

age = 22

# Python's smart way of checking if age is between 18 and 30
print(18 < age < 30)  # Prints True, because 22 is between 18 and 30.

In this, Python automatically understands that age must satisfy both conditions (be greater than 18 AND less than 30).

Q: What is the opposite of > (greater than)?

A: The opposite of > is <= (less than or equal to). If a number is not greater than another number, then it must be either smaller than it or equal to it. The <= operator represents this combined condition.

Next Lesson: Logical Operators (and, or, not) in Python

Scroll to Top