Introduction to Python Comparison Operators
These are special symbols in python. Basically, 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 perform an operation in relation to one or more than one operand. An operand can be described as a value or maybe a variable on which the operation is performed. As we move forward, we will get insights into the Comparison Operators in Python with their respective syntax as well as the instances.
Different Basic Types of Python Operators
Python language supports the following operators:
1. Arithmetic Operators
Python Arithmetic Operators are used to perform basic math operations, which include addition, subtraction, and so on. 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 works on bits and operates 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 the value, whether it is a member of a sequence or not. This sequence can be a list, 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 know more about the Comparison Operator.
Python Comparison Operator
The comparison operators are also called as relational operators. These operators are used to compare the values and returns ‘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 is equals to the one on the right side. This equals to operator returns ‘True if the values of the operators on both the sides are equal or else it will returns ‘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 ‘<’, 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, which is 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 returns ‘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 come up with essential points regarding Python Comparison Operators and learned about the various types and their functionality in python. To be precise, we have walked through key highlights of Python Comparison Operator in detail. We learned 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 surely say that the Python Comparison Operators’ functioning is very easy to understand. I hope you gathered thorough information on Python Operators, and the article will effectively assist you in your further assignments.
Recommended Articles
This is a guide to Python Comparison Operators. Here we discuss the introduction and different Comparison Operators in Python along with examples. You may also look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses