Updated May 3, 2023
Introduction to Python Comparison Operators
These are special symbols in Python. These are used to carry out arithmetic or logical calculation. In mathematical language, we can say that the Python operator is a special symbol used to operate in relation to one or more than one operand. An operand can be described as a value or variable on which the operation is performed. As we progress, we will get insights into the Comparison Operators in Python with their respective syntax and the instances.
Different Basic Types of Python Operators
Python language supports the following operators:
1. Arithmetic Operators
Python Arithmetic Operators perform basic math operations, including addition, subtraction, etc. The various operators are Subtraction, Division, Addition, Multiplication, Floor Division, Exponent, and Modulus.
2. Comparison (Relational) Operators
- Python Comparison Operators are used to compare the values on both sides. Various operators are ==, != , <>, >,<=, etc.
- [This Comparison Operator we are going to discuss in detail below.]
3. Assignment Operators
Python Assignment Operators are used to assign values to the variables. Various operators are +=, – = , *=, /= , etc.
4. Logical Operators
Python Logical Operators are used for conditional statements. Various operators are Logical AND, Logical OR, and Logical NOT.
5. Bitwise Operators
Python Bitwise Operators work on bits and operate on operands bit by bit instead of whole. Various operators are –Python Bitwise AND, OR, XOR, Left-shift, Right-shift, and 1’s complement Bitwise Operator.
6. Membership Operators
Python Membership Operators are used to test whether the value is a member of a sequence or not. This sequence can be a list, a tuple, or a string. The two identify operators used in Python are ‘in and not in.’
7. Identity Operators
- Python Identity Operators are used for comparing the memory location of the two objects. The two identified operators used in Python are ‘is’ and ‘is not.
- So, let’s get started to learn more about the Comparison Operator.
Python Comparison Operator
The comparison operators are also called relational operators. These operators compare the values and return ‘True’ or ‘False’ based on the condition.
Comparison Operators in Python
Equal To – ‘= =.’
Greater Than – ‘>.’
Less Than – ‘<.’
Greater Than or Equal to – ‘>=.’
Less Than or Equal To – ‘<=.’
Not Equal To – ‘!=.’
1. Equal To
Equal To the Operator, denoted by ‘==,’ checks the value of the Operator on the left side equals the one on the right side. This equals to the Operator returns ‘True if the values of the operators on both sides are equal, or else it will return ‘False.’
Example #1
x = 7
y = 5
print(x == y)
Output: False
# returns ‘False’ because 7 is not equal to 5.
Example #2
x = 10
y = 20
print(x == y)
Output: False
# returns ‘False’ because 10 is not equal to 20.
2. Greater Than
Greater than Operator is denoted by ‘>,’ checks the value of the Operator on the left side is greater than that on the right side.
Example #1
x = 7
y = 5
print(x > y)
Output: True
# returns ‘True’ because 7 is greater than 5.
Example #2
x = 10
y = 20
print(x > y)
Output: False
# returns ‘False’ because 10 is not greater than 20.
3. Less Than
Less Than Operator is denoted by ‘<,’ which checks the value of the Operator on the left side is lesser than that on the right side.
Example #1
x = 7
y = 5
print(x < y)
Output: False
# returns ‘False’ because 7 is not less than 5
Example #2
x = 10
y = 20
print(x < y)
Output: True
# returns ‘True’ because 10 is less than 20
4. Greater Than or Equal To
The Greater Than or Equal To Operator, denoted by ‘>=,’ returns ‘True if and only if the value of the Operator on the left side is either greater than or equal to that on the right side.
Example #1
x = 7
y = 5
print(x >= y)
Output: True
# returns ‘True’ because 7 is greater, or equal, to 5
Example #2
x = 10
y = 20
print(x >= y)
Output: False
# returns ‘False’ because 10 is not greater nor equal to 20
5. Less Than or Equal To
The Less Than or Equal To the Operator, denoted as ‘<=,’ returns ‘True if and only if the value of the Operator on the left side is either less than or equal to that on the right side.
Example #1
x = 7
y = 5
print(x <= y)
Output: False
# returns ‘False’ because 7 is neither less than or equal to 5
Example #2
x = 10
y = 20
print(x <= y)
Output: True
# returns ‘True’ because 10 is less than, or equal, to 20
6. Not Equal To
Not Equal To Operator, denoted by ‘!=’, works exactly opposite to the Equal To operator.
This Operator returns ‘True if the values of the operators on both sides are unequal, or else it will return ‘False.’
Example #1
x = 7
y = 5
print(x != y)
Output: True
# returns ‘True’ because 7 is not equal to 5
Example #2
x = 10
y = 20
print(x != y)
Output: True
# returns ‘True’ because 10 is not equal to 20.
Conclusion
To summarize, we have developed essential points regarding Python Comparison Operators and learned about their various types and functionality in Python. To be precise, we have detailed key highlights of Python Comparison Operator. We knew the comparison operators are Equal To, Less Than, Greater Than, Greater Than or Equal To, Less Than or Equal To, and Not Equal To.
With this, we can indeed say that the Python Comparison Operators’ functioning is effortless to understand. I hope you gathered thorough information on Python Operators and that the article will effectively assist you in further assignments.
Recommended Articles
This is a guide to Python Comparison Operators. Here we discuss the introduction, different Comparison Operators in Python, and examples. You may also look at the following articles to learn more –