Python Relational Operator

As the name suggests, relational operators help establish relationships between values. These are also known as comparison operators because they enable us to compare two values and verify the validity of the relationship. If the relationship holds true, Python returns True; otherwise, it returns False.

Relational operators are not new to us. We have encountered symbols like > (greater than) and < (less than) since our school days. In Python, these operators come in six different types:

Python Relational Operator
Python Relational Operator

1. Equal To (==)

The == operator checks if the left value is equal to the right value. If they are equal, it returns True; otherwise, it returns False.

Example:

print(6 == 6)  # Output: True
print(7 == 6)  # Output: False
Python

In the first example, 6 is equal to 6, so the result is True. In the second example, 7 is not equal to 6, so the result is False.

2. Not Equal To (!=)

The != operator checks if the left value is not equal to the right value. If they are not equal, it returns True; otherwise, it returns False.

Example:

print(6 != 6)  # Output: False
print(7 != 6)  # Output: True
Python

In the first example, 6 is equal to 6, so the result is False. In the second example, 7 is not equal to 6, so the result is True.

3. Greater Than (>)

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

Example:

print(8 > 6)  # Output: True
print(7 > 8)  # Output: False
Python

In the first example, 8 is greater than 6, so the result is True. In the second example, 7 is not greater than 8, so the result is False.

4. Less Than (<)

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

Example:

print(6 < 8)  # Output: True
print(6 < 6)  # Output: False
Python

In the first example, 6 is less than 8, so the result is True. In the second example, 6 is not less than 6, so the result is False.

5. Greater Than or Equal To (>=)

The >= operator checks if the left value is greater than or equal to the right value.

Example:

print(6 >= 6)  # Output: True
print(7 >= 8)  # Output: False
Python

In the first example, 6 is equal to 6, so the result is True. In the second example, 7 is not greater than or equal to 8, so the result is False.

6. Less Than or Equal To (<=)

The <= operator checks if the left value is less than or equal to the right value.

Example:

print(6 <= 8)  # Output: True
print(8 <= 6)  # Output: False
Python

In the first example, 6 is less than 8, so the result is True. In the second example, 8 is not less than or equal to 6, so the result is False.

Working with Different Data Types

While we mostly use these operators with numbers, Python allows their usage with other data types like strings, lists, and more. Let’s explore some examples:

Comparing Strings (Lexicographical Order)

Python compares strings lexicographically (in dictionary order), where each character’s Unicode or ASCII value is considered.

Example:

print('apple' > 'banana')  # Output: False
print('zebra' < 'lion')    # Output: False
print('cat' == 'cat')      # Output: True
Python

Here, ‘apple’ is less than ‘banana’ because ‘a’ comes before ‘b’ in lexicographical order. Similarly, ‘zebra’ is not less than ‘lion’ because ‘z’ comes after ‘l’.

Comparing Lists

When comparing lists, Python compares them element by element.

Example:

print([1, 2, 3] > [1, 2])     # Output: True
print([1, 2, 3] < [1, 2, 4])  # Output: True
Python

In the first example, [1, 2, 3] is greater than [1, 2] because it has more elements. In the second example, [1, 2, 3] is less than [1, 2, 4] because the third element, 3, is less than 4.

Homework for You

Try experimenting with relational operators on other data types, such as:

  • Sets
  • Dictionaries
  • Custom objects

For instance:

print({'a': 1} > {'b': 2})
Python

Additionally, try using multiple relational operators in a single expression. For example:

print(5 < 10 < 15) # Output: True
print(10 > 5 == 5) # Output: True
Python

Explore how Python evaluates such expressions step by step.

Conclusion

Understanding relational operators is very important for effective programming in Python as it allows you to make decisions based on comparisons between 2 values, which is a fundamental aspect of coding. I encourage you to play with these operators using various data types to see how they behave differently.

Python Relational Operator

Chain comparison result

1 / 4

What is the output of the expression: print(5 < 10 < 15)?

Dictionary order comparison

2 / 4

How does Python compare strings using relational operators?

Greater than operator

3 / 4

Which operator returns True when the left value is greater than the right value?

Element-by-element comparison

4 / 4

How does Python compare lists using relational operators?

Your score is

The average score is 0%

0%

Leave a Reply

Your email address will not be published. Required fields are marked *