|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Comparison Operators \n", |
| 8 | + "\n", |
| 9 | + "In this lecture we will be learning about Comparison Operators in Python. These operators will allow us to compare variables and output a Boolean value (True or False). \n", |
| 10 | + "\n", |
| 11 | + "If you have any sort of background in Math, these operators should be very straight forward.\n", |
| 12 | + "\n", |
| 13 | + "First we'll present a table of the comparison operators and then work through some examples:\n", |
| 14 | + "\n", |
| 15 | + "<h2> Table of Comparison Operators </h2><p> In the table below, a=3 and b=4.</p>\n", |
| 16 | + "\n", |
| 17 | + "<table class=\"table table-bordered\">\n", |
| 18 | + "<tr>\n", |
| 19 | + "<th style=\"width:10%\">Operator</th><th style=\"width:45%\">Description</th><th>Example</th>\n", |
| 20 | + "</tr>\n", |
| 21 | + "<tr>\n", |
| 22 | + "<td>==</td>\n", |
| 23 | + "<td>If the values of two operands are equal, then the condition becomes true.</td>\n", |
| 24 | + "<td> (a == b) is not true.</td>\n", |
| 25 | + "</tr>\n", |
| 26 | + "<tr>\n", |
| 27 | + "<td>!=</td>\n", |
| 28 | + "<td>If values of two operands are not equal, then condition becomes true.</td>\n", |
| 29 | + "<td>(a != b) is true</td>\n", |
| 30 | + "</tr>\n", |
| 31 | + "<tr>\n", |
| 32 | + "<td>></td>\n", |
| 33 | + "<td>If the value of left operand is greater than the value of right operand, then condition becomes true.</td>\n", |
| 34 | + "<td> (a > b) is not true.</td>\n", |
| 35 | + "</tr>\n", |
| 36 | + "<tr>\n", |
| 37 | + "<td><</td>\n", |
| 38 | + "<td>If the value of left operand is less than the value of right operand, then condition becomes true.</td>\n", |
| 39 | + "<td> (a < b) is true.</td>\n", |
| 40 | + "</tr>\n", |
| 41 | + "<tr>\n", |
| 42 | + "<td>>=</td>\n", |
| 43 | + "<td>If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.</td>\n", |
| 44 | + "<td> (a >= b) is not true. </td>\n", |
| 45 | + "</tr>\n", |
| 46 | + "<tr>\n", |
| 47 | + "<td><=</td>\n", |
| 48 | + "<td>If the value of left operand is less than or equal to the value of right operand, then condition becomes true.</td>\n", |
| 49 | + "<td> (a <= b) is true. </td>\n", |
| 50 | + "</tr>\n", |
| 51 | + "</table>" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "Let's now work through quick examples of each of these.\n", |
| 59 | + "\n", |
| 60 | + "#### Equal" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 1, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [ |
| 68 | + { |
| 69 | + "data": { |
| 70 | + "text/plain": [ |
| 71 | + "True" |
| 72 | + ] |
| 73 | + }, |
| 74 | + "execution_count": 1, |
| 75 | + "metadata": {}, |
| 76 | + "output_type": "execute_result" |
| 77 | + } |
| 78 | + ], |
| 79 | + "source": [ |
| 80 | + "2 == 2" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": 2, |
| 86 | + "metadata": {}, |
| 87 | + "outputs": [ |
| 88 | + { |
| 89 | + "data": { |
| 90 | + "text/plain": [ |
| 91 | + "False" |
| 92 | + ] |
| 93 | + }, |
| 94 | + "execution_count": 2, |
| 95 | + "metadata": {}, |
| 96 | + "output_type": "execute_result" |
| 97 | + } |
| 98 | + ], |
| 99 | + "source": [ |
| 100 | + "1 == 0" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "Note that <code>==</code> is a <em>comparison</em> operator, while <code>=</code> is an <em>assignment</em> operator." |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "#### Not Equal" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": 3, |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [ |
| 122 | + { |
| 123 | + "data": { |
| 124 | + "text/plain": [ |
| 125 | + "True" |
| 126 | + ] |
| 127 | + }, |
| 128 | + "execution_count": 3, |
| 129 | + "metadata": {}, |
| 130 | + "output_type": "execute_result" |
| 131 | + } |
| 132 | + ], |
| 133 | + "source": [ |
| 134 | + "2 != 1" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": 4, |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [ |
| 142 | + { |
| 143 | + "data": { |
| 144 | + "text/plain": [ |
| 145 | + "False" |
| 146 | + ] |
| 147 | + }, |
| 148 | + "execution_count": 4, |
| 149 | + "metadata": {}, |
| 150 | + "output_type": "execute_result" |
| 151 | + } |
| 152 | + ], |
| 153 | + "source": [ |
| 154 | + "2 != 2" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "markdown", |
| 159 | + "metadata": {}, |
| 160 | + "source": [ |
| 161 | + "#### Greater Than" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": 5, |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "data": { |
| 171 | + "text/plain": [ |
| 172 | + "True" |
| 173 | + ] |
| 174 | + }, |
| 175 | + "execution_count": 5, |
| 176 | + "metadata": {}, |
| 177 | + "output_type": "execute_result" |
| 178 | + } |
| 179 | + ], |
| 180 | + "source": [ |
| 181 | + "2 > 1" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "code", |
| 186 | + "execution_count": 6, |
| 187 | + "metadata": {}, |
| 188 | + "outputs": [ |
| 189 | + { |
| 190 | + "data": { |
| 191 | + "text/plain": [ |
| 192 | + "False" |
| 193 | + ] |
| 194 | + }, |
| 195 | + "execution_count": 6, |
| 196 | + "metadata": {}, |
| 197 | + "output_type": "execute_result" |
| 198 | + } |
| 199 | + ], |
| 200 | + "source": [ |
| 201 | + "2 > 4" |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "markdown", |
| 206 | + "metadata": {}, |
| 207 | + "source": [ |
| 208 | + "#### Less Than" |
| 209 | + ] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "code", |
| 213 | + "execution_count": 7, |
| 214 | + "metadata": {}, |
| 215 | + "outputs": [ |
| 216 | + { |
| 217 | + "data": { |
| 218 | + "text/plain": [ |
| 219 | + "True" |
| 220 | + ] |
| 221 | + }, |
| 222 | + "execution_count": 7, |
| 223 | + "metadata": {}, |
| 224 | + "output_type": "execute_result" |
| 225 | + } |
| 226 | + ], |
| 227 | + "source": [ |
| 228 | + "2 < 4" |
| 229 | + ] |
| 230 | + }, |
| 231 | + { |
| 232 | + "cell_type": "code", |
| 233 | + "execution_count": 8, |
| 234 | + "metadata": {}, |
| 235 | + "outputs": [ |
| 236 | + { |
| 237 | + "data": { |
| 238 | + "text/plain": [ |
| 239 | + "False" |
| 240 | + ] |
| 241 | + }, |
| 242 | + "execution_count": 8, |
| 243 | + "metadata": {}, |
| 244 | + "output_type": "execute_result" |
| 245 | + } |
| 246 | + ], |
| 247 | + "source": [ |
| 248 | + "2 < 1" |
| 249 | + ] |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "markdown", |
| 253 | + "metadata": {}, |
| 254 | + "source": [ |
| 255 | + "#### Greater Than or Equal to" |
| 256 | + ] |
| 257 | + }, |
| 258 | + { |
| 259 | + "cell_type": "code", |
| 260 | + "execution_count": 9, |
| 261 | + "metadata": {}, |
| 262 | + "outputs": [ |
| 263 | + { |
| 264 | + "data": { |
| 265 | + "text/plain": [ |
| 266 | + "True" |
| 267 | + ] |
| 268 | + }, |
| 269 | + "execution_count": 9, |
| 270 | + "metadata": {}, |
| 271 | + "output_type": "execute_result" |
| 272 | + } |
| 273 | + ], |
| 274 | + "source": [ |
| 275 | + "2 >= 2" |
| 276 | + ] |
| 277 | + }, |
| 278 | + { |
| 279 | + "cell_type": "code", |
| 280 | + "execution_count": 10, |
| 281 | + "metadata": {}, |
| 282 | + "outputs": [ |
| 283 | + { |
| 284 | + "data": { |
| 285 | + "text/plain": [ |
| 286 | + "True" |
| 287 | + ] |
| 288 | + }, |
| 289 | + "execution_count": 10, |
| 290 | + "metadata": {}, |
| 291 | + "output_type": "execute_result" |
| 292 | + } |
| 293 | + ], |
| 294 | + "source": [ |
| 295 | + "2 >= 1" |
| 296 | + ] |
| 297 | + }, |
| 298 | + { |
| 299 | + "cell_type": "markdown", |
| 300 | + "metadata": {}, |
| 301 | + "source": [ |
| 302 | + "#### Less than or Equal to" |
| 303 | + ] |
| 304 | + }, |
| 305 | + { |
| 306 | + "cell_type": "code", |
| 307 | + "execution_count": 11, |
| 308 | + "metadata": {}, |
| 309 | + "outputs": [ |
| 310 | + { |
| 311 | + "data": { |
| 312 | + "text/plain": [ |
| 313 | + "True" |
| 314 | + ] |
| 315 | + }, |
| 316 | + "execution_count": 11, |
| 317 | + "metadata": {}, |
| 318 | + "output_type": "execute_result" |
| 319 | + } |
| 320 | + ], |
| 321 | + "source": [ |
| 322 | + "2 <= 2" |
| 323 | + ] |
| 324 | + }, |
| 325 | + { |
| 326 | + "cell_type": "code", |
| 327 | + "execution_count": 12, |
| 328 | + "metadata": {}, |
| 329 | + "outputs": [ |
| 330 | + { |
| 331 | + "data": { |
| 332 | + "text/plain": [ |
| 333 | + "True" |
| 334 | + ] |
| 335 | + }, |
| 336 | + "execution_count": 12, |
| 337 | + "metadata": {}, |
| 338 | + "output_type": "execute_result" |
| 339 | + } |
| 340 | + ], |
| 341 | + "source": [ |
| 342 | + "2 <= 4" |
| 343 | + ] |
| 344 | + }, |
| 345 | + { |
| 346 | + "cell_type": "markdown", |
| 347 | + "metadata": {}, |
| 348 | + "source": [ |
| 349 | + "**Great! Go over each comparison operator to make sure you understand what each one is saying. But hopefully this was straightforward for you.**\n", |
| 350 | + "\n", |
| 351 | + "Next we will cover chained comparison operators" |
| 352 | + ] |
| 353 | + } |
| 354 | + ], |
| 355 | + "metadata": { |
| 356 | + "kernelspec": { |
| 357 | + "display_name": "Python 3", |
| 358 | + "language": "python", |
| 359 | + "name": "python3" |
| 360 | + }, |
| 361 | + "language_info": { |
| 362 | + "codemirror_mode": { |
| 363 | + "name": "ipython", |
| 364 | + "version": 3 |
| 365 | + }, |
| 366 | + "file_extension": ".py", |
| 367 | + "mimetype": "text/x-python", |
| 368 | + "name": "python", |
| 369 | + "nbconvert_exporter": "python", |
| 370 | + "pygments_lexer": "ipython3", |
| 371 | + "version": "3.6.2" |
| 372 | + } |
| 373 | + }, |
| 374 | + "nbformat": 4, |
| 375 | + "nbformat_minor": 1 |
| 376 | +} |
0 commit comments