What are Comparison Operators in Python

What are Comparison Operators in Python

In Python, comparison operators are used to compare the values of two variables. These operators return a Boolean value (either True or False) based on the result of the comparison.


For example, the operator == tests for equality. If the values of the two operands are equal, the operator returns True. Otherwise, it returns False. For example:


Copy code

a = 5

b = 10


# check if a is equal to b

c = a == b

print(c)  # Output: False


# check if a is not equal to b

c = a != b

print(c)  # Output: True

Similarly, the operator > tests for greater than. If the value of the left operand is greater than the value of the right operand, the operator returns True. Otherwise, it returns False. For example:


Copy code

a = 5

b = 10


# check if a is greater than b

c = a > b

print(c)  # Output: False


# check if a is less than b

c = a < b

print(c)  # Output: True

In addition to these operators, Python provides several other comparison operators that can be used to compare the values of two variables. For example, the operator <= tests for less than or equal to, and the operator >= tests for greater than or equal to.



Overall, comparison operators are an important part of Python, and they can be used to make decisions in your code based on the values of variables. They are often used in combination with if statements and while loops to control the flow of a program.

Comments