|
| 1 | +--- |
| 2 | +description: Comparison and Arithmetic Operations in Python. |
| 3 | +--- |
| 4 | + |
| 5 | +# Comparison and Arithmetic Operations |
| 6 | + |
| 7 | +A quick revision on what all concepts we have learned so far: |
| 8 | + |
| 9 | +- Brief on Python history |
| 10 | +- Variables |
| 11 | +- Data types (strings, integers, floats, booleans) |
| 12 | +- String concatenation & interpolation |
| 13 | + |
| 14 | +In this chapter we will be looking at what comparison and arithmetic operations are. |
| 15 | + |
| 16 | +## Comparison Operators |
| 17 | + |
| 18 | +Comparison Operators (also known as Relational Operators), are used to compare values. They're very |
| 19 | +similar to how they're in mathematics. |
| 20 | + |
| 21 | +There are six types of comparison operators as follows: |
| 22 | + |
| 23 | +| Operator Symbol | Name | |
| 24 | +| ---------------- | ----------------------------- | |
| 25 | +| `>` | Greater Than | |
| 26 | +| `<` | Lesser Than | |
| 27 | +| `==` | Equal To | |
| 28 | +| `!=` | Not Equal To | |
| 29 | +| `>=` | Greater Than Or Equal To | |
| 30 | +| `<=` | Lesser Than Or Equal To | |
| 31 | + |
| 32 | +Comparison operators always return a boolean values (`True` or `False`) when used. |
| 33 | + |
| 34 | +<figure markdown="span"> |
| 35 | + |
| 36 | +{ width="640" } |
| 37 | + |
| 38 | +<figcaption> |
| 39 | + The either side of the operator is what we call an operand. |
| 40 | +</figcaption> |
| 41 | + |
| 42 | +</figure> |
| 43 | + |
| 44 | +### Greater Than Operator (`>`) |
| 45 | + |
| 46 | +Checks if the left hand side operand is larger than the right hand side operand. |
| 47 | + |
| 48 | +```py |
| 49 | +print(1 > 2) |
| 50 | +print(20 > 1) |
| 51 | +print(3.2 > 9) |
| 52 | +``` |
| 53 | + |
| 54 | +Running this will give us the following output: ```text False True False ``` |
| 55 | + |
| 56 | +This reads almost like plain English! 1 is not greater than 2, hence it returns and prints out |
| 57 | +`False`. 20 is larger than 1, which results the output being `True`, and so on. |
| 58 | + |
| 59 | +### Lesser Than Operator (`<`) |
| 60 | + |
| 61 | +Checks if the right hand side operand is larger than the left hand side operand. |
| 62 | + |
| 63 | +```py |
| 64 | +print(3 < 9) |
| 65 | +print(200 < 100) |
| 66 | +print(23.12 < 9) |
| 67 | +``` |
| 68 | + |
| 69 | +Output: |
| 70 | + |
| 71 | +```text |
| 72 | +True |
| 73 | +False |
| 74 | +False |
| 75 | +``` |
| 76 | + |
| 77 | +### Equal To Operator (`==`) |
| 78 | + |
| 79 | +Checks if both left and right hand side operands are exactly equal. |
| 80 | + |
| 81 | +```py |
| 82 | +print(123 == 321) |
| 83 | +print(909 == 909) |
| 84 | +print(812 == 22) |
| 85 | +``` |
| 86 | + |
| 87 | +Output: |
| 88 | + |
| 89 | +```text |
| 90 | +False |
| 91 | +True |
| 92 | +False |
| 93 | +``` |
| 94 | + |
| 95 | +!!! warning |
| 96 | + |
| 97 | + Do not confuse between the Equivalence Operator (`==`) and the Assignment Operator (`=`). The |
| 98 | + Equivalence Operator is used to check if both the operands are equal, whereas the Assignment |
| 99 | + Operator is used to assign data to a variable. |
| 100 | + |
| 101 | +### Not Equal To Operator (`!=`) |
| 102 | + |
| 103 | +Checks if both the operands are _not_ equal. |
| 104 | + |
| 105 | +```py |
| 106 | + |
| 107 | +print(10 != 10) |
| 108 | +print(3 != 4) |
| 109 | +print(3 != 8) |
| 110 | +``` |
| 111 | + |
| 112 | +Output: |
| 113 | + |
| 114 | +```text |
| 115 | +False |
| 116 | +True |
| 117 | +True |
| 118 | +``` |
| 119 | + |
| 120 | +### Greater Than Or Equal To Operator (`>=`) |
| 121 | + |
| 122 | +Checks if the left hand side operand is either greater than or equal to the right hand side operand. |
| 123 | + |
| 124 | +```py |
| 125 | +print(90 >= 18) |
| 126 | +print(23 >= 32) |
| 127 | +print(1 >= 0) |
| 128 | +``` |
| 129 | + |
| 130 | +Output: |
| 131 | + |
| 132 | +```text |
| 133 | +True |
| 134 | +True |
| 135 | +False |
| 136 | +``` |
| 137 | + |
| 138 | +Here, 90 is greater than 18 which gives us `True` as the output. 23 is not greater but it is equal |
| 139 | +to 23 which also gives us `True` as the output. |
| 140 | + |
| 141 | +### Lesser Than Or Equal To Operator (`<=`) |
| 142 | + |
| 143 | +Checks if the right hand side operand is either greater than or equal to the left hand side operand. |
| 144 | + |
| 145 | +```py |
| 146 | +print(4 <= 18) |
| 147 | +print(9 <= 3) |
| 148 | +print(10 <= 10) |
| 149 | +``` |
| 150 | + |
| 151 | +Output: |
| 152 | + |
| 153 | +```text |
| 154 | +True |
| 155 | +False |
| 156 | +True |
| 157 | +``` |
0 commit comments