11: Operation – II (Comparison)
Operation – II (Comparison)
Comparison is the second set of operators we will study here but their actual use will be shown in Decision and Loops.
- Comparison operators work on numbers.
- But their result is not a number. The result of these operations is true or false.
- The result is required to store in some bool data type variable.
- In many situations, it is not required to store the result.
- A total of six comparison operators are used in Python.
- == (Equals)
- != (Not Equals)
- < (Less Than)
- > (Greater Than)
- <= (Less Than or Equals)
- >= (Greater Than or Equals)
- If we want to compare two numbers (store in variables or just use their values) then we use this operation.
decision1 = 10 == 10print (decision1)x = 10y = 20decision2 = x == yprint (decision2)z = 10decision3 = x == zprint (decision3)
Code 1: Using Equals to (==) Operation
- On line 1, we compare 10 with 10 which correct so, decision1 variable store True.
- Next, we store two variables x and y with respective values 10 and 20 but when we compare these variables then the decision2 variable will store False because both are not the same.
- Similarly, for comparison between x and z the decision3 will show true.
Figure 1: Result of Comparison Operators
- The operation “Not Equals” is opposite to “Equals”.
- It produces True when numbers are not the same and False when numbers are the same.
decision1 = 10 != 10print (decision1)x = 10y = 20decision2 = x != yprint (decision2)z = 10decision3 = x != zprint (decision3)
Code 2: Using Not Equals (!=) Operator
- As we know that 10 is equal to 10 so when trying to find that “are they not equal” then the answer should be no or False. Hence decision1 will store False
- Similarly, the decision2 will store True because x and y are really not equal and answer of question “are they not equal” will be yes or True.
- Now, again x and z have the same value so the Not Equality will be False.
Figure 2: Result of Code 2
- The Less Than (<) and Greater Than (>) symbol can be used similarly. The operator checks which side of the angle is close and which side is open.
- The angle < has left side is close while right side is close.
- Similarly, the angle > has the left side open and right side close.
- Operators consider that close side value is lesser and open side value is greater and make decisions either True or False.
x = 10decision1 = x < 20
Code 3: Using Less Than (or Greater Than) Operator
- We can see that x is written on the left and 20 on the right.
- The operator will consider that the value of x is less and 20 is greater.
- Which is the correct decision so the decision1 variable will store True.
- But if we change the symbol between both then the situation will also change.
x = 10decision1 = x > 20
Code 4: The Alternative Symbol
- Now, the left side of the operator is open so it considers that x is greater while 20 is less which is not correct so the variable decision1 will store False.
- Similarly, if we exchange the position of values then the decision will change.
- The operation Less Than or Equals (<=) and Greater Than or Equals (=>) works similarly by opening and closing angle of operator but they also check the equality.
- If the closing side value is less or equal then decision will be True and if not then it will produce False.
x = 10y = 20z = 10decision1 = x <= ydecision2 = x <= zdecision3 = x >= z
Code 5: Using Less Than or Equals and Greater Than or Equals
- decision1 will store True because x is less than y
- decision2 will store also True because though the variable x is not less than z but it is equal.
- decision3 will also store True because x is not greater but equals to z
- In this lecture we found out how the comparison operators use but we did not use the result of a comparison for the next statement and just print but comparison operators are very important in helping to make an intelligent program.








