Skip to content

Module 4 · Operators — topic 9 of 16

Comparison operators

The six comparison operators (==, !=, <, >, <=, >=) produce True or False. Learn how they compare numbers, strings and sequences, and how to chain comparisons.

Beginner 12 min read·Lesson 52 of 306 in Python Programming

Learning objectives

  • Use all six comparison operators
  • Compare strings and sequences correctly
  • Write chained comparisons such as 0 <= x < 10

Definition

The six comparison operators (==, !=, <, >, <=, >=) produce True or False. Learn how they compare numbers, strings and sequences, and how to chain comparisons.

Introduction

Comparison operators ask questions about two values and answer with True or False. Is the score at least 40? Are the two passwords equal? Is this name alphabetically before that one?

Why it matters

Every decision in a program starts with a comparison. Choosing the right operator, especially at boundaries such as >= versus >, is the difference between a student passing and failing on exactly 40 marks.

Detailed explanation

OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal to5 != 3True
<less than3 < 5True
>greater than3 > 5False
<=less than or equal5 <= 5True
>=greater than or equal4 >= 5False

Numbers of different types compare by value: 1 == 1.0 is True.

Strings compare character by character using Unicode code points, so upper-case letters come before lower-case: "Zebra" < "apple" is True. Lower-case both sides for alphabetical comparisons.

Lists and tuples compare element by element: [1, 2, 3] < [1, 3] is True because 2 < 3 at the second position.

Comparisons can be chained: 0 <= score <= 100 means 0 <= score and score <= 100, evaluating score only once.

Ordering incompatible types, such as 5 < "5", raises TypeError, while == simply returns False.

Be careful comparing text that represents numbers: "10" < "9" is True because the character 1 comes before 9. Convert to int or float before comparing quantities read from input or files.

Syntax

Python
a == b
a != b
low <= value < high
  • Each comparison evaluates to True or False.
  • == compares values; = assigns.
  • Chained comparisons read like mathematics.

Code examples

Comparing numbers

Python
score = 40
print(score >= 40, score > 40, score == 40, score != 40)
print(1 == 1.0)
Output
True False True False
True

Comparing strings

Python
print("apple" < "banana")
print("Zebra" < "apple")
print("Zebra".lower() < "apple")
Output
True
True
False

More examples

Validating ranges

Python
def grade_band(score):
if not 0 <= score <= 100:
return "invalid"
if score >= 75:
return "distinction"
if score >= 40:
return "pass"
return "fail"
for s in [101, 75, 74, 40, 39, -1]:
print(s, grade_band(s))
print([1, 2, 3] < [1, 3], (2, "b") > (2, "a"))
Output
101 invalid
75 distinction
74 pass
40 pass
39 fail
-1 invalid
True True

Step-by-step explanation

  • The chained comparison 0 <= score <= 100 rejects anything outside the valid range.
  • >= 75 and >= 40 include the boundary scores in the higher band.
  • Testing 75, 74, 40 and 39 checks both sides of each boundary.
  • Lists and tuples compare element by element until a difference is found.

Expected output

Running “Validating ranges” prints exactly:

TextOutput
101 invalid
75 distinction
74 pass
40 pass
39 fail
-1 invalid
True True

Practical applications

  • Form validation checks that ages and quantities fall within allowed ranges.
  • Sorting algorithms rely entirely on < comparisons.
  • Login systems compare stored and entered values for equality.

Best practices

  • Test boundary values explicitly.
  • Use chained comparisons for ranges.
  • Normalise case before comparing text alphabetically.

Common mistakes

= instead of ==

Avoid

if total = 100:

Prefer

if total == 100:

A single = is assignment and is a SyntaxError in a condition.

Off-by-one at boundaries

Avoid

if marks > 40: # 40 fails

Prefer

if marks >= 40: # 40 passes

Decide whether the boundary value belongs to the group and choose > or >= accordingly.

Key takeaways

  • ==, !=, <, >, <=, >= compare values and return Booleans.
  • Strings compare by character code; sequences element by element.
  • Chained comparisons such as a < b < c are allowed.

Try it yourself

  1. 1
    Write a check that a time given as hours is between 9 and 17 inclusive using a chained comparison.
  2. 2
    Predict and check: "10" < "9" and 10 < 9. Explain the difference.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic