0% found this document useful (0 votes)
27 views

Comparison Operators in Python - GeeksforGeeks

Uploaded by

Kiran Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Comparison Operators in Python - GeeksforGeeks

Uploaded by

Kiran Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

37

Black Friday Sale @Courses Python Course Python Basics Interview Questions Python Quiz Popu

Comparison Operators in Python


Last Updated : 03 May, 2024

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
Equal to: True if both operands are
Equality == a == b
equal
Operators

Inequality Not equal to: True if operands are


!= a != b
Operators not equal

Greater Greater than: True if the left


> a>b
than Sign operand is greater than the right

Less than Less than: True if the left operand


< a<b
Sign is less than the right

https://www.geeksforgeeks.org/relational-operators-in-python/ 1/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

Operator Description Syntax

Greater
Greater than or equal to: True if left
than or
>= operand is greater than or equal to a >= b
Equal to
Sign the right

Less than Less than or equal to: True if left


or Equal to <= operand is less than or equal to the a <= b
Sign right

Chaining
It Consist of mixture of above
Comparisio
Operators.
n 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

1 a = 9
2 b = 5
3 c = 9
4
5 # Output
6 print(a == b)
https://www.geeksforgeeks.org/relational-operators-in-python/ 2/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

7 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

1 a = 9
2 b = 5
3 c = 9
4
5 # Output
6 print(a != b)
7 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

https://www.geeksforgeeks.org/relational-operators-in-python/ 3/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

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

1 a = 9
2 b = 5
3
4 # Output
5 print(a > b)
6 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

1 a = 9
2 b = 5
3
4 # Output
5 print(a < b)
6 print(b < a)

Output:

https://www.geeksforgeeks.org/relational-operators-in-python/ 4/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

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

1 a = 9
2 b = 5
3 c = 9
4
5 # Output
6 print(a >= b)
7 print(a >= c)
8 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

https://www.geeksforgeeks.org/relational-operators-in-python/ 5/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

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

1 a = 9
2 b = 5
3 c = 9
4
5 # Output
6 print(a <= b)
7 print(a <= c)
8 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

1 a = 5

https://www.geeksforgeeks.org/relational-operators-in-python/ 6/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

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

Output:

True
True
False
True

Looking to dive into the world of programming or sharpen your Python


skills? Our Master Python: Complete Beginner to Advanced Course is
your ultimate guide to becoming proficient in Python. This course covers
everything you need to build a solid foundation from fundamental
programming concepts to advanced techniques. With hands-on
projects, real-world examples, and expert guidance, you'll gain the
confidence to tackle complex coding challenges. Whether you're
starting from scratch or aiming to enhance your skills, this course is the
perfect fit. Enroll now and master Python, the language of the future!

ankth… 24

Previous Article Next Article


New '=' Operator in Python3.8 f-string Python NOT EQUAL operator

Similar Reads

https://www.geeksforgeeks.org/relational-operators-in-python/ 7/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

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 < b < c Th…
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 …
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…
2 min read

Python | Tkinter ttk.Checkbutton and comparison with simple…


Tkinter is a GUI (Graphical User Interface) module which comes along
with the Python itself. This module is widely used to create GUI…
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…
3 min read

Choosing the Right IDE for Python Development: A Comparison


Python, being one of the most popular programming languages, has a rich
ecosystem of development environments. Selecting the right Integrated…
5 min read

String Comparison in Python


Python supports several operators for string comparison, including ==, !=,
<, <=, >, and >=. These operators allow for both equality and…
3 min read

https://www.geeksforgeeks.org/relational-operators-in-python/ 8/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

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 ru…
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. Python…
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…
4 min read

Article Tags : Python Python-Operators

Practice Tags : python

Corporate & Communications Address:-


A-143, 9th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305) | Registered Address:- K 061,
Tower K, Gulshan Vivante Apartment,
Sector 137, Noida, Gautam Buddh
Nagar, Uttar Pradesh, 201305

Company Explore
About Us Job-A-Thon Hiring Challenge

https://www.geeksforgeeks.org/relational-operators-in-python/ 9/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community

Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial

Data Science & ML Web Technologies


Data Science With Python HTML
Data Science For Beginner CSS
Machine Learning JavaScript
ML Maths TypeScript
Data Visualisation ReactJS
Pandas NextJS
NumPy NodeJs
NLP Bootstrap
Deep Learning Tailwind CSS

Python Tutorial Computer Science


Python Programming Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design
Python Interview Question Engineering Maths

DevOps System Design


Git High Level Design
AWS Low Level Design
Docker UML Diagrams
Kubernetes Interview Guide
Azure Design Patterns
GCP OOAD
DevOps Roadmap System Design Bootcamp
Interview Questions

https://www.geeksforgeeks.org/relational-operators-in-python/ 10/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

School Subjects Commerce


Mathematics Accountancy
Physics Business Studies
Chemistry Economics
Biology Management
Social Science HR Management
English Grammar Finance
Income Tax

Databases Preparation Corner


SQL Company-Wise Recruitment Process
MYSQL Resume Templates
PostgreSQL Aptitude Preparation
PL/SQL Puzzles
MongoDB Company-Wise Preparation
Companies
Colleges

Competitive Exams More Tutorials


JEE Advanced Software Development
UGC NET Software Testing
UPSC Product Management
SSC CGL Project Management
SBI PO Linux
SBI Clerk Excel
IBPS PO All Cheat Sheets
IBPS Clerk Recent Articles

Free Online Tools Write & Earn


Typing Test Write an Article
Image Editor Improve an Article
Code Formatters Pick Topics to Write
Code Converters Share your Experiences
Currency Converter Internships
Random Number Generator
Random Password Generator

https://www.geeksforgeeks.org/relational-operators-in-python/ 11/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks

DSA/Placements Development/Testing
DSA - Self Paced Course JavaScript Full Course
DSA in JavaScript - Self Paced Course React JS Course
DSA in Python - Self Paced React Native Course
C Programming Course Online - Learn C with Data Structures Django Web Development Course
Complete Interview Preparation Complete Bootstrap Course
Master Competitive Programming Full Stack Development - [LIVE]
Core CS Subject for Interview Preparation JAVA Backend Development - [LIVE]
Mastering System Design: LLD to HLD Complete Software Testing Course [LIVE]
Tech Interview 101 - From DSA to System Design [LIVE] Android Mastery with Kotlin [LIVE]
DSA to Development [HYBRID]
Placement Preparation Crash Course [LIVE]

Machine Learning/Data Science Programming Languages


Complete Machine Learning & Data Science Program - [LIVE] C Programming with Data Structures
Data Analytics Training using Excel, SQL, Python & PowerBI - C++ Programming Course
[LIVE] Java Programming Course
Data Science Training Program - [LIVE] Python Full Course
Mastering Generative AI and ChatGPT

Clouds/Devops GATE
DevOps Engineering GATE CS & IT Test Series - 2025
AWS Solutions Architect Certification GATE DA Test Series 2025
Salesforce Certified Administrator Course GATE CS & IT Course - 2025
GATE DA Course 2025

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

https://www.geeksforgeeks.org/relational-operators-in-python/ 12/12

You might also like