Placement Prep

Python Comparison Operators: All 6 With Examples

Python has six comparison operators that each return True or False. This guide covers ==, !=, >, <, >=, and <= with examples for numbers, strings, and chaining.

By FACE Prep Team 4 min read
python comparison-operators relational-operators python-basics control-flow placement-prep

Python has six comparison operators, and every if statement, while loop, and list filter you write depends on at least one of them.

The Six Python Comparison Operators

Per the Python language reference, comparison operators compare two values and return one of two results: True or False. There are no other possible return values.

OperatorMeaningExampleResult
==Equal to10 == 10True
!=Not equal to10 != 20True
>Greater than10 > 5True
<Less than10 < 5False
>=Greater than or equal to10 >= 10True
<=Less than or equal to5 <= 10True

These six operators form the core of every condition in Python. Every if block, while guard, and list comprehension filter collapses to a boolean produced by at least one of them. Here they are in a single code block:

a = 10
b = 20

print(a == b)   # False
print(a != b)   # True
print(a > b)    # False
print(a < b)    # True
print(a >= b)   # False
print(a <= b)   # True

Numbers, Floats, and Edge Cases

Most number comparisons work exactly as expected. Two edge cases catch beginners and show up in placement coding rounds.

Int-float equality. 5 == 5.0 returns True. Python’s == compares numeric values, not types. The integer 5 and the float 5.0 carry the same value, so they are equal. If you also need to check type, use isinstance() separately.

The not operator. This is not a comparison operator itself, but it pairs with them in almost every program. not 5 evaluates to False, not 5. The not keyword converts its operand to a boolean first: 5 is truthy, so not 5 is not True, which is False.

Negative numbers work without surprises: -5 < -3 returns True because -5 is smaller on the number line.

print(5 == 5.0)    # True  -- int equals float of same value
print(not 5)       # False -- 5 is truthy, so not 5 is False
print(-5 < -3)     # True
print(100 >= 100)  # True
print(7 != 7.0)    # False -- 7 and 7.0 hold the same value

Chained Comparisons: Python’s Range-Check Shortcut

Python allows you to chain comparisons in a single expression. Writing 1 < x < 10 is valid Python and means exactly 1 < x and x < 10. Most other languages (C, Java, JavaScript) do not support this syntax, which can surprise developers who come from those backgrounds.

Python evaluates chained comparisons left to right:

  • 1 < 2 < 3 evaluates as 1 < 2 and 2 < 3, returning True
  • 10 < 5 < 20 evaluates as 10 < 5 and 5 < 20; since 10 < 5 is False, the result is False
  • 1 <= 5 <= 10 returns True
  • 5 != 3 != 7 evaluates as 5 != 3 and 3 != 7, returning True (note: this does not check whether 5 and 7 are equal)

A practical use: instead of if age >= 18 and age <= 60:, write the chained form:

if 18 <= age <= 60:
    print("Eligible")

Both produce identical output. The chained form is the standard Python style for range checks.

One precision note from the Python operator precedence table: all six comparison operators share the same precedence level, below arithmetic operators and above the not keyword. So 1 + 1 > 1 first computes 1 + 1 = 2, then evaluates 2 > 1, which is True.

Comparing Strings and Other Types

Python compares strings lexicographically: character by character, from left to right, using Unicode code points. Comparison stops at the first position where the two characters differ.

  • "apple" < "banana" returns True (because 'a' has a lower code point than 'b')
  • "apple" == "apple" returns True
  • "apple" != "Apple" returns True (comparisons are case-sensitive; uppercase letters have lower code points than lowercase)
  • "Apple" < "apple" returns True ('A' is code point 65; 'a' is 97)
  • "cat" > "car" returns True ('t' has a higher code point than 'r')

This matters in placement aptitude and coding rounds that ask you to sort names alphabetically or validate user input. Python’s built-in sorted() and list.sort() use this same comparison logic on strings, so mastering == and < on strings is the same skill that powers those functions.

You can also compare lists. Python compares them element by element, using the same left-to-right logic as strings. [1, 2] < [1, 3] returns True because the first elements are equal and the second elements compare as 2 < 3.

Using Comparisons in Control Flow

Comparison operators do their heaviest work inside the three constructs that drive most Python programs: if blocks, while loops, and list comprehensions.

if / else

marks = 75

if marks >= 60:
    print("Pass")
else:
    print("Fail")
# Output: Pass

For a practical extension of the > operator, the find the greater of two values walkthrough applies it directly in an if block. The greatest of three numbers in Python example chains if-elif-else to compare three operands in sequence.

while loop

count = 0
while count < 5:
    print(count)
    count += 1
# Prints: 0  1  2  3  4

The count < 5 expression re-evaluates after every iteration. Once count reaches 5, the condition is False and the loop exits. Forgetting to increment count inside the loop produces an infinite loop, a common beginner mistake.

List comprehension filter

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [n for n in numbers if n % 2 == 0]
print(even_numbers)  # [2, 4, 6]

The if n % 2 == 0 part is a standard equality comparison: modulo result equal to zero means the number is even. The comprehension collects only the elements where that condition is True.

For more hands-on exercises that combine conditionals, loops, and list operations, the Python basic programs practice collection has problems that rely on comparison operators throughout.

The chained 18 <= age <= 60 form and the list-filter pattern from the examples above both reappear in AI application code: checking whether a model confidence score falls within an acceptable range, filtering API responses by value, or capping token counts. TinkerLLM (₹299) runs Python-based LLM notebooks where you will write exactly these conditions against live model outputs, not just toy datasets.

Primary sources

Frequently asked questions

What is the difference between == and is in Python?

The == operator checks value equality: whether two objects hold the same data. The is operator checks identity: whether both variables point to the exact same object in memory. Use == for comparing values; reserve is for checking against None.

Can Python comparison operators compare strings?

Yes. Python compares strings lexicographically, character by character from left to right using Unicode code points. The string 'apple' is less than 'banana' because 'a' has a lower code point than 'b'.

What is a chained comparison in Python?

A chained comparison links multiple comparisons in one expression, such as 1 < x < 10. Python evaluates this as 1 < x and x < 10, left to right. This syntax is valid Python but not C, Java, or JavaScript.

Why does 5 == 5.0 return True in Python?

Python's == operator compares values, not types. The integer 5 and the float 5.0 represent the same numeric value, so == returns True. For type-aware comparison, use isinstance() to check the type before comparing.

Do all six comparison operators have the same precedence?

Yes. All six comparison operators share the same precedence level, lower than arithmetic operators and higher than the not keyword. When multiple comparisons appear in one expression, Python evaluates left to right.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF