Python Relational Operators: A Beginner’s Guide to Comparisons

Have you ever wondered how a Python program makes decisions? How does it know if a user’s password is correct, if a game character has enough health, or if a number is within a valid range? The answer lies in a fundamental programming concept: comparison.

At the heart of every “if” statement is a simple question: is this greater than that? Are these two values equal? Python uses special symbols called relational operators to ask these questions and decide what to do next. If you’re learning to code, mastering these operators is a crucial step toward writing dynamic and intelligent programs.

In this guide, you’ll learn exactly what Python relational operators are, how they work, and how to use them in your own code with clear, practical examples. By the end, you’ll be confidently comparing values like a pro.

What Are Python Relational Operators?

In simple terms, relational operators are used to check the relationship between two values (known as “operands”). They ask a “yes or no” question about how these two values relate to each other.

The most important thing to remember is that every relational operation returns a Boolean value. This means the answer is always either True or False. This True or False result is what allows your program to choose its path, like a fork in the road.

Think of it like a simple question: “Is 10 greater than 5?” The answer is a clear “yes,” or in Python terms, True.

The Six Essential Relational Operators

Let’s break down each of the six relational operators one by one. We’ll use the Python shell for our examples, which provides immediate feedback—perfect for learning!

1. Greater Than (>)

The greater than operator checks if the value on the left is larger than the value on the right.

# Is 5 greater than 3?
print(5 > 3)

Output:

True

# Is 5 greater than 6?
print(5 > 6)

Output:

False

2. Less Than (<)

The less than operator checks if the value on the left is smaller than the value on the right.

# Is 5 less than 10?
print(5 < 10)

Output:

True

# Is 5 less than 3?
print(5 < 3)

Output:

False

3. Equal To (==)

This is a crucial one! The equality operator (==) checks if two values are the same.

Heads-up: It’s easy to confuse this with the single equals sign (=), which is the assignment operator used to assign a value to a variable.

# Is 5 equal to 5?
print(5 == 5)

Output:

True
# Is 5 equal to 6?
print(5 == 6)

Output:

False

4. Not Equal To (!=)

This operator is the opposite of the equality operator. It checks if two values are not the same.

# Is 5 not equal to 6?
print(5 != 6)

Output:

True
# Is 5 not equal to 5?
print(5 != 5)

Output:

False

5. Greater Than or Equal To (>=)

This operator combines two checks. It returns True if the left value is either greater than OR equal to the right value. Only one of the conditions needs to be true.

# Is 5 greater than OR equal to 3? (It's greater than)
print(5 >= 3)

Output:

True
# Is 5 greater than OR equal to 5? (It's equal to)
print(5 >= 5)

Output:

True
# Is 5 greater than OR equal to 6? (It's neither)
print(5 >= 6)

Output:

False

6. Less Than or Equal To (<=)

Similar to the previous operator, this checks if the left value is either less than OR equal to the right value.

# Is 5 less than OR equal to 10? (It's less than)
print(5 <= 10)

Output:

True
# Is 5 less than OR equal to 5? (It's equal to)
print(5 <= 5)

Output:

True
# Is 5 less than OR equal to 4? (It's neither)
print(5 <= 4)

Output:

False

Confirming the Boolean Result

As we mentioned, all relational operators return a Boolean value (True or False). We can prove this using Python’s type() function.

# Check the type of the result from a relational operation
result = (5 > 3)
print(result)        # This will print: True
print(type(result)) # This will print: <class 'bool'>

When you run type(5 > 6), Python first evaluates 5 > 6 to False, and then checks the type of False, confirming it is a Boolean (bool).

Ready to Go from Basics to Pro?

You’ve just taken a vital step in your Python journey by learning how to make comparisons in your code. But this is just the beginning. Relational operators are the building blocks for more complex logic using if statements, loops, and data validation. To truly master Python, you need a structured path that guides you from these fundamentals to building real-world applications.

If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.

Conclusion

Python relational operators are your key to creating programs that can make decisions. You learned about the six core operators: > (greater than), < (less than), == (equal to), != (not equal to), >= (greater than or equal to), and <= (less than or equal to). Remember, every time you use one, you are asking a simple True/False question that directs the flow of your code. The best way to solidify this knowledge is to practice. Open a Python shell and try comparing different numbers and even strings! This hands-on experience is invaluable.

Common Questions About Relational Operators

What’s the difference between = and == in Python?
The single equals sign (=) is the assignment operator. It’s used to assign a value to a variable (e.g., age = 25). The double equals sign (==) is the equality operator. It’s used to compare two values to see if they are equal (e.g., age == 25).

Can I compare data types other than numbers with relational operators?
Yes! You can also compare strings. Python compares strings lexicographically (based on the ASCII/Unicode values of the characters). For example, "apple" < "banana" would return True.

Do relational operators only work with two numbers?
While we focused on numbers for simplicity, relational operators can work with other compatible data types, like checking if two variables point to the same object with is, but for beginners, starting with numbers and strings is the perfect foundation.