Lessons
Courses
Notes in current language not found. Defaulting to English.

Comparison Operators in Python [ English ]

1. Introduction

Comparison operators (also called relational operators) are used to compare two values or expressions. The result of a comparison is always a Boolean value:

  • True
  • False

These operators are widely used in decision-making and conditions.


2. Types of Comparison Operators

OperatorNameDescriptionExample
==Equal toChecks if values are equal5 == 5 → True
!=Not equal toChecks if values are different5 != 3 → True
>Greater thanLeft value is greater5 > 3 → True
<Less thanLeft value is smaller3 < 5 → True
>=Greater or equalGreater or equal5 >= 5 → True
<=Less or equalSmaller or equal3 <= 5 → True

3. Detailed Explanation


3.1 Equal To (==)

Checks whether two values are equal.

print(5 == 5)print(5 == 3)

Output

TrueFalse

3.2 Not Equal To (!=)

Checks whether two values are not equal.

print(5 != 3)print(5 != 5)

Output

TrueFalse

3.3 Greater Than (>)

Checks if the left value is greater than the right value.

print(10 > 5)

Output

True

3.4 Less Than (<)

Checks if the left value is less than the right value.

print(3 < 7)

Output

True

3.5 Greater Than or Equal To (>=)

print(5 >= 5)print(6 >= 5)

Output

TrueTrue

3.6 Less Than or Equal To (<=)

print(4 <= 5)print(5 <= 5)

Output

TrueTrue

4. Comparison with Variables

a = 10b = 20 print(a < b)print(a == b)

5. Comparison with Strings

print("apple" == "apple")print("apple" > "banana")

Explanation

  • Python compares strings based on ASCII/Unicode values

6. Chained Comparisons

Python allows chaining of comparison operators.

x = 10 print(5 < x < 20)

Output

True

7. Comparison with Different Data Types

print(5 == 5.0)

Output

True

Explanation

  • Python compares values, not just types

8. Practical Example

age = 18 if age >= 18: print("Eligible to vote")else: print("Not eligible")

9. Important Points

  • Always returns True or False
  • Used in conditions (if, while)
  • Can compare numbers, strings, and variables
  • Supports chaining

10. Summary

  • Comparison operators are used to compare values.

  • They return Boolean results.

  • Types include:

    • ==, !=, >, <, >=, <=
  • Useful for decision-making and control flow.

  • Supports chained comparisons.