The Wayback Machine - https://web.archive.org/web/20240901094421/https://www.geeksforgeeks.org/relational-operators-in-python/
Open In App

Comparison Operators in Python

Last Updated : 03 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 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
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
String Comparison in Python
String comparison is a fundamental operation in any programming language, including Python. It enables us to ascertain strings' relative positions, ordering, and equality. Python has a number of operators and techniques for comparing strings, each with a specific function. We will examine numerous Python string comparison methods in this article an
5 min read
Comparison of Java with other programming languages
Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet, Java is used in ev
4 min read
Python2 vs Python3 | Syntax and performance Comparison
Python 2.x has been the most popular version for over a decade and a half. But now more and more people are switching to Python 3.x. Python3 is a lot better than Python2 and comes with many additional features. Also, Python 2.x is becoming obsolete this year. So, it is now recommended to start using Python 3.x from now-onwards. Still in dilemma? Ev
4 min read
When to Use Django? Comparison with other Development Stacks
Prerequisite - Django Introduction and Installation When to Use Django and Why? After getting to know the basics of Python, one should know when to use Django and why? Django is a high-level python based web framework which allows you to quickly create web applications without all of the installation or dependency problems that you normally will fi
4 min read
Comparison of FastAPI with Django and Flask
For starters, as you may know, Python is an interpreted programming language that is becoming more and more popular for building web applications. However, there are numerous web frameworks to choose from but they all have different use cases. In this article, we will look at 3 frameworks which are : Django - It follows the model-template-view arch
4 min read
Python | Operators | Question 1
What is the output of the following code : C/C++ Code print 9//2 (A) 4.5 (B) 4.0 (C) 4 (D) Error Answer: (C)Explanation: The ‘//’ operator in Python returns the integer part of the floating number. Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Operators | Question 2
Which function overloads the &gt;&gt; operator? (A) more() (B) gt() (C) ge() (D) None of the above Answer: (D) Explanation: rshift() overloads the &gt;&gt; operatorQuiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Operators | Question 3
Which operator is overloaded by the or() function? (A) || (B) | (C) // (D) / Answer: (B) Explanation: or() function overloads the bitwise OR operatorQuiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Operators | Question 4
What is the output of the following program : C/C++ Code i = 0 while i (A) 0 2 1 3 2 4 (B) 0 1 2 3 4 5 (C) Error (D) 1 0 2 4 3 5 Answer: (C)Explanation: There is no operator ++ in Python Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Merging and Updating Dictionary Operators in Python 3.9
Python 3.9 is still in development and scheduled to be released in October this year. On Feb 26, alpha 4 versions have been released by the development team. One of the latest features in Python 3.9 is the merge and update operators. There are various ways in which Dictionaries can be merged by the use of various functions and constructors in Pytho
3 min read
Python 3 - Logical Operators
Logical Operators are used to perform certain logical operations on values and variables. These are the special reserved keywords that carry out some logical computations. The value the operator operates on is known as Operand. In Python, they are used on conditional statements (either True or False), and as a result, they return boolean only (True
4 min read
How To Do Math in Python 3 with Operators?
Python3 provides us data types like integer and float along with various operators to perform mathematical calculations for graph plotting, machine learning algorithms, Statistical, data analytical purposes. An operator is a symbol that performs specific operations, which is very useful if one is frequently dealing with numbers. Operator precedence
5 min read
Precedence and Associativity of Operators in Python
In Python, operators have different levels of precedence, which determine the order in which they are evaluated. When multiple operators are present in an expression, the ones with higher precedence are evaluated first. In the case of operators with the same precedence, their associativity comes into play, determining the order of evaluation. Opera
4 min read
Augmented Assignment Operators in Python
An assignment operator is an operator that is used to assign some value to a variable. Like normally in Python, we write "a = 5" to assign value 5 to variable 'a'. Augmented assignment operators have a special role to play in Python programming. It basically combines the functioning of the arithmetic or bitwise operator with the assignment operator
4 min read
Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...)
Python in its definition provides methods to perform inplace operations, i.e. doing assignments and computations in a single statement using an operator module. Example x += y is equivalent to x = operator.iadd(x, y) Inplace Operators in PythonBelow are some of the important Inplace operators in Python: iadd()isub()iconcat()imul()itruediv()imod()ia
3 min read
Inplace Operators in Python | Set 2 (ixor(), iand(), ipow(),…)
Inplace Operators in Python | Set 1(iadd(), isub(), iconcat()…) More functions are discussed in this articles. 1. ixor() :- This function is used to assign and xor the current value. This operation does "a^ = b" operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples. 2. ipow() :- This function is
3 min read
Inplace vs Standard Operators in Python
Inplace Operators - Set 1, Set 2Normal operators do the simple assigning job. On other hand, Inplace operators behave similarly to normal operators except that they act in a different manner in case of mutable and Immutable targets. The _add_ method, does simple addition, takes two arguments, returns the sum, and stores it in another variable witho
3 min read
Increment += and Decrement -= Assignment Operators in Python
If you're familiar with Python, you would have known Increment and Decrement operators ( both pre and post) are not allowed in it. Python is designed to be consistent and readable. One common error by a novice programmer in languages with ++ and -- operators are mixing up the differences (both in precedence and in return value) between pre and post
3 min read
Python Operators
In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators.  OPERATORS: These are the special symbols. Eg- + , * , /, etc.OPERAND: It is the value on which the operato
12 min read
Python Membership and Identity Operators
There is a large set of Python operators that can be used on different datatypes. Sometimes we need to know if a value belongs to a particular set. This can be done by using the Membership and Identity Operators. In this article, we will learn about Python Membership and Identity Operators. Table of Content Python Membership OperatorsPython IN Oper
7 min read
Python Bitwise Operators
Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic and logical computations. The value the operator operates on is known as the Operand.  Python Bitwise OperatorsPython bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into
6 min read
Division Operators in Python
Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. Division Operators in PythonThere are two types of division operators: Float divisionInteger division( Floor division)When an integer is divided, the
6 min read
Python Logical Operators
Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss logical operators in Python definition and also loo
6 min read
Python Arithmetic Operators
The Python operators are fundamental for performing mathematical calculations in programming languages like Python, Java, C++, and many others. Arithmetic operators are symbols used to perform mathematical operations on numerical values. In most programming languages, arithmetic operators include addition (+), subtraction (-), multiplication (*), d
5 min read
Assignment Operators in Python
The Python Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, and bitwise computations. The value the operator operates on is known as the Operand. Here, we will cover Different Assignment operators in Python. Operators Sign Description SyntaxAssignment Operator = Assi
11 min read
Article Tags :
Practice Tags :