Comparison Operators in Python

Python, a versatile programming language, provides various comparison operators that allow developers to compare values and make decisions based on the results.

Comparison operators evaluate the relationship between two operands and return a Boolean value (True or False) based on the comparison's outcome.

In this module, we will explore the different comparison operators available in Python, along with code examples and detailed explanations.

Equality Operator

The equality operator (==) compares two values and returns True if they are equal, and False otherwise. It checks for the equality of both value and data type.

Example:

 >>> x = 10
 >>> y = 5
 >>> print(x == y)
 >>> # Output: False
 >>> name1 = "Alice"
 >>> name2 = "Alice"
 >>> print(name1 == name2)
 >>> # Output: True

Inequality Operator (!=)

The inequality operator (!=) checks if two values are not equal and returns True if they are not equal, and False if they are equal.

Example:

 >>> x = 10
 >>> y = 5
 >>> print(x != y)
 >>> # Output: True
 >>> name1 = "Alice"
 >>> name2 = "Bob"
 >>> print(name1 != name2)
 >>> # Output: True

Greater Than Operator (>)

The greater than operator (>) compares two values and returns True if the left operand is greater than the right operand; otherwise, it returns False.

Example:

 >>> x = 10
 >>> y = 5
 >>> print(x > y)
 >>> # Output: True
 >>> age1 = 25
 >>> age2 = 30
 >>> print(age1 > age2)
 >>> # Output: False

Less Than Operator (<)

The less than operator (<) compares two values and returns True if the left operand is less than the right operand; otherwise, it returns False.

Example:

 >>> x = 10
 >>> y = 5
 >>> print(x < y)
 >>> # Output: False
 >>> age1 = 25
 >>> age2 = 30
 >>> print(age1 < age2)
 >>> # Output: True

Greater Than or Equal To Operator (>=)

The greater than or equal to operator (>=) checks if the left operand is greater than or equal to the right operand. It returns True if the comparison is true and False otherwise.

Example:

 >>> x = 10
 >>> y = 5
 >>> print(x >= y)
 >>> # Output: True
 >>> age1 = 25
 >>> age2 = 30
 >>> print(age1 >= age2)
 >>> # Output: False

Less Than or Equal To Operator (<=)

The less than or equal to operator (<=) checks if the left operand is less than or equal to the right operand. It returns True if the comparison is true and False otherwise.

Example:

 >>> x = 10
 >>> y = 5
 >>> print(x <= y)
 >>> # Output: False
 >>> age1 = 25
 >>> age2 = 30
 >>> print(age1 <= age2)
 >>> # Output: True

IMP: It is essential to note that comparison operators can be used not only with numerical values but also with strings, lists, and other data types. The operators evaluate the values based on their defined comparison rules for each data type.

Module Takeaways…

Comparison operators in Python play a vital role in making decisions based on the relationship between values.

This article covered the most commonly used comparison operators, including the equality operator (==), inequality operator (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).

By understanding and utilizing these operators effectively, developers can write conditional statements, loop structures, and perform data filtering with ease. Remember to consider the data types being compared to ensure accurate and expected results in your Python programs.


Learn via Video Course

Python Logo

Python

6109

7 hrs