Comparison Operators

Comparison Operators in Python are used to compare two values and check whether a condition is True or False.

Real-Life Example

Comparison operators are like asking questions:

  • Is 10 greater than 5? → True
  • Is 8 equal to 10? → False

Just like answering Yes/No, Python gives True/False.



Explanation

These operators help in decision-making in programs.

They:

  • Compare values like numbers or variables
  • Return Boolean results (True/False)
  • Are used in if statements, loops, and logical conditions

Types of Comparison Operators

  • == (Equal to) → Checks if two values are equal
  • != (Not equal to) → Checks if values are different
  • > (Greater than) → Checks if left value is greater
  • < (Less than) → Checks if left value is smaller
  • >= (Greater than or equal to)
  • <= (Less than or equal to)

Example 1 

x = 10
y = 20

print(x > y)
print(x == y)

Output:

False
False

Example 2

a = 15
b = 10

print("Is a greater than b?", a > b)
print("Is a equal to b?", a == b)
print("Is a not equal to b?", a != b)

Output:

Is a greater than b? True
Is a equal to b? False
Is a not equal to b? True

Explanation

  • a > b → checks if a is greater than b
  • a == b → checks if both values are equal
  • a != b → checks if values are different

Common Beginner Mistakes

  •  Using = instead of ==
  •  Confusing != and ==
  •  Not understanding True/False
  •  Wrong comparison logic

Key Points 

  • Comparison operators return True or False
  • Used in decision-making (if conditions)
  • Important for logical thinking in Python
  • Helps in building program flow and control

Tips 

  • Practice comparing numbers and variables
  • Use them in simple if programs
  • Understand each symbol clearly
  • Start with easy examples

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

What do comparison operators return?

Comparison operators return Boolean values.

Question 2

Which operator checks equality?

== checks if two values are equal.

Question 3

What is the output?

  print(5 < 10)

5 is less than 10

Question 4

What is the output?
print(10 != 10)

10 is equal to 10, so not equal is False.

Question 5

What is the output?

x = 5
y = 5
print(x >= y)

5 is equal to 5, so >= returns True.

Congratulations!

You've successfully mastered the knowledge check for "Comparison Operators."

For more questions and practice, click the link below:

Practice More Questions
Previous Topic Arithmetic Operators Next Topic Logical Operators