Page 472 - ComputerScience_Class_11
P. 472
12.2.2 Comparison Operators
Comparison operators are used to compare two values. The result of a comparison is always a Boolean value, either
True or False. These operators help in making decisions in programs and controlling the flow of execution.
The following table shows the comparison operators in Python along with their description:
Operator Description
== Checks if two operands are equal.
!= Checks if two operands are not equal.
> Checks if left operand is greater than right operand.
< Checks if left operand is less than right operand.
>= Checks if left operand is greater than or equal to right operand.
<= Checks if left operand is less than or equal to right operand.
Program 5: To demonstrate the use of Comparison operators.
Program 5.py
File Edit Format Run Options Window Help
# To assign value 10 to variable a
a = 10
# To assign value 20 to variable b
b = 20
# To check if a is equal to b
print("a == b:", a == b) # To check equality
# To check if a is not equal to b
print("a != b:", a != b) # To check inequality
# To check if a is greater than b
print("a > b:", a > b) # To check greater than
# To check if a is less than b
print("a < b:", a < b) # To check less than
# To check if a is greater than or equal to b
print("a >= b:", a >= b) # To check greater than or equal
# To check if a is less than or equal to b
print("a <= b:", a <= b) # To check less than or equal
Output
a == b: False
a != b: True
a > b: False
a < b: True
a >= b: False
a <= b: True
470 Touchpad Computer Science (Ver. 3.0)-XI

