Comparison operator
We compare an operator with a value
Python Comparison Operators
These operators compare the values on either side of them and decide the relation among them. They are also called Relational operators.
Assume variable a holds 10 and variable b holds 20, then −
Operator | Description | Example |
---|---|---|
== | If the values of two operands are equal, then the condition becomes true. | (a == b) is not true. |
!= | If values of two operands are not equal, then condition becomes true. | (a != b) is true. |
<> | If values of two operands are not equal, then condition becomes true. | (a <> b) is true. This is similar to != operator. |
> | If the value of left operand is greater than the value of right operand, then condition becomes true. | (a > b) is not true. |
< | If the value of left operand is less than the value of right operand, then condition becomes true. | (a < b) is true. |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | (a >= b) is not true |
If the person is taller than 6 foot
He is a tall guy
Otherwise, if he is less than 6 foot
He is Not a tall guy
Otherwise, he is not short or tall
height = 6
if height > 6:
print ("he is a tall guy")
else:
print("he is not a tall guy")
Now let's change the values
So the height >6 on line 2 is a boolean expression
We can use different comparison operators.., like <= (less or equal to) ,>=(greater or equal to)..like the example
we can use the (==) or the (!=) but the result is different because we are not producing a boolean value
Is the name is 2 characters long
the name must be at least 2 characters long
Otherwise the name is 20 characters long
the name must be a maximum of 20 characters long
Otherwise, the name looks wonderful!
First part: defining the name length
Second Part: Short name
Third Part: Long name
Fourth Part
Fith Part:Good size name
Comparison operator
Reviewed by ohhhvictor
on
May 05, 2019
Rating:
No comments: