Blog Post #22: Making Comparisons: How to Use Comparison Operators (==, !=, <, >)

In Post #21, we established that Boolean expressions are the “questions” our programs ask to get a True or False answer. Now, it’s time to learn the language for asking those questions. How do we ask if two values are the same, or if one is larger than the other?

This is where comparison operators come in. In this post, we will explore the set of operators Python uses to compare values, which form the most common type of Boolean expression.

The Most Important Distinction: == vs. =

Before we go any further, we must clarify the single most common point of confusion for new programmers: the difference between a single equals sign (=) and a double equals sign (==).

  • = (Single Equals): The Assignment OperatorAs we’ve seen since Post #8, a single equals sign is for assignment. The statement score = 100 is a command that means, “Put the value 100 into the variable named score.”
  • == (Double Equals): The Equality OperatorA double equals sign is for comparison. The expression score == 100 is a question that asks, “Is the value currently in the variable score equal to 100?” The answer to this question will always be True or False.

Let’s see this in action:

# ASSIGNMENT: my_value now holds the value 10
my_value = 10

# COMPARISON: Ask if the value in my_value is equal to 10
is_equal_to_10 = (my_value == 10)
print(f"Is my_value equal to 10? {is_equal_to_10}")

# COMPARISON: Ask if the value in my_value is equal to 99
is_equal_to_99 = (my_value == 99)
print(f"Is my_value equal to 99? {is_equal_to_99}")

The output will be:

Is my_value equal to 10? True
Is my_value equal to 99? False

The Six Main Comparison Operators

With that crucial distinction made, let’s look at the full set of tools for comparing values.

Not Equal (!=)

Asks the question: “Is the value on the left not equal to the value on the right?”

print(f"Is 10 not equal to 5? {10 != 5}")
print(f"Is 10 not equal to 10? {10 != 10}")

Output: True, False

Greater Than (>)

Asks the question: “Is the value on the left greater than the value on the right?”

print(f"Is 10 greater than 5? {10 > 5}")

Output: True

Less Than (<)

Asks the question: “Is the value on the left less than the value on the right?”

print(f"Is 10 less than 5? {10 < 5}")

Output: False

Greater Than or Equal To (>=)

Asks the question: “Is the value on the left greater than or equal to the value on the right?”

print(f"Is 5 greater than or equal to 5? {5 >= 5}")
print(f"Is 10 greater than or equal to 5? {10 >= 5}")

Output: True, True

Less Than or Equal To (<=)

Asks the question: “Is the value on the left less than or equal to the value on the right?”

print(f"Is 10 less than or equal to 5? {10 <= 5}")
print(f"Is 5 less than or equal to 5? {5 <= 5}")

Output: False, True

Comparing More Than Just Numbers

These operators don’t just work on numbers; they work on strings as well. Python compares strings based on their lexicographical order, which is a fancy term for something very similar to alphabetical order.

print(f"Is 'apple' < 'banana'? {'apple' < 'banana'}")
print(f"Is 'cat' > 'dog'? {'cat' > 'dog'}")

Output: True, False

The comparison is case-sensitive. In Python’s character ordering, all uppercase letters come before all lowercase letters. This means 'A' is considered “less than” 'a'.

print(f"Is 'Apple' == 'apple'? {'Apple' == 'apple'}")
print(f"Is 'Apple' < 'apple'? {'Apple' < 'apple'}")

Output: False, True

What’s Next?

You can now use comparison operators like ==, !=, >, and < to ask questions about how two values relate to each other. These expressions are the most common way to generate the True and False values that drive program logic.

Asking a single question is powerful, but what if you need to check multiple conditions at once? For example, “is the user logged in and do they have admin privileges?” To combine multiple boolean expressions, we need a new set of tools. In Post #23, we will explore the logical operators: and, or, and not.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment