Notes
Categories

Comparison Operators in Python [ English ]

< Prev Next >

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:

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


2. Types of Comparison Operators

Operator Name Description Example
== Equal to Checks if values are equal 5 == 5 → True
!= Not equal to Checks if values are different 5 != 3 → True
> Greater than Left value is greater 5 > 3 → True
< Less than Left value is smaller 3 < 5 → True
>= Greater or equal Greater or equal 5 >= 5 → True
<= Less or equal Smaller or equal 3 <= 5 → True

3. Detailed Explanation


3.1 Equal To (==)

Checks whether two values are equal.

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

Output

True
False

3.2 Not Equal To (!=)

Checks whether two values are not equal.

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

Output

True
False

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

True
True

3.6 Less Than or Equal To (<=)

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

Output

True
True

4. Comparison with Variables

a = 10
b = 20

print(a < b)
print(a == b)

5. Comparison with Strings

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

Explanation


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


8. Practical Example

age = 18

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

9. Important Points


10. Summary

< Prev Next >