Comparison Operators in Python - GeeksforGeeks
Comparison Operators in Python - GeeksforGeeks
37
Black Friday Sale @Courses Python Course Python Basics Interview Questions Python Quiz Popu
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.
Python
Equal to: True if both operands are
Equality == a == b
equal
Operators
https://www.geeksforgeeks.org/relational-operators-in-python/ 1/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks
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
Chaining
It Consist of mixture of above
Comparisio
Operators.
n Operators
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
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
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
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
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
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
ankth… 24
Similar Reads
https://www.geeksforgeeks.org/relational-operators-in-python/ 7/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks
https://www.geeksforgeeks.org/relational-operators-in-python/ 8/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks
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
https://www.geeksforgeeks.org/relational-operators-in-python/ 10/12
11/27/24, 10:32 PM Comparison Operators in Python - GeeksforGeeks
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]
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
https://www.geeksforgeeks.org/relational-operators-in-python/ 12/12