0
0
Pythonprogramming~5 mins

Comparison operators in Python

Choose your learning style9 modes available
Introduction

Comparison operators help us check if things are equal, bigger, or smaller. They tell us true or false.

Checking if a password matches the saved one.
Finding out if a number is bigger than another.
Seeing if two words are the same.
Deciding if someone is old enough to enter a game.
Comparing scores to find the winner.
Syntax
Python
value1 operator value2

Operators return True or False.

Common operators: ==, !=, >, <, >=, <=

Examples
Checks if 5 is equal to 5. Result is True.
Python
5 == 5
Checks if 3 is less than 7. Result is True.
Python
3 < 7
Checks if "apple" is not equal to "orange". Result is True.
Python
"apple" != "orange"
Checks if 10 is greater than or equal to 10. Result is True.
Python
10 >= 10
Sample Program

This program compares two numbers using different comparison operators and prints True or False.

Python
a = 10
b = 20
print(a == b)   # False because 10 is not 20
print(a != b)   # True because 10 is not 20
print(a < b)  # True because 10 is less than 20
print(a >= 10) # True because a is equal to 10
OutputSuccess
Important Notes

Use double equals (==) to check equality, not a single equals (=) which assigns values.

Comparison operators always return a boolean: True or False.

You can compare numbers, text, and other types that support comparison.

Summary

Comparison operators check how two values relate.

They return True or False.

Common operators: ==, !=, >, <, >=, <=