Comparison Operators or Relational Operators in Python
A comparison operator in python, also called python relational operators are used to establish some sort of relationship between the two operands. Some of the relevant examples could be less than, greater than or equal to operators. Relational operators compares the values of two operands and returns TRUE or FALSE based on whether the condition is met.
Operator | uses |
== | Equal operator |
!= | Not Equal operator |
< | Less than operator |
> | Greater than operator |
<= | Less than or equal to operator |
>= | Greater than or equal to operator |
The program is using comparison operators in Python to compare the values of two variables a and b.
- a = 20: This line assigns the value 20 to the variable a.
- b = 20: This line assigns the value 20 to the variable b.
- print(a == b): This line uses the comparison operator == to check if the value of a is equal to the value of b. The output will be True because 20 is equal to 20.
- print(a != b): This line uses the comparison operator != to check if the value of a is not equal to the value of b. The output will be False because 20 is equal to 20.
- print(a > b): This line uses the comparison operator > to check if the value of a is greater than the value of b. The output will be False because 20 is not greater than 20.
- print(a < b): This line uses the comparison operator < to check if the value of a is less than the value of b. The output will be False because 20 is not less than 20.
- print(a >= b): This line uses the comparison operator >= to check if the value of a is greater than or equal to the value of b. The output will be True because 20 is equal to 20.
- print(a <= b): This line uses the comparison operator <= to check if the value of a is less than or equal to the value of b. The output will be True because 20 is equal to 20.
Source Code
# Comparison Operators or Relational Operators
"""
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
"""
a = 20
b = 20
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
To download raw file
Click Here
Output
True
False
False
False
True
True