L

learnasyoulike

time


Comparison Operators

Table of contents:



Python Comparison Operators

The boolean data-type

In the world of programming, we often need to know whether an expression is true or false.

For example, is the expression 2 > 1 true, or false?
The expression is true, because 2 is greater than 1.

Is the expression 7 < 5 true, or false?
The expression is false, because 7 is not less than 5.

Such expressions are called boolean expressions.

Table of contents:



A data-type, called boolean type, is used to define such expression. The result of such expression is either a true, or a false value. This data-type is used to check some condition; and action is taken, or not taken, depending on the result of the checking.

In python, a boolean expression, after evaluation, results in either a True or False (strictly with capital T and F). Similarly, a boolean variable will contain either True or False value, nothing else. The technical name of boolean data-type is 'bool'.


In this page we are going to use a series of expressions which will always result in a True or False values, depending on the condition in the expression.


Six types of comparison operators

In the world of programming, comparison operators are used when we want to compare one operand with another. The operands may contain values, or variables, even expresssions. The result of such comparison is always either True or False. Python has six comparison operators which are also known as relational operators.


OperatorName
== Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to


These comparison operators always operate on two operands, hence they are binary operators.

The == operator

The equal to operator == compares whether two values/operands/variables/expressions are equal or not.
if they are equal, the expression is evaluated to True.
if they are not equal, the expression is evaluated to False.

Take the expression 5 == 5; note that there are two equal signs. The == is a comparison operator in python. In 5 == 5, there are two operands and one operator(== is a single operator). When evaluating the expression, the interpreter compares the two operands 5 and 5. If they are equal, the value of the expression becomes True, otherwise False.

Since 5 is equal to 5, the expression 5 == 5 is True.
Since 6 is not equal to 4, the expression 6 == 4 is False.
5 == 5 may be translated in English as an interrogative sentence as "Is 5 equal to 5?"
The sentence "5 is equal to 5" is True.
The sentence "6 is equal to 4" is False.
We check this in the IDLE window:



Python confirms:  yes, 5 == 5 is True,
and disagrees:  no, 6 == 4 is not True, so False.

5 == 5 and 6 == 4 are expressions in the same way
as 5 + 5, 5 - 5, 5 * 5, 5 / 5 are expressions.


Expression Evaluated to Data-type
5 + 5 10int
5 * 5 25int
5 == 5 Truebool
6 - 4 2int
6 / 4 1.5float
6 == 4 Falsebool


Because -1 == -1 evaluates to True, all the following three expressions are True. Note that the print() is omitted for brevity.

The != operator

The not equal to operator != compares whether two operands are unequal or not. If the operands are unequal, output is True. If they are equal, output is False.

The != operator is just opposite to the == operator.

The expression 1 != 2 will evaluate to True;
(∵ the sentence "1 is not equal to 2" is True).
The expression 1 != 1 will evaluate to False;
(∵ the sentence "1 is not equal to 1" is False).


The < operator

The less than operator < compares whether the first operand is less than the second one. If it is less than, output is True. If it is not less than, output is False. The expression 3 < 9 will evaluate to True;
(∵ the sentence "3 is less than 9" is True).
The expression 9 < 3 will evaluate to False;
(∵ the sentence "9 is less than 3" is False).



Note: In the world of computer, not only numeric values, but also strings can be compared with each other. The character "b" is greater than "a", because "b" comes after "a".

Because the character "b" is greater than "a", the word "banana" is also greater than the word "apple". So the expression "banana" < "apple" returns a False.

The <= operator

The less than or equal to operator <= compares whether the first operand is less than the second one, or equal to the second one.

If it is less than, or equal to, the expression is evaluated to True. If it is not less than, and not equal to, the expression is evaluated to False.

The expression 77 <= 88 evaluates to True;
(∵ the sentence "77 is either less than, or equal to 88" is true).

The expression 88 <= 77 evaluates to False;
(∵ 88 is neither less than, nor equal to 77).

The expression 3.145 <= 3.145 evaluates to True;
(∵ 3.145 is not less than 3.145, but equal to 3.145).

The > operator

The greater than operator > compares whether the first operand is greater than the second one.
If it is greater than, output is True. If it is not greater than, output is False.


The word "car" is greater than "bus", because the first letter of "car" ("c") comes after first letter of "bus" ("b").

The expression "WAN" > "LAN" will evaluate to True;
(∵ the "W" comes after "L").
The expression "LAN" > "WAN" will evaluate to False;


The >= operator

The greater than or equal to operator >= compares whether the first operand is greater than the second one, or equal to the second one.

If it is greater than, or equal to, the output of the expression is True. If it is not greater than, and not equal to, the expression is evaluated to False.

The expression 3.145 >= 2.718 evaluates to True;
(∵ 3.145 is greater than 2.718, though not equal to 2.718; one condition is true).

The expression 2.718 >= 3.145 evaluates to False;
(∵ 2.718 is neither greater than, nor equal to 3.145; both conditions are false).

The expression "python" >= "python" evaluates to True;
(though "python" is not greater than "python", but equal to "python"; one condition is true).



Now observe the following:

In python, 100 is not equal to "100" because their data-type is not the same; the former is of 'int' type, and the latter is of 'str' type.

Handling 'bool' type variables

The result of the comparison can be stored in variables as done in the following:

The expression 2 == 3 is evaluted to False, then the result is stored in variable  c which is a boolean variable.



The first characters of "ram" and "rom" are the same("r"). So a match of the second characters "a" and "o" is checked. Since "a" comes before "o", "a" > "o" is False. So z is assigned the value of False. It is a boolean variable.



We can even directly assign True or False values to variables:

In the above, is_prime and is_adult are of 'bool' type(boolean) variables.




In the arithmetic operators page we have come to know about the modulo operator %. If an even number is divided by %2, the remainder will be 0. If an odd number is divided by 2, the remainder will be 1. We can use % to check if a number is even or odd.

In the second line, the number % 2 part is evaluated to 1.
Then the 1 == 0 part is evaluated to False. Then this boolean value False is assigned to variable is_even.

In the third line,

Out of the three variables above, number is of 'int' type, is_even and is_odd are of 'bool' type.




In IDLE window, try to evaluate and print different expressions containing different comparison operators and operands to grab clear understanding of the above contents.




--- x ---









Want to leave a message for me?