🌐
Python Tutorial
pythontutorial.net › home › python basics › python comparison operators
Python Comparison Operators
March 26, 2025 - Python has six comparison operators: less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), and not equal to (!=).
🌐
W3Schools
w3schools.com › python › gloss_python_comparison_operators.asp
Python Comparison Operators
Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match
Discussions

Comparison Operators
Using a separate operator for comparisons and assignment is purely intentional. First, if = was used for both, it would make parsing much more difficult as now Python would have to infer what we want to do based on context - and sometimes that may not be possible either. Second, keeping the two separate helps alleviate bugs where you've accidentally used the wrong operator. == can be made illegal in contexts where only = makes sense, and vice-versa. This of course doesn't magically solve the cases where both are valid, but it helps regardless. I'm not aware of any language that would use = for both. Some languages use = for comparison and := for assignment; Python adopted :=, aka the walrus operator, to have assignment expressions that also return a value, while not 0 <= (num := int(input(...))) <= 100: print("Must be between 0 and 100") but it's not going to replace the assignment operator. More on reddit.com
🌐 r/learnpython
11
1
September 26, 2022
python - Comparing two lists using the greater than or less than operator - Stack Overflow
I noticed a piece of code recently directly comparing two lists of integers like so: a = [10,3,5, ...] b = [5,4,3, ...,] if a > b: ... which seemed a bit peculiar, but I imagined it would ... More on stackoverflow.com
🌐 stackoverflow.com
Would you prefer support chaining of comparison operators?
I haven't seen it used for anything besides checking if a number is in a range, and for that case I prefer syntax to create a "range" object and syntax for checking if a value is in the range, like y in x..z in Kotlin. More on reddit.com
🌐 r/ProgrammingLanguages
56
41
March 19, 2021
Why are the usual operator precedence rules are the way they are?
The (not, and, or) triple is kinda like (unary -, *, +) triple except for booleans. This is how the logic expressions are usually written in math (DNF) and programming languages just copy that. As to why it is that way in math, I think that in most cases, it reduces the amount of parentheses needed. More on reddit.com
🌐 r/ProgrammingLanguages
39
14
October 19, 2022
People also ask

Can I compare strings using comparison operators in Python?
Yes, you can compare strings using comparison operators in Python.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › what are comparison operators in python
Comparison Operators in Python: A Comprehensive Guide
How do comparison operators work with different data types?
These operators evaluate different data types based on their inherent properties, such as numerical value or lexicographical order.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › what are comparison operators in python
Comparison Operators in Python: A Comprehensive Guide
What are the six comparison operators?
The six comparison operators are 1) == or equal to, 2) != or not equal to, 3) &gt; or greater than, 4) &gt;= or greater than or equal to, 5) &lt; or less than, and 6) &lt;= or less than or equal to. They can be used to compare different values in Python, such as integers or strings.
🌐
study.com
study.com › courses › computer science courses › computer science 113: programming in python
Python Comparison Operators | Usage & Examples - Lesson | Study.com
🌐
Tutorialspoint
tutorialspoint.com › home › python › python comparison operators
Python Comparison Operators
February 21, 2009 - Comparison operators in Python are very important in Python's conditional statements (if, else and elif) and looping statements (while and for loops). The comparison operators also called relational operators.
🌐
Educative
educative.io › answers › what-are-comparison-operators-in-python
What are comparison operators in Python?
Comparison or relational operators in Python are used to compare variables with values.
🌐
Simplilearn
simplilearn.com › home › resources › software development › what are comparison operators in python
Comparison Operators in Python: A Comprehensive Guide
July 31, 2025 - Dive deep into Python comparison operators. Understand how True, False results enhance coding efficiency and decision-making in your Python scripts.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Find elsewhere
🌐
Study.com
study.com › courses › computer science courses › computer science 113: programming in python
Python Comparison Operators | Usage & Examples - Lesson | Study.com
August 25, 2020 - The six comparison operators are 1) == or equal to, 2) != or not equal to, 3) > or greater than, 4) >= or greater than or equal to, 5) < or less than, and 6) <= or less than or equal to.
🌐
DataCamp
campus.datacamp.com › courses › intermediate-python-for-finance › control-flow-and-logic
Comparison operators | Python
The less than or equal operator returns True if the left item is less than or equal in value to the right one. Here we compare an small int against a larger one, returning True. A float which evaluates as being equal to an int, also returning True.
🌐
Real Python
realpython.com › python-operators-expressions
Operators and Expressions in Python – Real Python
January 11, 2025 - Python operators enable you to perform computations by combining objects and operators into expressions. Understanding Python operators is essential for manipulating data effectively. This tutorial covers arithmetic, comparison, Boolean, identity, membership, bitwise, concatenation, and repetition operators, along with augmented assignment operators.
🌐
GeeksforGeeks
geeksforgeeks.org › python › relational-operators-in-python
Comparison Operators in Python - GeeksforGeeks
September 17, 2025 - Comparison operators (or Relational) in Python allow you to compare two values and return a Boolean result: either True or False. Python supports comparison across different data types, such as numbers, strings and booleans.
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › comparison operators in python
Comparison Operators in Python - Shiksha Online
November 22, 2022 - We have also discussed the following Python operators in previous articles: ... Comparison operators in Python, also called relational operators, are used to compare two operands.
🌐
Python
docs.python.org › 3 › library › operator.html
operator — Standard operators as functions
Perform “rich comparisons” between a and b. Specifically, lt(a, b) is equivalent to a < b, le(a, b) is equivalent to a <= b, eq(a, b) is equivalent to a == b, ne(a, b) is equivalent to a != b, gt(a, b) is equivalent to a > b and ge(a, b) ...
Top answer
1 of 3
77

From Comparing Sequences and Other Types in the Python tutorial:

The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.

See also the Wikipedia article about lexicographical order.

2 of 3
27

Since I didn't find the explanation of list/tuple comparison using "lexicographical ordering" particularly illuminating at first, here's an attempt to explain it "in my own words". First, here are some example lists that are referred to in the explanation below:

a = [1, 2, 3]
b = [1, 2, 10]
c = [1, 2, 3, 100]
d = [1, 2, 3]
e = [1, 2, 3, 4, 'a']
f = ['a', 'b', 'c']

The pair of items at each index are compared in turn. So, comparing a to b will result in 1 being compared to 1, 2 being compared to 2, and 3 being compared to 10.

The comparison of pairs will stop when either an unequal pair of items is found or--if the lists are different lengths--the end of the shorter list is reached.

For example, when comparing a and b, comparisons will stop when 3 and 10 are compared. When comparing b and c, comparisons will stop when 10 and 3 are compared.

As soon as an unequal pair is found, the overall result is the result of comparing the unequal items. This applies whether the lists are the same length or not--for example, list b is greater than list c because the 100 in c never comes into play.

For example, when comparing a to b, the overall result will be the result of comparing 3 to 10. a < b -> True because 3 is less than 10. a > b -> False because 3 is not greater than 10. a == b -> False because 3 does not equal 10.

If one of the lists is shorter and its N items are equal to the first N items of the longer list, as with a and c, the shorter list will be considered less than the longer list (so a is less than c).

Two lists will compare as equal only if they're the same length and all pairs of items compare as equal.

Note about types: if the items in a pair aren't comparable, the comparison will fail with a TypeError as usual. For example, comparing list a to f will fail when 1 is compared to 'a'. But also note that lists d and e can be compared since the 'a' in e is never compared to anything in d.

🌐
RichardKilleen
richardkilleen.co.uk › home › blog › python for network engineers blog 15: python comparison operators
Python for Network Engineers Blog 15: Python Comparison Operators - RichardKilleen
September 11, 2025 - Learn Python comparison operators for network engineers with practical examples. Master all 6 operators, chaining, and avoid common mistakes
🌐
W3Schools
w3schools.com › python › python_operators.asp
Python Operators
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... Operators are used to perform operations on variables and values.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-operators
Python Operators - GeeksforGeeks
In Python, Comparison (or Relational) operators compares values.
Published   December 2, 2025
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
5 days ago - Comparisons can be chained arbitrarily; for example, x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false). ... Unless stated otherwise, objects of different types never compare equal. The == operator is always defined but for some object types (for example, class objects) is equivalent to is.
🌐
Python
python-list.python.narkive.com › Y36HKs9i › dynamic-comparison-operators
Dynamic comparison operators
URL: <http://mail.python.org/pipermail/python-list/attachments/20120524/8cf18be0/attachment.html> ... Post by mlangenhoven I would like to pass something like this into a function test(val1,val2,'>=') You can pass an operator as an argument to your function.
🌐
Intellipaat
intellipaat.com › home › blog › python comparison operators
Python Comparison Operators (With Examples)
October 17, 2025 - They help you compare two values and evaluate their relationship. These operators are mostly used in conditional statements like if, elif, and while to control the flow of the program based on specific criteria.