Sharing is caring 🙂

Comparison operators in Python are used to compare the value of two variables or values. These operators return a Boolean value of either True or False, depending on the outcome of the comparison. In this tutorial, we will explore the different comparison operators available in Python and how to use them in your code. We will also look at examples of how to combine comparison operators to create more complex expressions. By the end of this tutorial, you will have a solid understanding of how to use comparison operators in Python to make decisions in your code.

Equal to (==) Operator

The equal to operator in Python is represented by two equal signs (==). This operator is used to compare the values of two variables or values and returns a Boolean value of True if the values are the same and False if they are different. For example:

x = 5
y = 10

result = x == y
print(result) # False

x = 10
y = 10

result = x == y
print(result) # True

It can also be used to check if an element is present in a list or a key in a dictionary:

my_list = [1, 2, 3, 4, 5]
result = 3 in my_list
print(result) # True

my_dict = {"name": "John", "age": 25}
result = "name" in my_dict
print(result) # True

It’s also important to note that == operator is different from assignment operator = which assigns a value to a variable.

Not Equal to (!=) Operator

The not equal to operator in Python is represented by an exclamation mark and equal sign (!=). This operator is used to compare the values of two variables or values and returns a Boolean value of True if the values are different and False if they are the same. For example:

x = 5
y = 10

result = x != y
print(result) # True

x = 10
y = 10

result = x != y
print(result) # False

Like the equal operator, it can also be used to check if an element is not present in a list or a key in a dictionary:

my_list = [1, 2, 3, 4, 5]
result = 6 not in my_list
print(result) # True

my_dict = {"name": "John", "age": 25}
result = "gender" not in my_dict
print(result) # True

The!= operator is not the same as <>, which is an older, now deprecated syntax for the not equal operator in Python.

Less Than (<) Operator

The less than operator in Python is represented by a single angle bracket (<). This operator is used to compare the values of two variables or values and returns a Boolean value of True if the left-hand value is less than the right-hand value and False if it is not. For example:

x = 5
y = 10

result = x < y
print(result) # True

x = 10
y = 10

result = x < y
print(result) # False

It can also be used to compare letters and strings based on their ASCII/Unicode values:

result = 'a' < 'b'
print(result) # True

result = 'abc' < 'bcd'
print(result) # True

The less than operator can only be used for values of the same type and cannot be used to compare values of different types.

Less Than or Equal to (<=) Operator

The less than or equal to operator in Python is represented by a less than sign followed by an equal sign (<=). This operator is used to compare the values of two variables or values and returns a Boolean value of True if the left-hand value is less than or equal to the right-hand value and False if it is not. For example:

x = 5
y = 10

result = x <= y
print(result) # True

x = 10
y = 10

result = x <= y
print(result) # True

It can also be used to compare letters and strings based on their ASCII/Unicode values:

result = 'a' <= 'b'
print(result) # True

result = 'abc' <= 'abc'
print(result) # True

It’s important to note that the less than or equal to operator can only be used for values of the same type and cannot be used to compare values of different types.

Greater Than (>) Operator

The greater than operator in Python is represented by a single angle bracket (>). This operator is used to compare the values of two variables or values and returns a Boolean value of True if the left-hand value is greater than the right-hand value and False if it is not. For example:

x = 15
y = 10

result = x > y
print(result) # True

x = 10
y = 10

result = x > y
print(result) # False

It can also be used to compare letters and strings based on their ASCII/Unicode values:

result = 'b' > 'a'
print(result) # True

result = 'bcd' > 'abc'
print(result) # True

Greater Than or Equal to (>=) Operator

The greater than or equal to operator in Python is represented by a greater than sign followed by an equal sign (>=). This operator is used to compare the values of two variables or values and returns a Boolean value of True if the left-hand value is greater than or equal to the right-hand value and False if it is not. For example:

x = 15
y = 10

result = x >= y
print(result) # True

x = 10
y = 10

result = x >= y
print(result) # True

It can also be used to compare letters and strings based on their ASCII/Unicode values:

result = 'b' >= 'a'
print(result) # True

result = 'abc' >= 'abc'
print(result) # True

Combining Comparison Operators

In Python, comparison operators can be combined to create more complex expressions. This is done by using logical operators such as and, or, and not.

The and operator returns True if both the left-hand and right-hand expressions evaluate to True, and False otherwise. For example:

x = 5
y = 10

result = x < y and x > 0
print(result) # True

The or operator returns True if either the left-hand or right-hand expressions evaluate to True, and False otherwise. For example:

x = 15
y = 10

result = x > y or x < 0
print(result) # True

The not operator inverts the Boolean value of an expression. For example:

x = 5

result = not (x > 0)
print(result) # False

You can also use the parentheses to group the comparison expressions and make the code more readable:

x = 5
y = 10
z = 15

result = (x < y) and (y < z)
print(result) # True

The precedence of the operators will affect the outcome of the expression and use parentheses accordingly to avoid confusion and unexpected results.

Comparison Operators FAQ

  1. What are comparison operators in Python? Comparison operators in Python are used to compare the value of two variables or values. These operators return a Boolean value of either True or False, depending on the outcome of the comparison.
  2. What is the difference between the equal operator (==) and assignment operator (=)? The equal operator (==) is used to compare the values of two variables or values, while the assignment operator (=) assigns a value to a variable.
  3. Can comparison operators be used to compare values of different types? No, comparison operators can only be used for values of the same type and cannot be used to compare values of different types.
  4. What are logical operators in Python? Logical operators in Python are used to combine comparison operators to create more complex expressions. The logical operators include and, or, and not.
  5. How do I use the not operator to invert a Boolean value? You can use the not operator to invert a Boolean value by placing it before the expression. For example: not (x > 0)
  6. How do I use parentheses to group comparison expressions? You can use parentheses to group comparison expressions and make the code more readable. For example: (x < y) and (y < z)
  7. What is the difference between <> and !=? <> is an older, now deprecated syntax for the not equal operator in Python, while != is the current syntax for the not equal operator.
Sharing is caring 🙂