Python

Comparison Operators

Click for All Topics

Comparison Operators in python

In Python, comparison operators are used to compare the values of two operands and ascertain their relationship. In response to the comparison outcome, these operators return a boolean value (true or false). The most widely used comparison operators in Python are listed below:

Equal to operator

In Python, the equals operator (==) determines whether the values of two operands are equal. It compares the values on both sides of the operator and returns true if they are equal and false otherwise. Here is an example to clarify the concept:

x = 5
y = 5
print(x == y) 
 
# Output of the above Python code:
# ---> Output: True
# Comparing floats
c = 3.14
d = 3.14
print(c == d)  

# Output of the above Python code:
# ---> Output: True

# Comparing strings
message1 = "edslash"
message2 = "Learning"
print(message1 == message2)  

# Output of the above Python code:
# --->Output: False

# Comparing boolean values
is_raining = True
is_sunny = False
print(is_raining == is_sunny)  

# Output of the above Python code:
#---> Output: False

Not equal to

Python’s not equal to operator (!=) determines whether the values of two operands do not equal each other. It compares the values on both sides of the operator and outputs true or false depending on whether they are equal or not.

# Comparing integers
a = 10
b = 20
print(a != b)  

# Output of the above Python code:
# --->Output: True

# Comparing floats
c = 3.14
d = 2.71
print(c != d) 

# Output of the above Python code:
# --->Output: True

# Comparing strings
message1 = "edslash"
message2 = "learning"
print(message1 != message2)  

# Output of the above Python code:
# --->Output: True

Greater than

Python’s greater than operator (>) determines whether the value on the operator’s left side is greater than the value on its right side through comparison. If the criterion is met, it returns true. e. whenever the left operand exceeds the right operand. If not, false is returned.

x = 5
y = 3

print(x > y)  

# Output of the above Python code:
# --->Output: True
# Comparing integers
a = 10
b = 5
print(a > b)
  
# Output of the above Python code:
#---> Output: True

# Comparing floats
c = 3.14
d = 2.71
print(c > d)  

# Output of the above Python code:
# ---> Output: True

# Comparing strings
message1 = "apple"
message2 = "banana"
print(message1 > message2)
  
# Output of the above Python code:
# ---> Output: False

# Comparing characters
char1 = 'a'
char2 = 'A'
print(char1 > char2) 

# Output of the above Python code:
# ---> Output: True

Less than

Python’s less than operator () determines whether the value on the left side of the operator is less than the value on the right side through comparison. If the condition is met—that is, if the left operand is less than the right operand—it returns true. Otherwise, false is returned.

# Comparing integers
a = 5
b = 10
print(a < b)  

# Output of the above Python code:
# Output: True

# Comparing floats
c = 2.71
d = 3.14
print(c < d)  

# Output of the above Python code:
# ---> Output: True

# Comparing strings
message1 = "edslash"
message2 = "learning"
print(message1 < message2)  

# Output of the above Python code:
# ---> Output: True

Greater than equal to

A comparison operator in Python called greater than or equal to (>=) determines whether the value on the left side of the operator is greater than or equal to the value on the right side. If the condition is met, i.e., when it returns true, i.e., if the right operand and left operand both have values greater than or equal to one, If not, false is returned.

# Comparing integers
a = 10
b = 5
print(a >= b)  

# Output of the above Python code:
# --->Output: True

# Comparing floats
c = 3.14
d = 3.14
print(c >= d)  

# Output of the above Python code:
# Output: True

Less than equal to

The symbol “=” (a less than sign followed by an equal sign) stands in for the “less than or equal to” operator in Python. When two values are compared, it determines whether the left-hand side value is lower than or equal to the right-hand side value and produces a boolean value (true or false).

x = 5
y = 10

result = x <= y

print(result)

# Output of the above Python code:
# --->Output: True

Article written by Aakarsh Pandey, Team edSlash