Skip to content

Python comparison operators

  • by

Python comparison operators are used to compare values and determine their relationship. These operators allow you to check conditions and make decisions based on the results of the comparisons. Python provides several comparison operators, including:

  1. Equal to (==): Checks if two values are equal.
  2. Not equal to (!=): Checks if two values are not equal.
  3. Greater than (>): Checks if the left operand is greater than the right operand.
  4. Less than (<): Checks if the left operand is less than the right operand.
  5. Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  6. Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

These operators return a Boolean value of either True or False based on the comparison result. They can be used in conditional statements, loop conditions, and any situation where you need to compare values in Python.

Here’s a tabular format representing the Python comparison operators:

OperatorDescriptionExample
== (equal to)Checks if values are equala == b
!= (not equal to)Checks if values are not equala != b
> (greater than)Checks if left value is greatera > b
< (less than)Checks if the left value is greatera < b
>= (greater or equal to)Checks if the left value is lessa >= b
<= (less or equal to)Checks if the left value is greater or equala <= b

These operators can be used with numeric values, strings, and other data types to compare and evaluate expressions.

Python comparison operators example

Here are some examples of Python comparison operators in action:

# Equal to (==)
x = 5
y = 7
print(x == y)  # False

# Not equal to (!=)
a = 10
b = 10
print(a != b)  # False

# Greater than (>)
p = 15
q = 10
print(p > q)   # True

# Less than (<)
m = 5
n = 8
print(m < n)   # True

# Greater than or equal to (>=)
num1 = 20
num2 = 20
print(num1 >= num2)  # True

# Less than or equal to (<=)
val1 = 10
val2 = 15
print(val1 <= val2)  # True

Output:

Python comparison operators

In the above examples, the comparison operators are used to compare different values and produce Boolean results (True or False). Depending on the values being compared, the output will vary.

Here’s an example that demonstrates the use of multiple comparison operators in Python:

x = 10
y = 15
z = 20

# Using multiple comparison operators
result = x < y < z
print(result)  # True

# Using multiple comparison operators with logical AND
result = x < y and y < z
print(result)  # True

# Using multiple comparison operators with logical OR
result = x > y or y < z
print(result)  # True

These examples demonstrate how you can combine multiple comparison operators using logical operators (and, or) to perform more complex evaluations and make decisions based on the comparison results.

Do comment if you have any doubts or suggestions on this Python operator topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading