Python Comparison Operators

1. What are Comparison Operators?

Comparison operators are used to compare two values.

The result is always:

  • True

  • False

They help Python make decisions.

2. List of Comparison Operators

Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal a >= b
<= Less than or equal a <= b

3. Equal To (==)

Checks if two values are the same.

Example

a = 10
b = 10

print(a == b)

Output:

True

4. Not Equal To (!=)

Checks if two values are different.

Example

a = 10
b = 5

print(a != b)

Output:

True

5. Greater Than (>)

Checks if left value is bigger.

Example

a = 15
b = 10

print(a > b)

6. Less Than (<)

Checks if left value is smaller.

Example

print(a < b)

7. Greater Than or Equal (>=)

Checks if value is greater or equal.

Example

age = 18
print(age >= 18)

8. Less Than or Equal (<=)

Checks if value is smaller or equal.

Example

marks = 40
print(marks <= 50)

9. Comparison with Variables

Example

x = 20
y = 30

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

10. Comparison with User Input

Example

age = int(input("Enter age: "))

print(age >= 18)

11. Comparison in if Statement

Example

number = 10

if number > 5:
print("Number is greater than 5")
else:
print("Number is 5 or less")

12. Comparing Strings

Strings can also be compared.

Example

print("apple" == "apple")
print("apple" != "banana")

13. Common Beginner Mistakes

❌ Using = Instead of ==

if x = 5:

✔ Correct:

if x == 5:

❌ Comparing String with Number

age = "20"
print(age > 18)

✔ Correct:

age = int("20")
print(age > 18)

14. Simple Practice Examples

Example 1: Pass or Fail

marks = 35

print(marks >= 40)

Example 2: Check Two Numbers

a = 5
b = 10

print(a > b)
print(a == b)

Example 3: Password Match

password = "python"

user_input = input("Enter password: ")

print(user_input == password)

15. Summary (Comparison Operators)

✔ Compare two values
✔ Result is True or False
✔ Used in if–else
✔ Very important for decision making

📘 Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Logical Operators

  • if–else statements

  • Comparison Exercises

  • MCQs with answers

Just tell me 😊

You may also like...