Uncategorized

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.

  1. Comparison operators work on numbers.
  2. But their result is not a number. The result of these operations is true or false.
  3. The result is required to store in some bool data type variable.
  4. In many situations, it is not required to store the result.
  5. A total of six comparison operators are used in Python.
  6. == (Equals)
  7. != (Not Equals)
  8. < (Less Than)
  9. > (Greater Than)
  10. <= (Less Than or Equals)
  11. >= (Greater Than or Equals)
  12. 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

  1. On line 1, we compare 10 with 10 which correct so, decision1 variable store True.
  2. 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.
  3. Similarly, for comparison between x and z the decision3 will show true.

Figure 1: Result of Comparison Operators

  1. The operation “Not Equals” is opposite to “Equals”.
  2. 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

  1. 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
  2. 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.
  3. Now, again x and z have the same value so the Not Equality will be False.

Figure 2: Result of Code 2

  1. 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.
  2. The angle < has left side is close while right side is close.
  3. Similarly, the angle > has the left side open and right side close.
  4. 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

  1. We can see that x is written on the left and 20 on the right.
  2. The operator will consider that the value of x is less and 20 is greater.
  3. Which is the correct decision so the decision1 variable will store True.
  4. But if we change the symbol between both then the situation will also change.
x = 10decision1 = x > 20

Code 4: The Alternative Symbol

  1. 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.
  2. Similarly, if we exchange the position of values then the decision will change.
  3. 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.
  4. 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

  1. decision1 will store True because x is less than y
  2. decision2 will store also True because though the variable x is not less than z but it is equal.
  3. decision3 will also store True because x is not greater but equals to z
  4. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *