Open In App

Comparison Operators in Python

Last Updated : 03 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Python operators can be used with various data types, including numbers, strings, booleans, and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters (lexicographic order).
Be cautious when comparing floating-point numbers due to potential precision issues. Consider using a small tolerance value for comparisons instead of strict equality.

Types of Comparison Operators in Python

The different types of Comparison Operators in Python are as follows:


Operator

Description

Syntax

Python Equality Operators

==

Equal to: True if both operands are equal

a == b

Inequality Operators

!=

Not equal to: True if operands are not equal

a != b

Greater than Sign

>

Greater than: True if the left operand is greater than the righta > b

Less than Sign

<

Less than: True if the left operand is less than the righta < b

Greater than or Equal to Sign

>=

Greater than or equal to: True if left operand is greater than or equal to the righta >= b

Less than or Equal to Sign

<=

Less than or equal to: True if left operand is less than or equal to the righta <= b

Chaining Comparision Operators


It Consist of mixture of above Operators.


Now let’s see the comparison operators in Python one by one.

Python Equality Operators

The Equal to Operator is also known as the Equality Operator in Python, as it is used to check for equality. It returns True if both the operands are equal i.e. if both the left and the right operands are equal to each other. Otherwise, it returns False.

Syntax: a == b

Example: In this code, we have three variables ‘a’, ‘b’ and ‘c’ and assigned them with some integer value. Then we have used equal to operator to check if the variables are equal to each other.

Python
a = 9
b = 5
c = 9

# Output
print(a == b)
print(a == c)

Output:

False
True

Inequality Operators

The Not Equal To Operator returns True if both the operands are not equal and returns False if both the operands are equal.

Syntax: a != b

Example: In this code, we have three variables ‘a’, ‘b’ and ‘c’ and assigned them with some integer value. Then we have used equal to operator to check if the variables are equal to each other.

Python
a = 9
b = 5
c = 9

# Output
print(a != b)
print(a != c)

Output:

True
False

Greater than Sign

The Greater Than Operator returns True if the left operand is greater than the right operand otherwise returns False.

Syntax: a > b

Example: In this code, we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used greater than operator to check if a variable is greater than the other.

Python
a = 9
b = 5

# Output
print(a > b)
print(b > a)

Output:

True
False

Less than Sign

The Less Than Operator returns True if the left operand is less than the right operand otherwise it returns False.

Syntax: a < b

Example: In this code, we have two variables ‘a’ and ‘b’ and assigned them with some integer value. Then we have used less than operator to check if a variable is less than the other.

Python
a = 9
b = 5

# Output
print(a < b)
print(b < a)

Output:

False
True

Greater than or Equal to Sign

The Greater Than or Equal To Operator returns True if the left operand is greater than or equal to the right operand, else it will return False.

Syntax: x >= y

Example: In this code, we have three variables ‘a’, ‘b’ and ‘c’ and assigned them with some integer value. Then we have used greater than or equal to operator to check if a variable is greater than or equal to the other.

Python
a = 9
b = 5
c = 9

# Output
print(a >= b)
print(a >= c)
print(b >= a)

Output:

True
True
False

Less than or Equal to Sign

The Less Than or Equal To Operator returns True if the left operand is less than or equal to the right operand.

Syntax: x <= y

Example: In this code, we have three variables ‘a’, ‘b’ and ‘c’ and assigned them with some integer value. Then we have used less than or equal to operator to check if a variable is less than or equal to the other.

Python
a = 9
b = 5
c = 9

# Output
print(a <= b)
print(a <= c)
print(b <= a)

Output:

False
True
True

Chaining Comparision Operators

In Python, we can use the chaining comparison operators to check multiple conditions in a single expression. One simple way of solving multiple conditions is by using Logical Operators. But in chaining comparison operators method, we can achieve this without any other operator.

Syntax: a op1 b op2 c

Example: In this code we have a variable ‘a’ which is assigned some integer value. We have used the chaining comparison operators method to compare the the value of ‘a’ with multiple conditions in a single expression.

Python
a = 5

# chaining comparison operators
print(1 < a < 10)
print(10 > a <= 9)
print(5 != a > 4)
print(a < 10 < a*10 == 50)

Output:

True
True
False
True


Previous Article
Next Article

Similar Reads

Chaining comparison operators in Python
Checking more than two conditions is very common in Programming Languages. Let's say we want to check the below condition: a &lt; b &lt; c The most common syntax to do it is as follows: if a &lt; b and b &lt; c : {...} In Python, there is a better way to write this using the Comparison operator Chaining. The chaining of operators can be written as
5 min read
Python | Data Comparison and Selection in Pandas
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier. The most important thing in Data Analysis is comparing values and selecting data accordingly. The "==" operator works for multiple valu
2 min read
Python | Excel File Comparison
Given Two Excel Files, We want to compare the values of each column row-wise after sorting the values and print the changed column name and row number and values change. Input : Two Excel files Output : Column name : 'location' and Row Number : 0 Column name : 'location' and Row Number : 3 Column name : 'date' and Row Number : 1 Code : Python code
1 min read
Python Object Comparison : "is" vs "=="
Both "is" and "==" are used for object comparison in Python. The operator "==" compares values of two objects, while "is" checks if two objects are same (In other words two references to same object). # Python program to demonstrate working of # &quot;==&quot; # Two different objects having same values x1 = [10, 20, 30] x2 = [10, 20, 30] # Comparis
2 min read
Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
Tkinter is a GUI (Graphical User Interface) module which comes along with the Python itself. This module is widely used to create GUI applications. tkinter.ttk is used to create the GUI applications with the effects of modern graphics which cannot be achieved using only tkinter. Checkbutton is used to select multiple options. Checkbuttons can be cr
2 min read
Python | Find Hotel Prices using Hotel price comparison API
Makcorps hotel API is used to get JSON data, to compare Hotel prices, ratings, and reviews from more than 200 websites including; Agoda.com, Hotels.com, Expedia and more. It is organized around GET Requests. One can use this API for free to get information for any hotel or any city regarding prices, ratings, reviews, historical prices and many othe
3 min read
Python | Consecutive String Comparison
Sometimes, while working with data, we can have a problem in which we need to perform comparison between a string and it's next element in a list and return all strings whose next element is similar list. Let's discuss certain ways in which this task can be performed. Method #1 : Using zip() + loop This is one way in which this task can be performe
3 min read
Face Comparison Using Face++ and Python
Prerequisites: Python Programming Language Python is a high-level general-purpose language. It is used for multiple purposes like AI, Web Development, Web Scraping, etc. One such use of Python can be Face Comparison. A module name python-facepp can be used for doing the same. This module is for communicating with Face++ facial recognition service.
3 min read
Comparison of Python with Other Programming Languages
Python is an easily adaptable programming language that offers a lot of features. Its concise syntax and open-source nature promote readability and implementation of programs which makes it the fastest-growing programming language in current times. Python has various other advantages which give it an edge over other popular programming languages su
3 min read
Case-insensitive string comparison in Python
We generally use Python lists to store items. An online shopping application may contain a list of items in it so that the user can search the item from the list of items. For example, Our shopping application has a list of laptops it sells. List contains many brands and one of them is 'Lenovo'. If we want to buy a laptop of Lenovo brand we go to t
4 min read
Article Tags :
Practice Tags :