Course Progress15% complete
🐍 Python Basics Topic 15 / 100
⏱ 7 min read

Comparison Operators

==, !=, <, >, <=, >= — asking Python questions about values and getting True or False back.

"A comparison operator asks a question. Python answers with True or False. All program logic flows from this."

— ShurAI

What Are Comparison Operators?

Comparison operators compare two values and always return a boolean — either True or False. You will use them constantly in if-statements, while loops, and any place where your program needs to make a decision. Think of them as asking Python a yes-or-no question.

Operator Meaning Example Result Read aloud as
== Equal to 5 == 5 True "is equal to"
!= Not equal to 5 != 3 True "is not equal to"
> Greater than 10 > 3 True "is greater than"
< Less than 3 < 10 True "is less than"
>= Greater than or equal to 5 >= 5 True "is at least"
<= Less than or equal to 3 <= 2 False "is at most"

== vs = — A Critical Distinction

This trips up every beginner at least once. These two look similar but do completely different things:

=
Assignment
Stores a value into a variable.
x = 5 → "Set x to 5"
==
Comparison
Asks if two values are equal.
x == 5 → "Is x equal to 5?"
python
x = 10          # assignment — x now holds 10

print(x == 10)  # True  — is x equal to 10? yes
print(x == 5)   # False — is x equal to 5? no
print(x != 5)   # True  — is x not equal to 5? yes

Comparing Numbers

python
score  = 75
target = 80

print(score >= 60)          # True  — passed minimum
print(score >= target)       # False — did not reach target
print(score == 75)           # True
print(score != target)       # True  — score and target differ
print(target - score == 5)  # True  — expressions work too

Comparing Strings

You can compare strings too. Python compares them alphabetically (technically by Unicode value). Uppercase letters come before lowercase:

python
print("apple" == "apple")   # True
print("apple" == "Apple")   # False — case sensitive!
print("banana" > "apple")   # True  — b comes after a
print("abc" < "abd")        # True  — c comes before d

# Useful for case-insensitive comparison
user_input = "YES"
print(user_input.lower() == "yes")   # True — safe comparison

Chained Comparisons — Python's Elegant Shortcut

Python lets you chain comparisons naturally, just like in maths. This is unique to Python and very readable:

python
age   = 25
score = 72
temp  = 36.6

# Python — readable chained comparison
print(18 <= age <= 65)         # True — working age
print(60 <= score < 80)         # True — B grade range
print(36.1 <= temp <= 37.2)    # True — normal body temp

# Equivalent without chaining — less readable
print(18 <= age and age <= 65) # same result, but wordier

Real Example — Exam Grade System

python
name  = input("Student name: ")
marks = int(input("Marks (0-100): "))

# Determine grade using comparison operators
if marks >= 90:
    grade = "A+"
elif marks >= 80:
    grade = "A"
elif marks >= 70:
    grade = "B"
elif marks >= 60:
    grade = "C"
elif marks >= 35:
    grade = "D"
else:
    grade = "F"   # marks < 35

passed = marks >= 35    # True or False

print(f"\n{name}: {marks}/100 → Grade {grade}")
print(f"Result: {'PASS' if passed else 'FAIL'}")
terminal
Student name: Kavya
Marks (0-100): 84

Kavya: 84/100 → Grade A
Result: PASS

"Every if-statement, every filter, every decision in code traces back to a comparison operator quietly returning True or False."

— ShurAI

🧠 Quiz — Question 1

What is the difference between = and ==?

🧠 Quiz — Question 2

What does "apple" == "Apple" return?

🧠 Quiz — Question 3

What does 18 <= age <= 65 check?

🧠 Quiz — Question 4

What type does every comparison operator always return?