Table of contents
1.
Introduction
2.
What are comparison operators in Python?
3.
Types of Python Comparison Operators
4.
Equal to “==”
4.1.
Example
4.2.
Python
5.
Not equal to “! =” 
5.1.
Example
5.2.
Python
6.
Greater than “>”
6.1.
Example
6.2.
Python
7.
Less than “<”
7.1.
Example
7.2.
Python
8.
Greater than or equal to “>=”
8.1.
Example
8.2.
Python
9.
Less than or equal to “<= “
9.1.
Example
9.2.
Python
10.
Chaining Comparison Operators
10.1.
Example:
10.2.
Python
11.
Comparison of Float Numbers
11.1.
Example:
11.2.
Python
11.3.
Python
12.
Comparison of Complex Numbers
12.1.
Example:
12.2.
Python
13.
Comparison of Booleans
13.1.
Example:
13.2.
Python
13.3.
Python
13.4.
Python
14.
Comparison of Sequence Types
14.1.
Example:
14.2.
Python
14.3.
Python
15.
Comparison of Dictionary Objects
15.1.
Example:
15.2.
Python
16.
Comparing Two Strings in Python
16.1.
Example:
16.2.
Python
17.
Frequently Asked Questions
17.1.
How to check == in Python?
17.2.
What is the "does not equal" sign in Python?
17.3.
What are the six comparison operators?
17.4.
Which operator can be used to compare two values Python?
17.5.
What type of operator is == in Python?
17.6.
What is a comparison operator?
18.
Conclusion
Last Updated: Jan 21, 2025
Easy

Python comparison operators

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, comparison operators are used to compare two values or expressions and determine the relationship between them. These operators are fundamental in decision-making and control flow, allowing you to evaluate conditions and make choices based on comparisons. Whether you're checking equality, inequality, or comparing values based on size, Python provides a simple and efficient way to perform these operations.

Python comparison operators

What are comparison operators in Python?

Comparison operators in Python are used to compare two values. They evaluate to True or False depending on the condition. These operators are the building blocks of control flow in Python, enabling conditional statements like if, elif, and else.

Types of Python Comparison Operators

Python provides several comparison operators to evaluate relationships between values. These include equality (==), inequality (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators return boolean values (True or False) based on the comparison result.

OperatorDescriptionExampleResult
==Equal to5 == 5True
! = Not equal to5  ! = 4True
>Greater than5 <>3True
<Less than 5 < 6True
>=Greater than or equal to  5 >= 4True
<= Less than or equal to 5 <= 4False

Equal to “==”

The == operator in Python is used to check if two values or expressions are equal. It returns True if the values are identical and False if they are not. This operator is commonly used in conditional statements to compare variables.

Example

  • Python

Python

print(5 == 5) 
You can also try this code with Online Python Compiler
Run Code

Output: 

True

Not equal to “! =” 

The not equal to operator (!=) in Python checks if two values or expressions are not equal. It returns True if the values differ and False if they are the same.

Example

  • Python

Python

print(5 != 4)  
You can also try this code with Online Python Compiler
Run Code

Output: 

True

Greater than “>”

The > operator in Python checks if the value on the left is greater than the value on the right. It returns True if the condition is met, otherwise False. This is commonly used for numerical comparisons.

Example

  • Python

Python

print(5 > 3)   
You can also try this code with Online Python Compiler
Run Code

Output: 

True

Less than “<”

The < operator in Python checks if the value on the left is less than the value on the right. It returns True if the condition is satisfied, otherwise False. This operator is frequently used for numerical comparisons.

Example

  • Python

Python

print(5 < 6)  
You can also try this code with Online Python Compiler
Run Code

Output:

 True

Greater than or equal to “>=”

The >= operator in Python checks if the value on the left is greater than or equal to the value on the right. It returns True if the condition is met, otherwise False. This is useful for range-based comparisons.

Example

  • Python

Python

print(5 >= 5)  
You can also try this code with Online Python Compiler
Run Code

Output:

 True

Less than or equal to “<= “

The <= operator in Python checks if the value on the left is less than or equal to the value on the right. It returns True if the condition holds, otherwise False. This operator is often used in boundary condition checks.

Example

  • Python

Python

print(5 <= 4) 
You can also try this code with Online Python Compiler
Run Code

Output: 

False

Chaining Comparison Operators

Python allows chaining of comparison operators, enabling multiple conditions to be evaluated in a single statement. This enhances readability and reduces the need for nested conditions.

Example:

  • Python

Python

x = 10
# Chaining comparison operators
result = 5 < x <= 15
print(result)
You can also try this code with Online Python Compiler
Run Code

In this example, 5 < x <= 15 checks if x is greater than 5 and less than or equal to 15 simultaneously. The result is True because both conditions are satisfied.

Comparison of Float Numbers

When comparing floating-point numbers, it's important to consider the precision of the values.

Example:

Comparing floats

  • Python

Python

print(0.1 + 0.2 == 0.3)  
You can also try this code with Online Python Compiler
Run Code

Output:

 False

due to precision issues

A better way to compare floats is to use a tolerance

tolerance = 1e-10

  • Python

Python

print(abs((0.1 + 0.2) - 0.3) < tolerance)  
You can also try this code with Online Python Compiler
Run Code

Output: 

True

The first comparison returns False because of the way floating-point numbers are represented in memory. The second comparison accounts for this by checking if the difference is smaller than a specified tolerance.

Comparison of Complex Numbers

Complex numbers in Python can be compared for equality but not for order.

Example:

Comparing complex numbers

  • Python

Python

print((1 + 1j) == (1 + 1j))  
You can also try this code with Online Python Compiler
Run Code

Output: 

True

The following will raise a TypeError, as order comparison is not supported

print((1 + 1j) > (1 + 1j))

The equality comparison checks if both the real and imaginary parts are the same.

Comparison of Booleans

Booleans in Python are a subtype of integers. True is equivalent to 1, and False is equivalent to 0.

Example:

# Comparing booleans

  • Python

Python

print(True == 1)   
You can also try this code with Online Python Compiler
Run Code

Output: 

True

 

  • Python

Python

print(False == 0) 
You can also try this code with Online Python Compiler
Run Code

Output: 

True

 

  • Python

Python

print(True > False) 
You can also try this code with Online Python Compiler
Run Code

Output: 

True

This shows that True is not only equal to 1 but also considered greater than False.

Comparison of Sequence Types

Python allows sequence types (like lists, tuples) to be compared lexicographically, using the comparison operators.

Example:

# Comparing lists

  • Python

Python

print([1, 2] < [1, 2, 3]) 
You can also try this code with Online Python Compiler
Run Code

Output: 

True

 

  • Python

Python

print([1, 2] < [1, 3])    
You can also try this code with Online Python Compiler
Run Code

Output: 

True

The comparison is performed element-wise until a difference is found.

Comparison of Dictionary Objects

Dictionaries can be compared for equality but not for order.

Example:

# Comparing dictionaries

  • Python

Python

print({'a': 1, 'b': 2} == {'b': 2, 'a': 1})  
You can also try this code with Online Python Compiler
Run Code

Output: 

True

# The following will raise a TypeError in Python 3

# print({'a': 1, 'b': 2} > {'a': 1, 'b': 3})

Equality comparison checks if both dictionaries have the same key-value pairs.

Comparing Two Strings in Python

In Python, you can compare two strings using various comparison operators. For example, equality operator(==):

Example:

  • Python

Python

str1 = "Coding Ninjas"
str2 = "Coding Ninjas"

if str1 == str2:
print("Strings are equal.")
else:
print("Strings are not equal.")
You can also try this code with Online Python Compiler
Run Code

Output

Strings are equal.

Frequently Asked Questions

How to check == in Python?

In Python, == means equality operator. It checks if two values are equal.

What is the "does not equal" sign in Python?

The "does not equal" sign in Python is !=.

What are the six comparison operators?

The six comparison operators in Python are ==, !=, <, >, <=, and >=.

Which operator can be used to compare two values Python?

The operator used to compare two values in Python is ==.

What type of operator is == in Python?

The == operator in Python is a comparison operator that checks if two values are equal, returning True if they are, otherwise False.

What is a comparison operator?

A comparison operator evaluates the relationship between two values, such as equality, inequality, or size, and returns a boolean result (True or False).

Conclusion

Python's comparison operators are a fundamental aspect of the language that enable logical decision-making in code. By understanding and utilizing these operators, developers can write more efficient and readable programs. Remember to consider the data types you're comparing and the context in which you're using them to avoid unexpected results. With the examples and explanations provided, you're now equipped to use Python comparison operators effectively in your coding endeavors.

Recommended Readings:

  1. Python Operators
  2. Python Operator Precedence
Live masterclass