Comparison operators in Python are used to compare values and determine the truth of a condition. They return boolean values (True or False) based on whether the comparison is true or false. Here are some common comparison operators in Python:
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal toLet's see some examples of using comparison operators in Python:
# Using '==' operator
x = 5
y = 10
result = x == y
print(result)
# Using '!=' operator
a = 20
b = 15
result = a != b
print(result)
# Using '>' operator
num1 = 25
num2 = 30
result = num1 > num2
print(result)
# Using '<' operator
i = 5
j = 7
result = i < j
print(result)
# Using '>=' operator
m = 10
n = 5
result = m >= n
print(result)
# Using '<=' operator
p = 8
q = 8
result = p <= q
print(result)
Understanding comparison operators is crucial for making decisions based on conditions and performing various logical operations in your Python programs.