Comparing values in Python: a deep dive into relational operators

·

3 min read

Relational operators are used to compare two values and determine whether the relationship between them is true or false. These operations are useful in control flow, such as conditional statements. Since they compare two values, relational operators are also called comparison operators.

In Python, we have access to six different relational operators:

  • <

  • >

  • <=

  • >=

  • ==

  • !=

< operator

The < operator is probably an operator you are already familiar with. It is widely used in basic mathematics and serves the same purpose in programming. The < operator returns True if the left operand is less than the right operand:

x = 3
y = 5
print(x < y) # Output => True

> operator

The > operator is probably an operator you are already familiar with. It is widely used in basic mathematics and serves the same purpose in programming. The > operator returns True if the left operand is greater than the right operand:

x = 5
y = 3
print(x > y) # Output => True

<= operator

The <= operator returns True if the left operand is less than or equal to the right operand. Unlike the < operator, the <= operator returns True if the operands have the same value:

a = 5
b = 7
print(a <= b) # Output => True
c = 4
d = 4
print(c <= d) # Output => True

>= operator

The >= operator returns True if the left operand is greater than or equal to the right operand. Unlike the > operator, the >= operator returns True if the operands have the same value:

a = 7
b = 5
print(a >= b) # Output => True
c = 4
d = 4
print(c >= d) # Output => True

== operator

The == operator returns True if the left operand is equal to the right operand. In basic mathematics, this purpose is served by the = operator. However, in programming the = represents the assignment operator and is used to assign a value to a variable. At first, it could be confusing, but with time you will learn to distinguish the uses of the == and the = operators.

x = 3
y = 3
print(x == y) # Output => True

!= operator

The != operator returns True if the left operand is not equal to the right operand:

x = 7
y = 10
print(x != y) # Output => True

Conditional statements and relational operators

As I have written before, relational operators are used to compare two values. The feature of returning either True or False makes them useful in control flow. Relational operators are often placed in the condition that determines the execution of the program.

Let's see an example with conditional statements:

age = 12
if age >= 18:
    print("Access allowed")
else:
    print("Access denied")
# Output => Access denied

user_name = "John"
if user_name == "John":
    print("Welcome, John")
else:
    print("Unknown user, please retry")
# Output => Welcome, John

In the examples above, the conditions are evaluated through relational operators. In the first example, the return value of the condition is False, therefore the code in the else block is executed. In the second example, the return value of the condition is True, therefore the code in the if block is executed.

Conclusion

In conclusion, relational operators are an important part of programming in Python. They allow us to compare values and make decisions based on the relationship between them.

Remember to be mindful of the specific details and syntax involved in using relational operators, as even small mistakes can lead to unexpected results. With practice, however, you will become more comfortable with using these operators and be able to integrate them into your code with ease.