Last updated on January 11, 2023
Comparison operator compares two values in the if-statement. There are six comparison operators in Python programming language which are listed below,
Each has a symbol to use in the if-statement. Let’s learn each by an example.
1. Less Than Operator:
Let’s suppose we have different prices for new and old bikes that need to be compared through less than operator.
newBikePrice = 500
oldBikePrice = 300
if newBikePrice < oldBikePrice:
print("True")
else:
print("False")
// Output: False
In this example, there are two variables in which first is newBikePrice
that has an assigned value 500 and the second oldBikePrice
has value 300. Both data types are integers. This if-statement returns false because newBikePrice
is not less than oldBikePrice
.
2. Greater Than Operator:
If we take the same variable that we used to understand less than operator, then it will be easier to understand greater than operator as well.
newBikePrice = 500
oldBikePrice = 300
if newBikePrice > oldBikePrice:
print("True")
else:
print("False")
// Output: True
In this example, we used greater than operator in the if-statement which returns true. It is because the newBikePrice
value is higher than the oldBikePrice
.
3. Equal Operator:
To understand equal operators, let’s assume 1kg of apples and grapes price is $4.
apple1KG = 4
grapes1KG = 4
if apple1KG == grapes1KG:
print("True")
else:
print("False")
// Output: True
In this example, we used a double equal symbol to check if apples and grapes prices are equal. In this case, since both prices are the same that’s why in the output we get true.
Equal operator also checks data type as well. To check it, let’s convert the grapes’ price into string.
apple1KG = 4
grapes1KG = “4”
if apple1KG == grapes1KG:
print("True")
else:
print("False")
// Output: False
In this case, both prices are the same but the grape variable has a string value. That’s why we false in the output.
4. Not Equal Operator:
The not equal operator checks if the first value doesn’t match or equivalent with the second value. Let’s say a fifteen year old boy’s height is 5 feet and a three-year-old kid’s height is 2 feet. As we know they have different heights, so let’s check it through the not equal operator.
boyHeight = 5
kidHeight = 2
if boyHeight != kidHeight:
print("True")
else:
print("False")
# Output: True
In this example, we have two variables that store different heights. In the if-statement we used not equal operator to check if they are equivalent. In the result we got true because both heights are not equivalent.
It also checks the variable’s data type. Let’s check it by adding modifications in the assigned value.
boyHeight = 5
kidHeight = "5"
if boyHeight != kidHeight:
print("True")
else:
print("False")
# Output: True
In this example, we replaced the kid height from 2 to 5 also if you noticed that there are quotes which make the variable string. So now we have the same value in both variables with different data types.
In the output we get still true which is because the kid height variable’s data type doesn’t match with boy height variable.
5. Less Than or Equal:
Less than or equal means it checks if the first value is either less or equivalent with the second value. Let’s understand this operator through below example,
apple1KG = 3
grapes1KG = 4
if apple1KG <= grapes1KG:
print("True")
else:
print("False")
# Output: True
In this example, a 1 kg apple’s price is $3 and a 1 kg grape’s price is $4. That means the price is less than the price. That’s why in the response of the above condition we got true.
6. Greater Than or Equal:
The greater than or equal operator checks if the first value in if-statement is greater than the second one. For example, the 1 kg apple price is $5 and 1 kg grapes price is $4 which is less than apple’s price. Let’s check by using greater than operator.
apple1KG = 5
grapes1KG = 4
if apple1KG >= grapes1KG:
print("True")
else:
print("False")
# Output: True
In this example, we get true in the output which is because apple price is greater than grapes price.