├── .gitattributes ├── .ipynb_checkpoints ├── Untitled-checkpoint.ipynb └── notes-checkpoint.ipynb ├── Al Sweigart - Automate the Boring Stuff with Python_ Practical Programming for Total Beginners-No Starch Press (2015).pdf ├── Automate_the_Boring_Stuff_onlinematerials.zip ├── README.md ├── ch1 ├── .ipynb_checkpoints │ └── ch1-checkpoint.ipynb └── ch1.ipynb ├── ch2 └── fig2-1.jpg └── notes.ipynb /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.7.1" 28 | }, 29 | "toc": { 30 | "base_numbering": 1, 31 | "nav_menu": {}, 32 | "number_sections": true, 33 | "sideBar": true, 34 | "skip_h1_title": false, 35 | "title_cell": "Table of Contents", 36 | "title_sidebar": "Contents", 37 | "toc_cell": false, 38 | "toc_position": {}, 39 | "toc_section_display": true, 40 | "toc_window_display": false 41 | }, 42 | "varInspector": { 43 | "cols": { 44 | "lenName": 16, 45 | "lenType": 16, 46 | "lenVar": 40 47 | }, 48 | "kernels_config": { 49 | "python": { 50 | "delete_cmd_postfix": "", 51 | "delete_cmd_prefix": "del ", 52 | "library": "var_list.py", 53 | "varRefreshCmd": "print(var_dic_list())" 54 | }, 55 | "r": { 56 | "delete_cmd_postfix": ") ", 57 | "delete_cmd_prefix": "rm(", 58 | "library": "var_list.r", 59 | "varRefreshCmd": "cat(var_dic_list()) " 60 | } 61 | }, 62 | "types_to_exclude": [ 63 | "module", 64 | "function", 65 | "builtin_function_or_method", 66 | "instance", 67 | "_Feature" 68 | ], 69 | "window_display": false 70 | } 71 | }, 72 | "nbformat": 4, 73 | "nbformat_minor": 2 74 | } 75 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/notes-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The preface " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "When I doing [google hashcode 2018](https://codingcompetitions.withgoogle.com/hashcode/archive), I found that I could not finished the task in 3 hours. It's so hard for me.\n", 15 | "\n", 16 | "The first thing that drags me is that I do not know how to realize a functionality in a decent way. I just spend 5 hours to write a helper function into the loop. The second thing is that I do not really know whether I should use numpy-array,list or dict to store my objects. The third thing is that I do not good at debug my program. Although I write my ideas on a blank paper and then coding it. There are still several bugs in the first draft code. I spend a little time to debug by code.\n", 17 | "\n", 18 | "After I finished my code, I go the rank board of hashcode 2018. I found that I did not give a good solution. I only care about coding and did not really focus on math. The NP-hard problem need a lot of mathmatic knowledge to give a estimated solution. I did not good at optimsing and algorithms. I watch a winners' video on Youtube. He is a Russian and he spend all his time to mathmatic part of the question and do not feel any impeded by coding part. Which makes me feel that I should code more and let code by my natural. If I am really good at coding, I think I can focus more on math when solving the problem.\n", 19 | "\n", 20 | "To achieve a better understanding of python standard library, I decided to read the book: **Automate The Broing Stuff with Python**. I think I can know what functionality can be easily realized by standard library and what is not." 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": { 26 | "ExecuteTime": { 27 | "end_time": "2020-01-27T03:05:56.136259Z", 28 | "start_time": "2020-01-27T03:05:56.133295Z" 29 | } 30 | }, 31 | "source": [ 32 | "# Part 1: Python Programming Basics \n", 33 | "## Python Basics" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": { 39 | "ExecuteTime": { 40 | "end_time": "2020-01-27T03:11:45.849032Z", 41 | "start_time": "2020-01-27T03:11:45.846038Z" 42 | } 43 | }, 44 | "source": [ 45 | "### Entreing Expressions into the Interactive Shell\n", 46 | "\n", 47 | "| Operator | Operation | Example | Evaluates to |\n", 48 | "|:--------:|:---------------------------------:|:-------:|:------------:|\n", 49 | "| ** | Exponent | 2 ** 3 | 8 |\n", 50 | "| % | Modeulus/remainder | 22 % 8 | 6 |\n", 51 | "| // | Integer division/floored quotient | 22//8 | 2 |\n", 52 | "| / | Division | 22/8 | 2.75 |\n", 53 | "| * | Multiplication | 3*5 | 15 |\n", 54 | "| - | Substraction | 5-2 | 3 |\n", 55 | "| + | Addition | 2+2 | 4 |" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": { 61 | "ExecuteTime": { 62 | "end_time": "2020-01-27T03:26:17.592346Z", 63 | "start_time": "2020-01-27T03:26:17.480645Z" 64 | } 65 | }, 66 | "source": [ 67 | "### The Integer,Floating-Point, and String Data Types" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "### String Concatenation and Replication" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "metadata": { 81 | "ExecuteTime": { 82 | "end_time": "2020-01-27T03:27:47.879330Z", 83 | "start_time": "2020-01-27T03:27:47.872346Z" 84 | } 85 | }, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "'AliceBob'" 91 | ] 92 | }, 93 | "execution_count": 4, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "'Alice'+'Bob'" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 5, 105 | "metadata": { 106 | "ExecuteTime": { 107 | "end_time": "2020-01-27T03:27:57.823253Z", 108 | "start_time": "2020-01-27T03:27:57.818245Z" 109 | } 110 | }, 111 | "outputs": [ 112 | { 113 | "ename": "SyntaxError", 114 | "evalue": "can't assign to literal (, line 1)", 115 | "output_type": "error", 116 | "traceback": [ 117 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m 'Alice'=42\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m can't assign to literal\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "'Alice'=42" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 6, 128 | "metadata": { 129 | "ExecuteTime": { 130 | "end_time": "2020-01-27T03:28:44.327387Z", 131 | "start_time": "2020-01-27T03:28:44.324367Z" 132 | } 133 | }, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "'AliceAliceAliceAliceAlice'" 139 | ] 140 | }, 141 | "execution_count": 6, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "'Alice'*5" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 7, 153 | "metadata": { 154 | "ExecuteTime": { 155 | "end_time": "2020-01-27T03:28:53.077738Z", 156 | "start_time": "2020-01-27T03:28:53.074745Z" 157 | } 158 | }, 159 | "outputs": [ 160 | { 161 | "ename": "SyntaxError", 162 | "evalue": "invalid syntax (, line 1)", 163 | "output_type": "error", 164 | "traceback": [ 165 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m 'Alice'Bob'\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "'Alice'Bob'" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 9, 176 | "metadata": { 177 | "ExecuteTime": { 178 | "end_time": "2020-01-27T03:29:03.311468Z", 179 | "start_time": "2020-01-27T03:29:03.306441Z" 180 | } 181 | }, 182 | "outputs": [ 183 | { 184 | "ename": "TypeError", 185 | "evalue": "can't multiply sequence by non-int of type 'float'", 186 | "output_type": "error", 187 | "traceback": [ 188 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 189 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 190 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;34m'Alice'\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;36m5.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 191 | "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'float'" 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "'Alice' * 5.0" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "### Storing Values in Variables\n", 204 | "\n", 205 | "A _variable_ is like a box in the computer's memory where you can store a single value. If you wan to use the result of an evaluated expression later in your program, you can save it inside a variable." 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": { 211 | "ExecuteTime": { 212 | "end_time": "2020-01-27T03:52:09.629559Z", 213 | "start_time": "2020-01-27T03:52:09.626540Z" 214 | } 215 | }, 216 | "source": [ 217 | "__Assignment statements__" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 10, 223 | "metadata": { 224 | "ExecuteTime": { 225 | "end_time": "2020-01-27T03:50:09.600079Z", 226 | "start_time": "2020-01-27T03:50:09.597083Z" 227 | } 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "spam = 42\n", 232 | "eggs = 2" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 12, 238 | "metadata": { 239 | "ExecuteTime": { 240 | "end_time": "2020-01-27T03:50:28.935088Z", 241 | "start_time": "2020-01-27T03:50:28.930100Z" 242 | } 243 | }, 244 | "outputs": [ 245 | { 246 | "data": { 247 | "text/plain": [ 248 | "(44, 86)" 249 | ] 250 | }, 251 | "execution_count": 12, 252 | "metadata": {}, 253 | "output_type": "execute_result" 254 | } 255 | ], 256 | "source": [ 257 | "spam + eggs,spam + eggs + spam" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 13, 263 | "metadata": { 264 | "ExecuteTime": { 265 | "end_time": "2020-01-27T03:50:40.909261Z", 266 | "start_time": "2020-01-27T03:50:40.906246Z" 267 | } 268 | }, 269 | "outputs": [ 270 | { 271 | "data": { 272 | "text/plain": [ 273 | "44" 274 | ] 275 | }, 276 | "execution_count": 13, 277 | "metadata": {}, 278 | "output_type": "execute_result" 279 | } 280 | ], 281 | "source": [ 282 | "spam = spam + 2\n", 283 | "spam" 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "metadata": {}, 289 | "source": [ 290 | "Overwriting a variable" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 20, 296 | "metadata": { 297 | "ExecuteTime": { 298 | "end_time": "2020-01-27T03:51:28.925024Z", 299 | "start_time": "2020-01-27T03:51:28.923004Z" 300 | } 301 | }, 302 | "outputs": [ 303 | { 304 | "data": { 305 | "text/plain": [ 306 | "'Hello'" 307 | ] 308 | }, 309 | "execution_count": 20, 310 | "metadata": {}, 311 | "output_type": "execute_result" 312 | } 313 | ], 314 | "source": [ 315 | "spam = 'Hello'\n", 316 | "spam" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 26, 322 | "metadata": { 323 | "ExecuteTime": { 324 | "end_time": "2020-01-27T04:15:16.203312Z", 325 | "start_time": "2020-01-27T04:15:16.200328Z" 326 | } 327 | }, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "'Goodbye'" 333 | ] 334 | }, 335 | "execution_count": 26, 336 | "metadata": {}, 337 | "output_type": "execute_result" 338 | } 339 | ], 340 | "source": [ 341 | "spam = 'Goodbye'\n", 342 | "spam" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "### Variable names\n", 350 | "\n", 351 | "1. It can be only one word\n", 352 | "2. It can use only letters, numbers and the underscore(_) character\n", 353 | "3. It can't begin with a number" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "| Valid variable names | Invalid variable names |\n", 361 | "|----------------------|--------------------------------------------------------|\n", 362 | "| balance | current-balance (hyphens are not allowed) |\n", 363 | "| currentBalance | current balance (spaces are not allowed) |\n", 364 | "| current_balance | 4account (can't begin with a number) |\n", 365 | "| _spam | 42 (can't begin with a number) |\n", 366 | "| SPAM | total_\\\\$um (special characters like \\\\$ are not allowed) |\n", 367 | "| account4 | 'hello' (special characters like ' are not allowed) |" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "metadata": { 373 | "ExecuteTime": { 374 | "end_time": "2020-01-27T04:01:37.277242Z", 375 | "start_time": "2020-01-27T04:01:37.274222Z" 376 | } 377 | }, 378 | "source": [ 379 | "Variable names are case-sensitive, meaning that spam,SPAM,Span and sPam are four differnet variables. It is a Python convention to start your variables with a lowercase letter.\n", 380 | "\n", 381 | "This book uses camelcase for variable names instead of underscores; that is , variables `lookLikeThis` instead of `looking_like_this`. Some experienced programmers may point out that the official Python code style.,PEP 8, says that underscores should be used.\n", 382 | "\n", 383 | "A good variable name describes the data it contains. It will make your code more readable." 384 | ] 385 | }, 386 | { 387 | "cell_type": "markdown", 388 | "metadata": {}, 389 | "source": [ 390 | "### Your First Program" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 46, 396 | "metadata": { 397 | "ExecuteTime": { 398 | "end_time": "2020-01-27T06:03:25.289150Z", 399 | "start_time": "2020-01-27T06:03:21.336540Z" 400 | } 401 | }, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "hello world!\n", 408 | "What is your name?\n", 409 | "Travis\n", 410 | "It is good to meet you, Travis\n", 411 | "6\n", 412 | "What is your age?\n", 413 | "12\n", 414 | "You will be 13 in a year,\n" 415 | ] 416 | } 417 | ], 418 | "source": [ 419 | "%run ./ch1/ch1.ipynb" 420 | ] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": { 425 | "ExecuteTime": { 426 | "end_time": "2020-01-27T05:37:24.148628Z", 427 | "start_time": "2020-01-27T05:37:24.144619Z" 428 | } 429 | }, 430 | "source": [ 431 | "Python ignores comments, and you can use them to write notes or remind your self what hte code is tring to do.Any text for the rest of the line following a hash mark(#) is a part of a comment. \n", 432 | "\n", 433 | "Sometimes, programmers will put a # in front of a line of code to temporarily remove it while testiing a pogram. This is called __commenting out code__, and it can be useful when you're trying to figure out why a program doesnt't work. YOu can remove the # when you are ready to put the line back in.\n", 434 | "\n", 435 | "Python can not __concatenate__ different types of objects. For instance, Python do not know how to evaluate `5 + '1'`. However, we can use `int`,`str` and `float` functions to change the type of variables. Then, we can operate on variables with the same type." 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 47, 441 | "metadata": { 442 | "ExecuteTime": { 443 | "end_time": "2020-01-27T06:20:20.229377Z", 444 | "start_time": "2020-01-27T06:20:20.223386Z" 445 | } 446 | }, 447 | "outputs": [ 448 | { 449 | "ename": "TypeError", 450 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 451 | "output_type": "error", 452 | "traceback": [ 453 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 454 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 455 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m5\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m'1'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 456 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 457 | ] 458 | } 459 | ], 460 | "source": [ 461 | "5 + '1'" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": 48, 467 | "metadata": { 468 | "ExecuteTime": { 469 | "end_time": "2020-01-27T06:21:24.698310Z", 470 | "start_time": "2020-01-27T06:21:24.693324Z" 471 | } 472 | }, 473 | "outputs": [ 474 | { 475 | "data": { 476 | "text/plain": [ 477 | "6" 478 | ] 479 | }, 480 | "execution_count": 48, 481 | "metadata": {}, 482 | "output_type": "execute_result" 483 | } 484 | ], 485 | "source": [ 486 | "5 + int('1')" 487 | ] 488 | }, 489 | { 490 | "cell_type": "markdown", 491 | "metadata": {}, 492 | "source": [ 493 | "We can use int to round a floating-point number down" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 50, 499 | "metadata": { 500 | "ExecuteTime": { 501 | "end_time": "2020-01-27T06:38:03.449576Z", 502 | "start_time": "2020-01-27T06:38:03.445618Z" 503 | } 504 | }, 505 | "outputs": [ 506 | { 507 | "data": { 508 | "text/plain": [ 509 | "7" 510 | ] 511 | }, 512 | "execution_count": 50, 513 | "metadata": {}, 514 | "output_type": "execute_result" 515 | } 516 | ], 517 | "source": [ 518 | "int(7.7)" 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": {}, 524 | "source": [ 525 | "### Practice Questions\n", 526 | "\n", 527 | "- 4. What is an expression made up of? What do all expressions do? What is the similarity and difference between `expression` and `statement`?\n", 528 | "\n", 529 | "> An expresson can only contains identifiers, literals and operators. However, a statement.All expressions need to be evaluated and to reduce to a value, which can be any type of objects in Python.\n", 530 | "A statement is an instruction that the Python interpreter can execute. We have only seen the assignment statement so far. Some other kinds of statements that we’ll see shortly are `while` statements, `for` statements, `if` statements, and `import` statements. (There are other kinds too!)\n", 531 | "\n", 532 | "> An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated. If you ask Python to `print` an expression, the interpreter evaluates the expression and displays the result.\n", 533 | "\n", 534 | "> Expression is a part of statements.\n", 535 | "\n", 536 | "> Source: \n", 537 | "1. [What is the difference between an expression and a statement in Python?](https://stackoverflow.com/questions/4728073/what-is-the-difference-between-an-expression-and-a-statement-in-python) See the top2 answer and the last ansmer.\n", 538 | "2. [official guide for expressions](https://docs.python.org/zh-cn/3/reference/expressions.html).\n", 539 | "3. [official guide for statements](https://docs.python.org/3/reference/simple_stmts.html)\n", 540 | "4. [thinkcspy](https://runestone.academy/runestone/books/published/thinkcspy/SimplePythonData/StatementsandExpressions.html)\n", 541 | "\n", 542 | "\n", 543 | "- 9. What three functions can be used to get the integer, floating-point number, or string version of a value?\n", 544 | "\n", 545 | "> `int`,`float` and `str`.\n", 546 | "\n", 547 | "- Extra credit: Search online for the Pyton documentation for the `len()` function. It will be on a web page titled \"Built-in Functions.\"Skim the list of ohter functions Python has, look up what the `round` function does, and experiment with it in the interactive shell.\n", 548 | "\n", 549 | "> For the built-in types supporting `round()`, values are rounded to the closest multiple of 10 to the power minus __ndigits__; if two multiples are equally close, rounding is done toward the even choice (so, for example, both `round(0.5)` and round`(-0.5)` are `0`, and `round(1.5)` is `2`). Any integer value is valid for __ndigits__ (positive, zero, or negative). The return value is an integer if ndigits is omitted or `None`. Otherwise the return value has the same type as number." 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 99, 555 | "metadata": { 556 | "ExecuteTime": { 557 | "end_time": "2020-01-27T07:17:05.469053Z", 558 | "start_time": "2020-01-27T07:17:05.465069Z" 559 | } 560 | }, 561 | "outputs": [ 562 | { 563 | "data": { 564 | "text/plain": [ 565 | "10.01" 566 | ] 567 | }, 568 | "execution_count": 99, 569 | "metadata": {}, 570 | "output_type": "execute_result" 571 | } 572 | ], 573 | "source": [ 574 | "round(10.01457,2)" 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": 103, 580 | "metadata": { 581 | "ExecuteTime": { 582 | "end_time": "2020-01-27T07:18:13.103324Z", 583 | "start_time": "2020-01-27T07:18:13.100321Z" 584 | } 585 | }, 586 | "outputs": [ 587 | { 588 | "data": { 589 | "text/plain": [ 590 | "10.02" 591 | ] 592 | }, 593 | "execution_count": 103, 594 | "metadata": {}, 595 | "output_type": "execute_result" 596 | } 597 | ], 598 | "source": [ 599 | "# because 10.02 is even and 10.01 is old, round to the even choice when 2 multiples are equally close\n", 600 | "round(10.015,2)" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 104, 606 | "metadata": { 607 | "ExecuteTime": { 608 | "end_time": "2020-01-27T07:18:14.787101Z", 609 | "start_time": "2020-01-27T07:18:14.783140Z" 610 | } 611 | }, 612 | "outputs": [ 613 | { 614 | "data": { 615 | "text/plain": [ 616 | "140" 617 | ] 618 | }, 619 | "execution_count": 104, 620 | "metadata": {}, 621 | "output_type": "execute_result" 622 | } 623 | ], 624 | "source": [ 625 | "round(145,ndigits= -1)" 626 | ] 627 | }, 628 | { 629 | "cell_type": "markdown", 630 | "metadata": {}, 631 | "source": [ 632 | "## Flow Control" 633 | ] 634 | }, 635 | { 636 | "cell_type": "markdown", 637 | "metadata": { 638 | "ExecuteTime": { 639 | "end_time": "2020-01-27T07:40:55.727089Z", 640 | "start_time": "2020-01-27T07:40:55.723101Z" 641 | } 642 | }, 643 | "source": [ 644 | "" 645 | ] 646 | }, 647 | { 648 | "cell_type": "markdown", 649 | "metadata": {}, 650 | "source": [ 651 | "In a flow chart like above ,there is usually more than one way to go from the start to the end. THe same is true for lines of code in a computer program. Flowcharts represent these branching points with diamonds, while the other steps are represented with rectangles.The starting and ending steps are represented with rounded rectangles." 652 | ] 653 | }, 654 | { 655 | "cell_type": "markdown", 656 | "metadata": {}, 657 | "source": [ 658 | "### __Boolean Values__\n", 659 | "\n", 660 | "`True` and `False`\n", 661 | "\n", 662 | "### Comparison Operators\n", 663 | "\n", 664 | "| Operator | Meaning |\n", 665 | "|:--------:|:------------------------:|\n", 666 | "| == | Equal to |\n", 667 | "| != | Not equal to |\n", 668 | "| < | Less than |\n", 669 | "| > | Greater than |\n", 670 | "| <= | Less than or equal to |\n", 671 | "| >= | Greater than or equal to |" 672 | ] 673 | }, 674 | { 675 | "cell_type": "markdown", 676 | "metadata": {}, 677 | "source": [ 678 | "### Bollean Operators\n", 679 | "\n", 680 | "The three Boolean operators(and, or and not) are used to compare Boolean values. Like comparasion operators, they evaluate these expressions down to a Boolean value." 681 | ] 682 | }, 683 | { 684 | "cell_type": "markdown", 685 | "metadata": { 686 | "ExecuteTime": { 687 | "end_time": "2020-01-27T08:17:53.447795Z", 688 | "start_time": "2020-01-27T08:17:53.440814Z" 689 | } 690 | }, 691 | "source": [ 692 | "__Binary Boolean Operators__\n", 693 | "\n", 694 | "`and`,`or`\n", 695 | "\n", 696 | "__The not Operator__\n", 697 | "\n", 698 | "`not`" 699 | ] 700 | }, 701 | { 702 | "cell_type": "markdown", 703 | "metadata": {}, 704 | "source": [ 705 | "`not` can work on other expressions, to achieve some strange functionality. Here is a example to check whether a list is empty:" 706 | ] 707 | }, 708 | { 709 | "cell_type": "code", 710 | "execution_count": 134, 711 | "metadata": { 712 | "ExecuteTime": { 713 | "end_time": "2020-01-27T08:48:51.346492Z", 714 | "start_time": "2020-01-27T08:48:51.343525Z" 715 | } 716 | }, 717 | "outputs": [ 718 | { 719 | "name": "stdout", 720 | "output_type": "stream", 721 | "text": [ 722 | "1\n" 723 | ] 724 | } 725 | ], 726 | "source": [ 727 | "a = []\n", 728 | "if not a:\n", 729 | " print(1)" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 136, 735 | "metadata": { 736 | "ExecuteTime": { 737 | "end_time": "2020-01-27T08:49:33.481505Z", 738 | "start_time": "2020-01-27T08:49:33.478509Z" 739 | } 740 | }, 741 | "outputs": [ 742 | { 743 | "name": "stdout", 744 | "output_type": "stream", 745 | "text": [ 746 | "1\n" 747 | ] 748 | } 749 | ], 750 | "source": [ 751 | "# equals to write it explicitly\n", 752 | "if len(a) == 0:\n", 753 | " print(1)" 754 | ] 755 | }, 756 | { 757 | "cell_type": "markdown", 758 | "metadata": {}, 759 | "source": [ 760 | "### Mixing Bolean and comparison Opearators\n", 761 | "\n", 762 | "We should notice the order of operators, there is a [operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence). Something about [bitwise operators](https://www.geeksforgeeks.org/python-bitwise-operators/)" 763 | ] 764 | }, 765 | { 766 | "cell_type": "code", 767 | "execution_count": 141, 768 | "metadata": { 769 | "ExecuteTime": { 770 | "end_time": "2020-01-27T09:00:18.029063Z", 771 | "start_time": "2020-01-27T09:00:18.025073Z" 772 | } 773 | }, 774 | "outputs": [ 775 | { 776 | "data": { 777 | "text/plain": [ 778 | "True" 779 | ] 780 | }, 781 | "execution_count": 141, 782 | "metadata": {}, 783 | "output_type": "execute_result" 784 | } 785 | ], 786 | "source": [ 787 | "2+2==4 and not 2+2 ==5 and 2*2 ==2+2" 788 | ] 789 | }, 790 | { 791 | "cell_type": "markdown", 792 | "metadata": {}, 793 | "source": [ 794 | "### Elements of Flow Control" 795 | ] 796 | }, 797 | { 798 | "cell_type": "markdown", 799 | "metadata": {}, 800 | "source": [ 801 | "1. if Statements\n", 802 | "2. else Statements\n", 803 | "3. elif Statements\n", 804 | "4. while Loop Statements\n", 805 | "5. break Statements\n", 806 | "6. continue Statements\n", 807 | "7. for loops and the range()" 808 | ] 809 | }, 810 | { 811 | "cell_type": "code", 812 | "execution_count": 143, 813 | "metadata": { 814 | "ExecuteTime": { 815 | "end_time": "2020-01-27T11:14:11.648521Z", 816 | "start_time": "2020-01-27T11:13:34.633634Z" 817 | }, 818 | "collapsed": true 819 | }, 820 | "outputs": [ 821 | { 822 | "name": "stdout", 823 | "output_type": "stream", 824 | "text": [ 825 | "Who are you?\n", 826 | "Joe\n", 827 | "Hello, Joe. What is the password?(It is a fish.)\n", 828 | "a\n", 829 | "Who are you?\n", 830 | "a\n", 831 | "Who are you?\n", 832 | "a\n", 833 | "Who are you?\n", 834 | "a\n", 835 | "Who are you?\n", 836 | "Joe\n", 837 | "Hello, Joe. What is the password?(It is a fish.)\n", 838 | "swordfish\n", 839 | "Access granted\n" 840 | ] 841 | } 842 | ], 843 | "source": [ 844 | "while True:\n", 845 | " print('Who are you?')\n", 846 | " name = input()\n", 847 | " if name != 'Joe':\n", 848 | " continue \n", 849 | " print('Hello, Joe. What is the password?(It is a fish.)')\n", 850 | " password = input()\n", 851 | " if password == 'swordfish':\n", 852 | " break\n", 853 | "print('Access granted')" 854 | ] 855 | }, 856 | { 857 | "cell_type": "markdown", 858 | "metadata": {}, 859 | "source": [ 860 | "### truthy and falsey values\n", 861 | "\n", 862 | "There are some values in other data types(not Boolean type) that conditions will consider euivalent to `True` and `False`. When used in conditions, `0`, `0.0` and `''`(empty string) are considered `False`, while all other values are considered `True`. \n", 863 | "\n", 864 | "\n", 865 | "Using truthy and false values can make program more readable and time-saving.\n", 866 | "\n", 867 | "For example, looke at the following programe:" 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "execution_count": 146, 873 | "metadata": { 874 | "ExecuteTime": { 875 | "end_time": "2020-01-27T11:22:22.401479Z", 876 | "start_time": "2020-01-27T11:22:01.703188Z" 877 | } 878 | }, 879 | "outputs": [ 880 | { 881 | "name": "stdout", 882 | "output_type": "stream", 883 | "text": [ 884 | "Enter your name:\n", 885 | "1\n", 886 | "How many guests will you have?\n", 887 | "12\n", 888 | "Be sure to have enough room for all your guests.\n", 889 | "Done\n" 890 | ] 891 | } 892 | ], 893 | "source": [ 894 | "name = '' \n", 895 | "while not name:\n", 896 | " print('Enter your name:')\n", 897 | " name = input()\n", 898 | "print('How many guests will you have?')\n", 899 | "numOfGuests = int(input())\n", 900 | "if numOfGuests:\n", 901 | " print('Be sure to have enough room for all your guests.')\n", 902 | "print('Done')" 903 | ] 904 | }, 905 | { 906 | "cell_type": "markdown", 907 | "metadata": {}, 908 | "source": [ 909 | "### Equivalent of for and while loop\n", 910 | "\n", 911 | "We can write equivalent for loop and while loop." 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 149, 917 | "metadata": { 918 | "ExecuteTime": { 919 | "end_time": "2020-01-27T11:26:50.865544Z", 920 | "start_time": "2020-01-27T11:26:50.862519Z" 921 | } 922 | }, 923 | "outputs": [ 924 | { 925 | "name": "stdout", 926 | "output_type": "stream", 927 | "text": [ 928 | "0\n", 929 | "1\n", 930 | "2\n", 931 | "3\n", 932 | "4\n" 933 | ] 934 | } 935 | ], 936 | "source": [ 937 | "for i in range(5):\n", 938 | " print(i)" 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 150, 944 | "metadata": { 945 | "ExecuteTime": { 946 | "end_time": "2020-01-27T11:26:54.572258Z", 947 | "start_time": "2020-01-27T11:26:54.568269Z" 948 | } 949 | }, 950 | "outputs": [ 951 | { 952 | "name": "stdout", 953 | "output_type": "stream", 954 | "text": [ 955 | "0\n", 956 | "1\n", 957 | "2\n", 958 | "3\n", 959 | "4\n", 960 | "5\n" 961 | ] 962 | } 963 | ], 964 | "source": [ 965 | "i = 0\n", 966 | "while i <= 5:\n", 967 | " print(i)\n", 968 | " i += 1" 969 | ] 970 | }, 971 | { 972 | "cell_type": "markdown", 973 | "metadata": {}, 974 | "source": [ 975 | "### Import Modules\n", 976 | "\n", 977 | "All Python programs can call a basic set of functions called _built-in functions_, cincluding the print(), input(), and len() functions you've seen before. Python also comes with a set of modules called the _standard library_. Each module is a Python program that contains a related group of functions that can be embedded in your programs. For example, the math module has mathematics-related functions, the random model has random number-related functions, and so on.\n", 978 | "\n", 979 | "Befor you can use the functions in a module, you must import the module with an `import` statement. In code, an `import` statement consists of the following:\n", 980 | "- The `import` keyword\n", 981 | "- The name of the module\n", 982 | "- Optionally, more module namse as long as they are separated by commas" 983 | ] 984 | }, 985 | { 986 | "cell_type": "code", 987 | "execution_count": 169, 988 | "metadata": { 989 | "ExecuteTime": { 990 | "end_time": "2020-01-27T11:40:03.034805Z", 991 | "start_time": "2020-01-27T11:40:03.030814Z" 992 | } 993 | }, 994 | "outputs": [ 995 | { 996 | "name": "stdout", 997 | "output_type": "stream", 998 | "text": [ 999 | "3\n", 1000 | "10\n", 1001 | "2\n", 1002 | "5\n", 1003 | "2\n" 1004 | ] 1005 | } 1006 | ], 1007 | "source": [ 1008 | "import random\n", 1009 | "random.seed(1)\n", 1010 | "for i in range(5):\n", 1011 | " print(random.randint(1,10))" 1012 | ] 1013 | }, 1014 | { 1015 | "cell_type": "code", 1016 | "execution_count": 170, 1017 | "metadata": { 1018 | "ExecuteTime": { 1019 | "end_time": "2020-01-27T11:40:11.914602Z", 1020 | "start_time": "2020-01-27T11:40:11.911611Z" 1021 | } 1022 | }, 1023 | "outputs": [], 1024 | "source": [ 1025 | "import random,sys,os,math" 1026 | ] 1027 | }, 1028 | { 1029 | "cell_type": "markdown", 1030 | "metadata": {}, 1031 | "source": [ 1032 | "__from import Statements__" 1033 | ] 1034 | }, 1035 | { 1036 | "cell_type": "code", 1037 | "execution_count": 172, 1038 | "metadata": { 1039 | "ExecuteTime": { 1040 | "end_time": "2020-01-27T11:40:56.996023Z", 1041 | "start_time": "2020-01-27T11:40:56.991037Z" 1042 | } 1043 | }, 1044 | "outputs": [ 1045 | { 1046 | "data": { 1047 | "text/plain": [ 1048 | "2" 1049 | ] 1050 | }, 1051 | "execution_count": 172, 1052 | "metadata": {}, 1053 | "output_type": "execute_result" 1054 | } 1055 | ], 1056 | "source": [ 1057 | "from random import *\n", 1058 | "randint(1,2)" 1059 | ] 1060 | }, 1061 | { 1062 | "cell_type": "markdown", 1063 | "metadata": {}, 1064 | "source": [ 1065 | "### Ending a Program Early with `sys.exit()`" 1066 | ] 1067 | }, 1068 | { 1069 | "cell_type": "markdown", 1070 | "metadata": {}, 1071 | "source": [ 1072 | "THe last flow control concept to cover is how to terminate the program. This always happens if the program execution reaches the bottom of the instructions. However, you can cause the program to terminate, or exit, by calling the `sys.exit()` function. " 1073 | ] 1074 | }, 1075 | { 1076 | "cell_type": "code", 1077 | "execution_count": 177, 1078 | "metadata": { 1079 | "ExecuteTime": { 1080 | "end_time": "2020-01-27T11:55:14.488235Z", 1081 | "start_time": "2020-01-27T11:55:10.103574Z" 1082 | } 1083 | }, 1084 | "outputs": [ 1085 | { 1086 | "name": "stdout", 1087 | "output_type": "stream", 1088 | "text": [ 1089 | "Type exit to exit.\n", 1090 | "1\n", 1091 | "your typed 1.\n", 1092 | "Type exit to exit.\n", 1093 | "2\n", 1094 | "your typed 2.\n", 1095 | "Type exit to exit.\n", 1096 | "3\n", 1097 | "your typed 3.\n", 1098 | "Type exit to exit.\n", 1099 | "4\n", 1100 | "your typed 4.\n", 1101 | "Type exit to exit.\n", 1102 | "5\n", 1103 | "your typed 5.\n", 1104 | "Type exit to exit.\n", 1105 | "exit\n" 1106 | ] 1107 | }, 1108 | { 1109 | "ename": "SystemExit", 1110 | "evalue": "[1234]", 1111 | "output_type": "error", 1112 | "traceback": [ 1113 | "An exception has occurred, use %tb to see the full traceback.\n", 1114 | "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m [1234]\n" 1115 | ] 1116 | } 1117 | ], 1118 | "source": [ 1119 | "import sys\n", 1120 | "\n", 1121 | "while True:\n", 1122 | " print('Type exit to exit.')\n", 1123 | " response = input()\n", 1124 | " if response == 'exit':\n", 1125 | " sys.exit([1234]) # can take a status, always raise a SystemExit Error\n", 1126 | " print('your typed '+ response + '.')" 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "markdown", 1131 | "metadata": { 1132 | "ExecuteTime": { 1133 | "end_time": "2020-01-27T12:19:53.234492Z", 1134 | "start_time": "2020-01-27T12:19:53.230502Z" 1135 | } 1136 | }, 1137 | "source": [ 1138 | "### Practice Questions\n", 1139 | "\n", 1140 | "- 6. What is the difference between the equal to operator and the assignment opearator?\n", 1141 | "> The == operator(equal to) askes whether two values are the same as each other. The = operator(assignment operator) puts the value of right into the variable on the left.To help remember which is which, notice that the == operator (equal to)\n", 1142 | "consists of two characters, just like the != operator (not equal to) consists of\n", 1143 | "two characters.\n", 1144 | "\n", 1145 | "- 7. Explain what a conditon is and where you would use one.\n", 1146 | "> Conditon is an expression to be reduced to a Boolean value, which helps us choose what to do in a flow control.\n", 1147 | "\n" 1148 | ] 1149 | }, 1150 | { 1151 | "cell_type": "markdown", 1152 | "metadata": {}, 1153 | "source": [ 1154 | "## Functions" 1155 | ] 1156 | }, 1157 | { 1158 | "cell_type": "code", 1159 | "execution_count": 13, 1160 | "metadata": { 1161 | "ExecuteTime": { 1162 | "end_time": "2020-01-30T09:15:03.951765Z", 1163 | "start_time": "2020-01-30T09:15:03.943788Z" 1164 | } 1165 | }, 1166 | "outputs": [ 1167 | { 1168 | "ename": "UnboundLocalError", 1169 | "evalue": "local variable 'eggs' referenced before assignment", 1170 | "output_type": "error", 1171 | "traceback": [ 1172 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1173 | "\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", 1174 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0meggs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'global'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mspam\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1175 | "\u001b[1;32m\u001b[0m in \u001b[0;36mspam\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mspam\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0meggs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0meggs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'local'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0meggs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'global'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 1176 | "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'eggs' referenced before assignment" 1177 | ] 1178 | } 1179 | ], 1180 | "source": [ 1181 | "def spam():\n", 1182 | " print(eggs)\n", 1183 | " eggs = 'local'\n", 1184 | "\n", 1185 | "eggs = 'global'\n", 1186 | "spam()" 1187 | ] 1188 | }, 1189 | { 1190 | "cell_type": "markdown", 1191 | "metadata": {}, 1192 | "source": [ 1193 | "## Lists" 1194 | ] 1195 | }, 1196 | { 1197 | "cell_type": "markdown", 1198 | "metadata": {}, 1199 | "source": [ 1200 | "## Dictionaries and structuring data" 1201 | ] 1202 | }, 1203 | { 1204 | "cell_type": "code", 1205 | "execution_count": 5, 1206 | "metadata": { 1207 | "ExecuteTime": { 1208 | "end_time": "2020-01-31T08:23:45.461041Z", 1209 | "start_time": "2020-01-31T08:23:45.458049Z" 1210 | } 1211 | }, 1212 | "outputs": [], 1213 | "source": [ 1214 | "eggs = [1,2,3]\n", 1215 | "eggs2 = eggs.copy()" 1216 | ] 1217 | }, 1218 | { 1219 | "cell_type": "code", 1220 | "execution_count": 6, 1221 | "metadata": { 1222 | "ExecuteTime": { 1223 | "end_time": "2020-01-31T08:23:46.093312Z", 1224 | "start_time": "2020-01-31T08:23:46.090310Z" 1225 | } 1226 | }, 1227 | "outputs": [], 1228 | "source": [ 1229 | "eggs2[0] = '1'" 1230 | ] 1231 | }, 1232 | { 1233 | "cell_type": "code", 1234 | "execution_count": 7, 1235 | "metadata": { 1236 | "ExecuteTime": { 1237 | "end_time": "2020-01-31T08:23:46.653401Z", 1238 | "start_time": "2020-01-31T08:23:46.650406Z" 1239 | } 1240 | }, 1241 | "outputs": [ 1242 | { 1243 | "data": { 1244 | "text/plain": [ 1245 | "(['1', 2, 3], [1, 2, 3])" 1246 | ] 1247 | }, 1248 | "execution_count": 7, 1249 | "metadata": {}, 1250 | "output_type": "execute_result" 1251 | } 1252 | ], 1253 | "source": [ 1254 | "eggs2,eggs" 1255 | ] 1256 | }, 1257 | { 1258 | "cell_type": "code", 1259 | "execution_count": 9, 1260 | "metadata": { 1261 | "ExecuteTime": { 1262 | "end_time": "2020-01-31T08:38:48.990730Z", 1263 | "start_time": "2020-01-31T08:38:48.986764Z" 1264 | } 1265 | }, 1266 | "outputs": [ 1267 | { 1268 | "data": { 1269 | "text/plain": [ 1270 | "(2160054314056, 2160055178632)" 1271 | ] 1272 | }, 1273 | "execution_count": 9, 1274 | "metadata": {}, 1275 | "output_type": "execute_result" 1276 | } 1277 | ], 1278 | "source": [ 1279 | "id(eggs),id(eggs2)" 1280 | ] 1281 | }, 1282 | { 1283 | "cell_type": "code", 1284 | "execution_count": 27, 1285 | "metadata": { 1286 | "ExecuteTime": { 1287 | "end_time": "2020-01-31T09:08:21.830987Z", 1288 | "start_time": "2020-01-31T09:08:21.825998Z" 1289 | } 1290 | }, 1291 | "outputs": [ 1292 | { 1293 | "data": { 1294 | "text/plain": [ 1295 | "([1, 2, [3, 'missing', ['inner_missing', [10, 2, 3]]]],\n", 1296 | " [1, 2, [3, 2, ['inner1', [1, 2, 3]]]])" 1297 | ] 1298 | }, 1299 | "execution_count": 27, 1300 | "metadata": {}, 1301 | "output_type": "execute_result" 1302 | } 1303 | ], 1304 | "source": [ 1305 | "import copy\n", 1306 | "m_3 = [1,2,3]\n", 1307 | "m_2 = ['inner1',m_3]\n", 1308 | "m_1 = [3,2,m_2]\n", 1309 | "eggs = [1,2,m_1]\n", 1310 | "eggs2 = copy.deepcopy(eggs)\n", 1311 | "m_3[0] = 10\n", 1312 | "m_2[0] = 'inner_missing'\n", 1313 | "m_1[1] = 'missing'\n", 1314 | "eggs,eggs2" 1315 | ] 1316 | }, 1317 | { 1318 | "cell_type": "code", 1319 | "execution_count": 23, 1320 | "metadata": { 1321 | "ExecuteTime": { 1322 | "end_time": "2020-01-31T08:44:24.517768Z", 1323 | "start_time": "2020-01-31T08:44:24.514742Z" 1324 | } 1325 | }, 1326 | "outputs": [ 1327 | { 1328 | "data": { 1329 | "text/plain": [ 1330 | "['inner_missing', 'inner2']" 1331 | ] 1332 | }, 1333 | "execution_count": 23, 1334 | "metadata": {}, 1335 | "output_type": "execute_result" 1336 | } 1337 | ], 1338 | "source": [ 1339 | "m_2" 1340 | ] 1341 | }, 1342 | { 1343 | "cell_type": "code", 1344 | "execution_count": 29, 1345 | "metadata": { 1346 | "ExecuteTime": { 1347 | "end_time": "2020-01-31T09:14:33.373932Z", 1348 | "start_time": "2020-01-31T09:14:33.369915Z" 1349 | } 1350 | }, 1351 | "outputs": [ 1352 | { 1353 | "data": { 1354 | "text/plain": [ 1355 | "array([3, 8, 5, 5, 4, 7, 8, 2, 5, 1, 8, 6, 7, 1, 4, 1, 5, 6, 7, 3, 2, 9,\n", 1356 | " 7, 1, 1, 8, 5, 4, 1, 9, 5, 0, 1, 6, 3, 5, 2, 1, 5, 5, 9, 9, 7, 9,\n", 1357 | " 7, 5, 7, 6, 9, 4, 8, 6, 5, 0, 2, 5, 6, 8, 8, 1, 7, 3, 3, 6, 0, 8,\n", 1358 | " 6, 9, 4, 4, 8, 1, 9, 7, 2, 5, 6, 6, 8, 8, 3, 4, 7, 0, 0, 7, 2, 8,\n", 1359 | " 8, 3, 0, 9, 3, 5, 9, 5, 6, 6, 9, 5])" 1360 | ] 1361 | }, 1362 | "execution_count": 29, 1363 | "metadata": {}, 1364 | "output_type": "execute_result" 1365 | } 1366 | ], 1367 | "source": [ 1368 | "list_random = np.random.randint(0,10,100)\n", 1369 | "list_random" 1370 | ] 1371 | }, 1372 | { 1373 | "cell_type": "code", 1374 | "execution_count": 58, 1375 | "metadata": { 1376 | "ExecuteTime": { 1377 | "end_time": "2020-01-31T09:20:24.518305Z", 1378 | "start_time": "2020-01-31T09:20:22.953489Z" 1379 | } 1380 | }, 1381 | "outputs": [ 1382 | { 1383 | "name": "stdout", 1384 | "output_type": "stream", 1385 | "text": [ 1386 | "19.1 µs ± 708 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" 1387 | ] 1388 | } 1389 | ], 1390 | "source": [ 1391 | "%%timeit\n", 1392 | "[i for i, element in enumerate(list_random) if element == 1]" 1393 | ] 1394 | }, 1395 | { 1396 | "cell_type": "code", 1397 | "execution_count": 60, 1398 | "metadata": { 1399 | "ExecuteTime": { 1400 | "end_time": "2020-01-31T09:20:41.771992Z", 1401 | "start_time": "2020-01-31T09:20:40.144317Z" 1402 | } 1403 | }, 1404 | "outputs": [ 1405 | { 1406 | "name": "stdout", 1407 | "output_type": "stream", 1408 | "text": [ 1409 | "20 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" 1410 | ] 1411 | } 1412 | ], 1413 | "source": [ 1414 | "%%timeit\n", 1415 | "g = (i for i, element in enumerate(list_random) if element == 1)\n", 1416 | "list(g)" 1417 | ] 1418 | }, 1419 | { 1420 | "cell_type": "code", 1421 | "execution_count": 189, 1422 | "metadata": { 1423 | "ExecuteTime": { 1424 | "end_time": "2020-01-31T11:34:10.553053Z", 1425 | "start_time": "2020-01-31T11:34:10.550034Z" 1426 | } 1427 | }, 1428 | "outputs": [], 1429 | "source": [ 1430 | "list_random = np.random.randint(0,10000,10000)" 1431 | ] 1432 | }, 1433 | { 1434 | "cell_type": "code", 1435 | "execution_count": 190, 1436 | "metadata": { 1437 | "ExecuteTime": { 1438 | "end_time": "2020-01-31T11:34:26.276708Z", 1439 | "start_time": "2020-01-31T11:34:11.190907Z" 1440 | } 1441 | }, 1442 | "outputs": [ 1443 | { 1444 | "name": "stdout", 1445 | "output_type": "stream", 1446 | "text": [ 1447 | "1.86 ms ± 1.53 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" 1448 | ] 1449 | } 1450 | ], 1451 | "source": [ 1452 | "%%timeit\n", 1453 | "[i for i, element in enumerate(list_random) if element == 1]" 1454 | ] 1455 | }, 1456 | { 1457 | "cell_type": "code", 1458 | "execution_count": 191, 1459 | "metadata": { 1460 | "ExecuteTime": { 1461 | "end_time": "2020-01-31T11:34:42.376683Z", 1462 | "start_time": "2020-01-31T11:34:26.286682Z" 1463 | } 1464 | }, 1465 | "outputs": [ 1466 | { 1467 | "name": "stdout", 1468 | "output_type": "stream", 1469 | "text": [ 1470 | "1.98 ms ± 3.24 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" 1471 | ] 1472 | } 1473 | ], 1474 | "source": [ 1475 | "%%timeit\n", 1476 | "g = (i for i, element in enumerate(list_random) if element == 1)\n", 1477 | "list(g)" 1478 | ] 1479 | }, 1480 | { 1481 | "cell_type": "code", 1482 | "execution_count": 192, 1483 | "metadata": { 1484 | "ExecuteTime": { 1485 | "end_time": "2020-01-31T11:34:54.537041Z", 1486 | "start_time": "2020-01-31T11:34:42.385632Z" 1487 | } 1488 | }, 1489 | "outputs": [ 1490 | { 1491 | "name": "stdout", 1492 | "output_type": "stream", 1493 | "text": [ 1494 | "1.5 ms ± 7.29 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" 1495 | ] 1496 | } 1497 | ], 1498 | "source": [ 1499 | "%%timeit\n", 1500 | "get_indices(list(list_random),1)" 1501 | ] 1502 | }, 1503 | { 1504 | "cell_type": "code", 1505 | "execution_count": 176, 1506 | "metadata": { 1507 | "ExecuteTime": { 1508 | "end_time": "2020-01-31T11:12:25.237476Z", 1509 | "start_time": "2020-01-31T11:12:25.232514Z" 1510 | } 1511 | }, 1512 | "outputs": [], 1513 | "source": [ 1514 | "# https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list\n", 1515 | "def get_indices(x: list, value: int) -> list:\n", 1516 | " indices = list()\n", 1517 | " i = 0\n", 1518 | " while True:\n", 1519 | " try:\n", 1520 | " # find an occurrence of value and update i to that index\n", 1521 | " i = x.index(value, i)\n", 1522 | " # add i to the list\n", 1523 | " indices.append(i)\n", 1524 | " # advance i by 1\n", 1525 | " i += 1\n", 1526 | " except ValueError as e:\n", 1527 | " break\n", 1528 | " return indices" 1529 | ] 1530 | }, 1531 | { 1532 | "cell_type": "code", 1533 | "execution_count": 64, 1534 | "metadata": { 1535 | "ExecuteTime": { 1536 | "end_time": "2020-01-31T09:22:59.173326Z", 1537 | "start_time": "2020-01-31T09:22:59.170334Z" 1538 | } 1539 | }, 1540 | "outputs": [], 1541 | "source": [ 1542 | "def gen_func():\n", 1543 | " for i in range(5):\n", 1544 | " yield i, i**2" 1545 | ] 1546 | }, 1547 | { 1548 | "cell_type": "code", 1549 | "execution_count": 65, 1550 | "metadata": { 1551 | "ExecuteTime": { 1552 | "end_time": "2020-01-31T09:23:19.588176Z", 1553 | "start_time": "2020-01-31T09:23:19.585179Z" 1554 | } 1555 | }, 1556 | "outputs": [], 1557 | "source": [ 1558 | "x1, x2 = zip(*gen_func())" 1559 | ] 1560 | }, 1561 | { 1562 | "cell_type": "code", 1563 | "execution_count": 70, 1564 | "metadata": { 1565 | "ExecuteTime": { 1566 | "end_time": "2020-01-31T09:23:42.037862Z", 1567 | "start_time": "2020-01-31T09:23:42.033873Z" 1568 | } 1569 | }, 1570 | "outputs": [ 1571 | { 1572 | "data": { 1573 | "text/plain": [ 1574 | "[(0, 1, 2, 3, 4), (0, 1, 4, 9, 16)]" 1575 | ] 1576 | }, 1577 | "execution_count": 70, 1578 | "metadata": {}, 1579 | "output_type": "execute_result" 1580 | } 1581 | ], 1582 | "source": [ 1583 | "list(zip(*gen_func()))" 1584 | ] 1585 | }, 1586 | { 1587 | "cell_type": "code", 1588 | "execution_count": 82, 1589 | "metadata": { 1590 | "ExecuteTime": { 1591 | "end_time": "2020-01-31T09:26:11.436231Z", 1592 | "start_time": "2020-01-31T09:26:11.433216Z" 1593 | } 1594 | }, 1595 | "outputs": [], 1596 | "source": [ 1597 | "(*x1,),(*x2,) = list(zip(*gen_func()))" 1598 | ] 1599 | }, 1600 | { 1601 | "cell_type": "code", 1602 | "execution_count": 84, 1603 | "metadata": { 1604 | "ExecuteTime": { 1605 | "end_time": "2020-01-31T09:27:18.181018Z", 1606 | "start_time": "2020-01-31T09:27:18.177029Z" 1607 | } 1608 | }, 1609 | "outputs": [ 1610 | { 1611 | "data": { 1612 | "text/plain": [ 1613 | "[((0, 0),), ((1, 1),), ((2, 4),), ((3, 9),), ((4, 16),)]" 1614 | ] 1615 | }, 1616 | "execution_count": 84, 1617 | "metadata": {}, 1618 | "output_type": "execute_result" 1619 | } 1620 | ], 1621 | "source": [ 1622 | "list(zip(gen_func()))" 1623 | ] 1624 | }, 1625 | { 1626 | "cell_type": "code", 1627 | "execution_count": 168, 1628 | "metadata": { 1629 | "ExecuteTime": { 1630 | "end_time": "2020-01-31T09:34:06.644723Z", 1631 | "start_time": "2020-01-31T09:34:06.640699Z" 1632 | } 1633 | }, 1634 | "outputs": [], 1635 | "source": [ 1636 | "g = gen_func()\n", 1637 | "a = []\n", 1638 | "b = []" 1639 | ] 1640 | }, 1641 | { 1642 | "cell_type": "code", 1643 | "execution_count": 174, 1644 | "metadata": { 1645 | "ExecuteTime": { 1646 | "end_time": "2020-01-31T09:34:09.240444Z", 1647 | "start_time": "2020-01-31T09:34:09.233456Z" 1648 | } 1649 | }, 1650 | "outputs": [ 1651 | { 1652 | "ename": "StopIteration", 1653 | "evalue": "", 1654 | "output_type": "error", 1655 | "traceback": [ 1656 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1657 | "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", 1658 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mai\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mbi\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mai\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 1659 | "\u001b[1;31mStopIteration\u001b[0m: " 1660 | ] 1661 | } 1662 | ], 1663 | "source": [ 1664 | "ai,bi = next(g)\n", 1665 | "a.append(ai)\n", 1666 | "b.append(bi)\n", 1667 | "a,b" 1668 | ] 1669 | }, 1670 | { 1671 | "cell_type": "code", 1672 | "execution_count": 175, 1673 | "metadata": { 1674 | "ExecuteTime": { 1675 | "end_time": "2020-01-31T09:34:12.893158Z", 1676 | "start_time": "2020-01-31T09:34:12.890143Z" 1677 | } 1678 | }, 1679 | "outputs": [ 1680 | { 1681 | "data": { 1682 | "text/plain": [ 1683 | "([0, 1, 2, 3, 4], [0, 1, 4, 9, 16])" 1684 | ] 1685 | }, 1686 | "execution_count": 175, 1687 | "metadata": {}, 1688 | "output_type": "execute_result" 1689 | } 1690 | ], 1691 | "source": [ 1692 | "a,b" 1693 | ] 1694 | }, 1695 | { 1696 | "cell_type": "code", 1697 | "execution_count": 159, 1698 | "metadata": { 1699 | "ExecuteTime": { 1700 | "end_time": "2020-01-31T09:33:34.987533Z", 1701 | "start_time": "2020-01-31T09:33:34.984544Z" 1702 | } 1703 | }, 1704 | "outputs": [ 1705 | { 1706 | "data": { 1707 | "text/plain": [ 1708 | "[0]" 1709 | ] 1710 | }, 1711 | "execution_count": 159, 1712 | "metadata": {}, 1713 | "output_type": "execute_result" 1714 | } 1715 | ], 1716 | "source": [ 1717 | "a" 1718 | ] 1719 | }, 1720 | { 1721 | "cell_type": "markdown", 1722 | "metadata": {}, 1723 | "source": [ 1724 | "## Manipulating strings" 1725 | ] 1726 | }, 1727 | { 1728 | "cell_type": "code", 1729 | "execution_count": null, 1730 | "metadata": {}, 1731 | "outputs": [], 1732 | "source": [] 1733 | } 1734 | ], 1735 | "metadata": { 1736 | "kernelspec": { 1737 | "display_name": "Python 3", 1738 | "language": "python", 1739 | "name": "python3" 1740 | }, 1741 | "language_info": { 1742 | "codemirror_mode": { 1743 | "name": "ipython", 1744 | "version": 3 1745 | }, 1746 | "file_extension": ".py", 1747 | "mimetype": "text/x-python", 1748 | "name": "python", 1749 | "nbconvert_exporter": "python", 1750 | "pygments_lexer": "ipython3", 1751 | "version": "3.7.1" 1752 | }, 1753 | "toc": { 1754 | "base_numbering": 1, 1755 | "nav_menu": {}, 1756 | "number_sections": true, 1757 | "sideBar": true, 1758 | "skip_h1_title": false, 1759 | "title_cell": "Table of Contents", 1760 | "title_sidebar": "Contents", 1761 | "toc_cell": false, 1762 | "toc_position": {}, 1763 | "toc_section_display": true, 1764 | "toc_window_display": true 1765 | }, 1766 | "varInspector": { 1767 | "cols": { 1768 | "lenName": 16, 1769 | "lenType": 16, 1770 | "lenVar": 40 1771 | }, 1772 | "kernels_config": { 1773 | "python": { 1774 | "delete_cmd_postfix": "", 1775 | "delete_cmd_prefix": "del ", 1776 | "library": "var_list.py", 1777 | "varRefreshCmd": "print(var_dic_list())" 1778 | }, 1779 | "r": { 1780 | "delete_cmd_postfix": ") ", 1781 | "delete_cmd_prefix": "rm(", 1782 | "library": "var_list.r", 1783 | "varRefreshCmd": "cat(var_dic_list()) " 1784 | } 1785 | }, 1786 | "types_to_exclude": [ 1787 | "module", 1788 | "function", 1789 | "builtin_function_or_method", 1790 | "instance", 1791 | "_Feature" 1792 | ], 1793 | "window_display": false 1794 | } 1795 | }, 1796 | "nbformat": 4, 1797 | "nbformat_minor": 2 1798 | } 1799 | -------------------------------------------------------------------------------- /Al Sweigart - Automate the Boring Stuff with Python_ Practical Programming for Total Beginners-No Starch Press (2015).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thisisreallife/automate-the-boring-stuff-with-python/40f1f6f5da2ade631abd743e32f9fdee11466d4c/Al Sweigart - Automate the Boring Stuff with Python_ Practical Programming for Total Beginners-No Starch Press (2015).pdf -------------------------------------------------------------------------------- /Automate_the_Boring_Stuff_onlinematerials.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thisisreallife/automate-the-boring-stuff-with-python/40f1f6f5da2ade631abd743e32f9fdee11466d4c/Automate_the_Boring_Stuff_onlinematerials.zip -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # automate-the-boring-stuff-with-python 2 | My code and note for this book 3 | -------------------------------------------------------------------------------- /ch1/.ipynb_checkpoints/ch1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2020-01-27T06:01:22.856977Z", 9 | "start_time": "2020-01-27T06:01:13.734607Z" 10 | } 11 | }, 12 | "outputs": [ 13 | { 14 | "name": "stdout", 15 | "output_type": "stream", 16 | "text": [ 17 | "hello world!\n", 18 | "What is your name?\n", 19 | "sunshiqing\n", 20 | "It is good to meet you, sunshiqing\n", 21 | "10\n", 22 | "What is your age?\n", 23 | "12\n", 24 | "You will be 13 in a year,\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "# This program says hello and asks for my name\n", 30 | "\n", 31 | "print('hello world!')\n", 32 | "print('What is your name?') # ask for their name\n", 33 | "myName = input()\n", 34 | "print('It is good to meet you, ' + myName)\n", 35 | "print(len(myName))\n", 36 | "print('What is your age?') # ask for their age\n", 37 | "myAge = input()\n", 38 | "print('You will be ' + str(int(myAge) + 1) + ' in a year.')" 39 | ] 40 | } 41 | ], 42 | "metadata": { 43 | "kernelspec": { 44 | "display_name": "Python 3", 45 | "language": "python", 46 | "name": "python3" 47 | }, 48 | "language_info": { 49 | "codemirror_mode": { 50 | "name": "ipython", 51 | "version": 3 52 | }, 53 | "file_extension": ".py", 54 | "mimetype": "text/x-python", 55 | "name": "python", 56 | "nbconvert_exporter": "python", 57 | "pygments_lexer": "ipython3", 58 | "version": "3.7.1" 59 | }, 60 | "toc": { 61 | "base_numbering": 1, 62 | "nav_menu": {}, 63 | "number_sections": true, 64 | "sideBar": true, 65 | "skip_h1_title": false, 66 | "title_cell": "Table of Contents", 67 | "title_sidebar": "Contents", 68 | "toc_cell": false, 69 | "toc_position": {}, 70 | "toc_section_display": true, 71 | "toc_window_display": false 72 | }, 73 | "varInspector": { 74 | "cols": { 75 | "lenName": 16, 76 | "lenType": 16, 77 | "lenVar": 40 78 | }, 79 | "kernels_config": { 80 | "python": { 81 | "delete_cmd_postfix": "", 82 | "delete_cmd_prefix": "del ", 83 | "library": "var_list.py", 84 | "varRefreshCmd": "print(var_dic_list())" 85 | }, 86 | "r": { 87 | "delete_cmd_postfix": ") ", 88 | "delete_cmd_prefix": "rm(", 89 | "library": "var_list.r", 90 | "varRefreshCmd": "cat(var_dic_list()) " 91 | } 92 | }, 93 | "types_to_exclude": [ 94 | "module", 95 | "function", 96 | "builtin_function_or_method", 97 | "instance", 98 | "_Feature" 99 | ], 100 | "window_display": false 101 | } 102 | }, 103 | "nbformat": 4, 104 | "nbformat_minor": 2 105 | } 106 | -------------------------------------------------------------------------------- /ch1/ch1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2020-01-27T06:01:22.856977Z", 9 | "start_time": "2020-01-27T06:01:13.734607Z" 10 | } 11 | }, 12 | "outputs": [ 13 | { 14 | "name": "stdout", 15 | "output_type": "stream", 16 | "text": [ 17 | "hello world!\n", 18 | "What is your name?\n", 19 | "sunshiqing\n", 20 | "It is good to meet you, sunshiqing\n", 21 | "10\n", 22 | "What is your age?\n", 23 | "12\n", 24 | "You will be 13 in a year,\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "# This program says hello and asks for my name\n", 30 | "\n", 31 | "print('hello world!')\n", 32 | "print('What is your name?') # ask for their name\n", 33 | "myName = input()\n", 34 | "print('It is good to meet you, ' + myName)\n", 35 | "print(len(myName))\n", 36 | "print('What is your age?') # ask for their age\n", 37 | "myAge = input()\n", 38 | "print('You will be ' + str(int(myAge) + 1) + ' in a year.')" 39 | ] 40 | } 41 | ], 42 | "metadata": { 43 | "kernelspec": { 44 | "display_name": "Python 3", 45 | "language": "python", 46 | "name": "python3" 47 | }, 48 | "language_info": { 49 | "codemirror_mode": { 50 | "name": "ipython", 51 | "version": 3 52 | }, 53 | "file_extension": ".py", 54 | "mimetype": "text/x-python", 55 | "name": "python", 56 | "nbconvert_exporter": "python", 57 | "pygments_lexer": "ipython3", 58 | "version": "3.7.1" 59 | }, 60 | "toc": { 61 | "base_numbering": 1, 62 | "nav_menu": {}, 63 | "number_sections": true, 64 | "sideBar": true, 65 | "skip_h1_title": false, 66 | "title_cell": "Table of Contents", 67 | "title_sidebar": "Contents", 68 | "toc_cell": false, 69 | "toc_position": {}, 70 | "toc_section_display": true, 71 | "toc_window_display": false 72 | }, 73 | "varInspector": { 74 | "cols": { 75 | "lenName": 16, 76 | "lenType": 16, 77 | "lenVar": 40 78 | }, 79 | "kernels_config": { 80 | "python": { 81 | "delete_cmd_postfix": "", 82 | "delete_cmd_prefix": "del ", 83 | "library": "var_list.py", 84 | "varRefreshCmd": "print(var_dic_list())" 85 | }, 86 | "r": { 87 | "delete_cmd_postfix": ") ", 88 | "delete_cmd_prefix": "rm(", 89 | "library": "var_list.r", 90 | "varRefreshCmd": "cat(var_dic_list()) " 91 | } 92 | }, 93 | "types_to_exclude": [ 94 | "module", 95 | "function", 96 | "builtin_function_or_method", 97 | "instance", 98 | "_Feature" 99 | ], 100 | "window_display": false 101 | } 102 | }, 103 | "nbformat": 4, 104 | "nbformat_minor": 2 105 | } 106 | -------------------------------------------------------------------------------- /ch2/fig2-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thisisreallife/automate-the-boring-stuff-with-python/40f1f6f5da2ade631abd743e32f9fdee11466d4c/ch2/fig2-1.jpg -------------------------------------------------------------------------------- /notes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# The preface " 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "When I doing [google hashcode 2018](https://codingcompetitions.withgoogle.com/hashcode/archive), I found that I could not finished the task in 3 hours. It's so hard for me.\n", 15 | "\n", 16 | "The first thing that drags me is that I do not know how to realize a functionality in a decent way. I just spend 5 hours to write a helper function into the loop. The second thing is that I do not really know whether I should use numpy-array,list or dict to store my objects. The third thing is that I do not good at debug my program. Although I write my ideas on a blank paper and then coding it. There are still several bugs in the first draft code. I spend a little time to debug by code.\n", 17 | "\n", 18 | "After I finished my code, I go the rank board of hashcode 2018. I found that I did not give a good solution. I only care about coding and did not really focus on math. The NP-hard problem need a lot of mathmatic knowledge to give a estimated solution. I did not good at optimsing and algorithms. I watch a winners' video on Youtube. He is a Russian and he spend all his time to mathmatic part of the question and do not feel any impeded by coding part. Which makes me feel that I should code more and let code by my natural. If I am really good at coding, I think I can focus more on math when solving the problem.\n", 19 | "\n", 20 | "To achieve a better understanding of python standard library, I decided to read the book: **Automate The Broing Stuff with Python**. I think I can know what functionality can be easily realized by standard library and what is not." 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": { 26 | "ExecuteTime": { 27 | "end_time": "2020-01-27T03:05:56.136259Z", 28 | "start_time": "2020-01-27T03:05:56.133295Z" 29 | } 30 | }, 31 | "source": [ 32 | "# Part 1: Python Programming Basics \n", 33 | "## Python Basics" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": { 39 | "ExecuteTime": { 40 | "end_time": "2020-01-27T03:11:45.849032Z", 41 | "start_time": "2020-01-27T03:11:45.846038Z" 42 | } 43 | }, 44 | "source": [ 45 | "### Entreing Expressions into the Interactive Shell\n", 46 | "\n", 47 | "| Operator | Operation | Example | Evaluates to |\n", 48 | "|:--------:|:---------------------------------:|:-------:|:------------:|\n", 49 | "| ** | Exponent | 2 ** 3 | 8 |\n", 50 | "| % | Modeulus/remainder | 22 % 8 | 6 |\n", 51 | "| // | Integer division/floored quotient | 22//8 | 2 |\n", 52 | "| / | Division | 22/8 | 2.75 |\n", 53 | "| * | Multiplication | 3*5 | 15 |\n", 54 | "| - | Substraction | 5-2 | 3 |\n", 55 | "| + | Addition | 2+2 | 4 |" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": { 61 | "ExecuteTime": { 62 | "end_time": "2020-01-27T03:26:17.592346Z", 63 | "start_time": "2020-01-27T03:26:17.480645Z" 64 | } 65 | }, 66 | "source": [ 67 | "### The Integer,Floating-Point, and String Data Types" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "### String Concatenation and Replication" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 4, 80 | "metadata": { 81 | "ExecuteTime": { 82 | "end_time": "2020-01-27T03:27:47.879330Z", 83 | "start_time": "2020-01-27T03:27:47.872346Z" 84 | } 85 | }, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "'AliceBob'" 91 | ] 92 | }, 93 | "execution_count": 4, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "'Alice'+'Bob'" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 5, 105 | "metadata": { 106 | "ExecuteTime": { 107 | "end_time": "2020-01-27T03:27:57.823253Z", 108 | "start_time": "2020-01-27T03:27:57.818245Z" 109 | } 110 | }, 111 | "outputs": [ 112 | { 113 | "ename": "SyntaxError", 114 | "evalue": "can't assign to literal (, line 1)", 115 | "output_type": "error", 116 | "traceback": [ 117 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m 'Alice'=42\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m can't assign to literal\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "'Alice'=42" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 6, 128 | "metadata": { 129 | "ExecuteTime": { 130 | "end_time": "2020-01-27T03:28:44.327387Z", 131 | "start_time": "2020-01-27T03:28:44.324367Z" 132 | } 133 | }, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "'AliceAliceAliceAliceAlice'" 139 | ] 140 | }, 141 | "execution_count": 6, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "'Alice'*5" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 7, 153 | "metadata": { 154 | "ExecuteTime": { 155 | "end_time": "2020-01-27T03:28:53.077738Z", 156 | "start_time": "2020-01-27T03:28:53.074745Z" 157 | } 158 | }, 159 | "outputs": [ 160 | { 161 | "ename": "SyntaxError", 162 | "evalue": "invalid syntax (, line 1)", 163 | "output_type": "error", 164 | "traceback": [ 165 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m 'Alice'Bob'\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" 166 | ] 167 | } 168 | ], 169 | "source": [ 170 | "'Alice'Bob'" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 9, 176 | "metadata": { 177 | "ExecuteTime": { 178 | "end_time": "2020-01-27T03:29:03.311468Z", 179 | "start_time": "2020-01-27T03:29:03.306441Z" 180 | } 181 | }, 182 | "outputs": [ 183 | { 184 | "ename": "TypeError", 185 | "evalue": "can't multiply sequence by non-int of type 'float'", 186 | "output_type": "error", 187 | "traceback": [ 188 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 189 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 190 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;34m'Alice'\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;36m5.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 191 | "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'float'" 192 | ] 193 | } 194 | ], 195 | "source": [ 196 | "'Alice' * 5.0" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "### Storing Values in Variables\n", 204 | "\n", 205 | "A _variable_ is like a box in the computer's memory where you can store a single value. If you wan to use the result of an evaluated expression later in your program, you can save it inside a variable." 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": { 211 | "ExecuteTime": { 212 | "end_time": "2020-01-27T03:52:09.629559Z", 213 | "start_time": "2020-01-27T03:52:09.626540Z" 214 | } 215 | }, 216 | "source": [ 217 | "__Assignment statements__" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 10, 223 | "metadata": { 224 | "ExecuteTime": { 225 | "end_time": "2020-01-27T03:50:09.600079Z", 226 | "start_time": "2020-01-27T03:50:09.597083Z" 227 | } 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "spam = 42\n", 232 | "eggs = 2" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 12, 238 | "metadata": { 239 | "ExecuteTime": { 240 | "end_time": "2020-01-27T03:50:28.935088Z", 241 | "start_time": "2020-01-27T03:50:28.930100Z" 242 | } 243 | }, 244 | "outputs": [ 245 | { 246 | "data": { 247 | "text/plain": [ 248 | "(44, 86)" 249 | ] 250 | }, 251 | "execution_count": 12, 252 | "metadata": {}, 253 | "output_type": "execute_result" 254 | } 255 | ], 256 | "source": [ 257 | "spam + eggs,spam + eggs + spam" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": 13, 263 | "metadata": { 264 | "ExecuteTime": { 265 | "end_time": "2020-01-27T03:50:40.909261Z", 266 | "start_time": "2020-01-27T03:50:40.906246Z" 267 | } 268 | }, 269 | "outputs": [ 270 | { 271 | "data": { 272 | "text/plain": [ 273 | "44" 274 | ] 275 | }, 276 | "execution_count": 13, 277 | "metadata": {}, 278 | "output_type": "execute_result" 279 | } 280 | ], 281 | "source": [ 282 | "spam = spam + 2\n", 283 | "spam" 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "metadata": {}, 289 | "source": [ 290 | "Overwriting a variable" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 20, 296 | "metadata": { 297 | "ExecuteTime": { 298 | "end_time": "2020-01-27T03:51:28.925024Z", 299 | "start_time": "2020-01-27T03:51:28.923004Z" 300 | } 301 | }, 302 | "outputs": [ 303 | { 304 | "data": { 305 | "text/plain": [ 306 | "'Hello'" 307 | ] 308 | }, 309 | "execution_count": 20, 310 | "metadata": {}, 311 | "output_type": "execute_result" 312 | } 313 | ], 314 | "source": [ 315 | "spam = 'Hello'\n", 316 | "spam" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 26, 322 | "metadata": { 323 | "ExecuteTime": { 324 | "end_time": "2020-01-27T04:15:16.203312Z", 325 | "start_time": "2020-01-27T04:15:16.200328Z" 326 | } 327 | }, 328 | "outputs": [ 329 | { 330 | "data": { 331 | "text/plain": [ 332 | "'Goodbye'" 333 | ] 334 | }, 335 | "execution_count": 26, 336 | "metadata": {}, 337 | "output_type": "execute_result" 338 | } 339 | ], 340 | "source": [ 341 | "spam = 'Goodbye'\n", 342 | "spam" 343 | ] 344 | }, 345 | { 346 | "cell_type": "markdown", 347 | "metadata": {}, 348 | "source": [ 349 | "### Variable names\n", 350 | "\n", 351 | "1. It can be only one word\n", 352 | "2. It can use only letters, numbers and the underscore(_) character\n", 353 | "3. It can't begin with a number" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "| Valid variable names | Invalid variable names |\n", 361 | "|----------------------|--------------------------------------------------------|\n", 362 | "| balance | current-balance (hyphens are not allowed) |\n", 363 | "| currentBalance | current balance (spaces are not allowed) |\n", 364 | "| current_balance | 4account (can't begin with a number) |\n", 365 | "| _spam | 42 (can't begin with a number) |\n", 366 | "| SPAM | total_\\\\$um (special characters like \\\\$ are not allowed) |\n", 367 | "| account4 | 'hello' (special characters like ' are not allowed) |" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "metadata": { 373 | "ExecuteTime": { 374 | "end_time": "2020-01-27T04:01:37.277242Z", 375 | "start_time": "2020-01-27T04:01:37.274222Z" 376 | } 377 | }, 378 | "source": [ 379 | "Variable names are case-sensitive, meaning that spam,SPAM,Span and sPam are four differnet variables. It is a Python convention to start your variables with a lowercase letter.\n", 380 | "\n", 381 | "This book uses camelcase for variable names instead of underscores; that is , variables `lookLikeThis` instead of `looking_like_this`. Some experienced programmers may point out that the official Python code style.,PEP 8, says that underscores should be used.\n", 382 | "\n", 383 | "A good variable name describes the data it contains. It will make your code more readable." 384 | ] 385 | }, 386 | { 387 | "cell_type": "markdown", 388 | "metadata": {}, 389 | "source": [ 390 | "### Your First Program" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 46, 396 | "metadata": { 397 | "ExecuteTime": { 398 | "end_time": "2020-01-27T06:03:25.289150Z", 399 | "start_time": "2020-01-27T06:03:21.336540Z" 400 | } 401 | }, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "hello world!\n", 408 | "What is your name?\n", 409 | "Travis\n", 410 | "It is good to meet you, Travis\n", 411 | "6\n", 412 | "What is your age?\n", 413 | "12\n", 414 | "You will be 13 in a year,\n" 415 | ] 416 | } 417 | ], 418 | "source": [ 419 | "%run ./ch1/ch1.ipynb" 420 | ] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": { 425 | "ExecuteTime": { 426 | "end_time": "2020-01-27T05:37:24.148628Z", 427 | "start_time": "2020-01-27T05:37:24.144619Z" 428 | } 429 | }, 430 | "source": [ 431 | "Python ignores comments, and you can use them to write notes or remind your self what hte code is tring to do.Any text for the rest of the line following a hash mark(#) is a part of a comment. \n", 432 | "\n", 433 | "Sometimes, programmers will put a # in front of a line of code to temporarily remove it while testiing a pogram. This is called __commenting out code__, and it can be useful when you're trying to figure out why a program doesnt't work. YOu can remove the # when you are ready to put the line back in.\n", 434 | "\n", 435 | "Python can not __concatenate__ different types of objects. For instance, Python do not know how to evaluate `5 + '1'`. However, we can use `int`,`str` and `float` functions to change the type of variables. Then, we can operate on variables with the same type." 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 47, 441 | "metadata": { 442 | "ExecuteTime": { 443 | "end_time": "2020-01-27T06:20:20.229377Z", 444 | "start_time": "2020-01-27T06:20:20.223386Z" 445 | } 446 | }, 447 | "outputs": [ 448 | { 449 | "ename": "TypeError", 450 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 451 | "output_type": "error", 452 | "traceback": [ 453 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 454 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 455 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m5\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m'1'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 456 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 457 | ] 458 | } 459 | ], 460 | "source": [ 461 | "5 + '1'" 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "execution_count": 48, 467 | "metadata": { 468 | "ExecuteTime": { 469 | "end_time": "2020-01-27T06:21:24.698310Z", 470 | "start_time": "2020-01-27T06:21:24.693324Z" 471 | } 472 | }, 473 | "outputs": [ 474 | { 475 | "data": { 476 | "text/plain": [ 477 | "6" 478 | ] 479 | }, 480 | "execution_count": 48, 481 | "metadata": {}, 482 | "output_type": "execute_result" 483 | } 484 | ], 485 | "source": [ 486 | "5 + int('1')" 487 | ] 488 | }, 489 | { 490 | "cell_type": "markdown", 491 | "metadata": {}, 492 | "source": [ 493 | "We can use int to round a floating-point number down" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 50, 499 | "metadata": { 500 | "ExecuteTime": { 501 | "end_time": "2020-01-27T06:38:03.449576Z", 502 | "start_time": "2020-01-27T06:38:03.445618Z" 503 | } 504 | }, 505 | "outputs": [ 506 | { 507 | "data": { 508 | "text/plain": [ 509 | "7" 510 | ] 511 | }, 512 | "execution_count": 50, 513 | "metadata": {}, 514 | "output_type": "execute_result" 515 | } 516 | ], 517 | "source": [ 518 | "int(7.7)" 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": {}, 524 | "source": [ 525 | "### Practice Questions\n", 526 | "\n", 527 | "- 4. What is an expression made up of? What do all expressions do? What is the similarity and difference between `expression` and `statement`?\n", 528 | "\n", 529 | "> An expresson can only contains identifiers, literals and operators. However, a statement.All expressions need to be evaluated and to reduce to a value, which can be any type of objects in Python.\n", 530 | "A statement is an instruction that the Python interpreter can execute. We have only seen the assignment statement so far. Some other kinds of statements that we’ll see shortly are `while` statements, `for` statements, `if` statements, and `import` statements. (There are other kinds too!)\n", 531 | "\n", 532 | "> An expression is a combination of values, variables, operators, and calls to functions. Expressions need to be evaluated. If you ask Python to `print` an expression, the interpreter evaluates the expression and displays the result.\n", 533 | "\n", 534 | "> Expression is a part of statements.\n", 535 | "\n", 536 | "> Source: \n", 537 | "1. [What is the difference between an expression and a statement in Python?](https://stackoverflow.com/questions/4728073/what-is-the-difference-between-an-expression-and-a-statement-in-python) See the top2 answer and the last ansmer.\n", 538 | "2. [official guide for expressions](https://docs.python.org/zh-cn/3/reference/expressions.html).\n", 539 | "3. [official guide for statements](https://docs.python.org/3/reference/simple_stmts.html)\n", 540 | "4. [thinkcspy](https://runestone.academy/runestone/books/published/thinkcspy/SimplePythonData/StatementsandExpressions.html)\n", 541 | "\n", 542 | "\n", 543 | "- 9. What three functions can be used to get the integer, floating-point number, or string version of a value?\n", 544 | "\n", 545 | "> `int`,`float` and `str`.\n", 546 | "\n", 547 | "- Extra credit: Search online for the Pyton documentation for the `len()` function. It will be on a web page titled \"Built-in Functions.\"Skim the list of ohter functions Python has, look up what the `round` function does, and experiment with it in the interactive shell.\n", 548 | "\n", 549 | "> For the built-in types supporting `round()`, values are rounded to the closest multiple of 10 to the power minus __ndigits__; if two multiples are equally close, rounding is done toward the even choice (so, for example, both `round(0.5)` and round`(-0.5)` are `0`, and `round(1.5)` is `2`). Any integer value is valid for __ndigits__ (positive, zero, or negative). The return value is an integer if ndigits is omitted or `None`. Otherwise the return value has the same type as number." 550 | ] 551 | }, 552 | { 553 | "cell_type": "code", 554 | "execution_count": 99, 555 | "metadata": { 556 | "ExecuteTime": { 557 | "end_time": "2020-01-27T07:17:05.469053Z", 558 | "start_time": "2020-01-27T07:17:05.465069Z" 559 | } 560 | }, 561 | "outputs": [ 562 | { 563 | "data": { 564 | "text/plain": [ 565 | "10.01" 566 | ] 567 | }, 568 | "execution_count": 99, 569 | "metadata": {}, 570 | "output_type": "execute_result" 571 | } 572 | ], 573 | "source": [ 574 | "round(10.01457,2)" 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": 103, 580 | "metadata": { 581 | "ExecuteTime": { 582 | "end_time": "2020-01-27T07:18:13.103324Z", 583 | "start_time": "2020-01-27T07:18:13.100321Z" 584 | } 585 | }, 586 | "outputs": [ 587 | { 588 | "data": { 589 | "text/plain": [ 590 | "10.02" 591 | ] 592 | }, 593 | "execution_count": 103, 594 | "metadata": {}, 595 | "output_type": "execute_result" 596 | } 597 | ], 598 | "source": [ 599 | "# because 10.02 is even and 10.01 is old, round to the even choice when 2 multiples are equally close\n", 600 | "round(10.015,2)" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 104, 606 | "metadata": { 607 | "ExecuteTime": { 608 | "end_time": "2020-01-27T07:18:14.787101Z", 609 | "start_time": "2020-01-27T07:18:14.783140Z" 610 | } 611 | }, 612 | "outputs": [ 613 | { 614 | "data": { 615 | "text/plain": [ 616 | "140" 617 | ] 618 | }, 619 | "execution_count": 104, 620 | "metadata": {}, 621 | "output_type": "execute_result" 622 | } 623 | ], 624 | "source": [ 625 | "round(145,ndigits= -1)" 626 | ] 627 | }, 628 | { 629 | "cell_type": "markdown", 630 | "metadata": {}, 631 | "source": [ 632 | "## Flow Control" 633 | ] 634 | }, 635 | { 636 | "cell_type": "markdown", 637 | "metadata": { 638 | "ExecuteTime": { 639 | "end_time": "2020-01-27T07:40:55.727089Z", 640 | "start_time": "2020-01-27T07:40:55.723101Z" 641 | } 642 | }, 643 | "source": [ 644 | "" 645 | ] 646 | }, 647 | { 648 | "cell_type": "markdown", 649 | "metadata": {}, 650 | "source": [ 651 | "In a flow chart like above ,there is usually more than one way to go from the start to the end. THe same is true for lines of code in a computer program. Flowcharts represent these branching points with diamonds, while the other steps are represented with rectangles.The starting and ending steps are represented with rounded rectangles." 652 | ] 653 | }, 654 | { 655 | "cell_type": "markdown", 656 | "metadata": {}, 657 | "source": [ 658 | "### __Boolean Values__\n", 659 | "\n", 660 | "`True` and `False`\n", 661 | "\n", 662 | "### Comparison Operators\n", 663 | "\n", 664 | "| Operator | Meaning |\n", 665 | "|:--------:|:------------------------:|\n", 666 | "| == | Equal to |\n", 667 | "| != | Not equal to |\n", 668 | "| < | Less than |\n", 669 | "| > | Greater than |\n", 670 | "| <= | Less than or equal to |\n", 671 | "| >= | Greater than or equal to |" 672 | ] 673 | }, 674 | { 675 | "cell_type": "markdown", 676 | "metadata": {}, 677 | "source": [ 678 | "### Bollean Operators\n", 679 | "\n", 680 | "The three Boolean operators(and, or and not) are used to compare Boolean values. Like comparasion operators, they evaluate these expressions down to a Boolean value." 681 | ] 682 | }, 683 | { 684 | "cell_type": "markdown", 685 | "metadata": { 686 | "ExecuteTime": { 687 | "end_time": "2020-01-27T08:17:53.447795Z", 688 | "start_time": "2020-01-27T08:17:53.440814Z" 689 | } 690 | }, 691 | "source": [ 692 | "__Binary Boolean Operators__\n", 693 | "\n", 694 | "`and`,`or`\n", 695 | "\n", 696 | "__The not Operator__\n", 697 | "\n", 698 | "`not`" 699 | ] 700 | }, 701 | { 702 | "cell_type": "markdown", 703 | "metadata": {}, 704 | "source": [ 705 | "`not` can work on other expressions, to achieve some strange functionality. Here is a example to check whether a list is empty:" 706 | ] 707 | }, 708 | { 709 | "cell_type": "code", 710 | "execution_count": 134, 711 | "metadata": { 712 | "ExecuteTime": { 713 | "end_time": "2020-01-27T08:48:51.346492Z", 714 | "start_time": "2020-01-27T08:48:51.343525Z" 715 | } 716 | }, 717 | "outputs": [ 718 | { 719 | "name": "stdout", 720 | "output_type": "stream", 721 | "text": [ 722 | "1\n" 723 | ] 724 | } 725 | ], 726 | "source": [ 727 | "a = []\n", 728 | "if not a:\n", 729 | " print(1)" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 136, 735 | "metadata": { 736 | "ExecuteTime": { 737 | "end_time": "2020-01-27T08:49:33.481505Z", 738 | "start_time": "2020-01-27T08:49:33.478509Z" 739 | } 740 | }, 741 | "outputs": [ 742 | { 743 | "name": "stdout", 744 | "output_type": "stream", 745 | "text": [ 746 | "1\n" 747 | ] 748 | } 749 | ], 750 | "source": [ 751 | "# equals to write it explicitly\n", 752 | "if len(a) == 0:\n", 753 | " print(1)" 754 | ] 755 | }, 756 | { 757 | "cell_type": "markdown", 758 | "metadata": {}, 759 | "source": [ 760 | "### Mixing Bolean and comparison Opearators\n", 761 | "\n", 762 | "We should notice the order of operators, there is a [operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence). Something about [bitwise operators](https://www.geeksforgeeks.org/python-bitwise-operators/)" 763 | ] 764 | }, 765 | { 766 | "cell_type": "code", 767 | "execution_count": 141, 768 | "metadata": { 769 | "ExecuteTime": { 770 | "end_time": "2020-01-27T09:00:18.029063Z", 771 | "start_time": "2020-01-27T09:00:18.025073Z" 772 | } 773 | }, 774 | "outputs": [ 775 | { 776 | "data": { 777 | "text/plain": [ 778 | "True" 779 | ] 780 | }, 781 | "execution_count": 141, 782 | "metadata": {}, 783 | "output_type": "execute_result" 784 | } 785 | ], 786 | "source": [ 787 | "2+2==4 and not 2+2 ==5 and 2*2 ==2+2" 788 | ] 789 | }, 790 | { 791 | "cell_type": "markdown", 792 | "metadata": {}, 793 | "source": [ 794 | "### Elements of Flow Control" 795 | ] 796 | }, 797 | { 798 | "cell_type": "markdown", 799 | "metadata": {}, 800 | "source": [ 801 | "1. if Statements\n", 802 | "2. else Statements\n", 803 | "3. elif Statements\n", 804 | "4. while Loop Statements\n", 805 | "5. break Statements\n", 806 | "6. continue Statements\n", 807 | "7. for loops and the range()" 808 | ] 809 | }, 810 | { 811 | "cell_type": "code", 812 | "execution_count": 143, 813 | "metadata": { 814 | "ExecuteTime": { 815 | "end_time": "2020-01-27T11:14:11.648521Z", 816 | "start_time": "2020-01-27T11:13:34.633634Z" 817 | }, 818 | "collapsed": true 819 | }, 820 | "outputs": [ 821 | { 822 | "name": "stdout", 823 | "output_type": "stream", 824 | "text": [ 825 | "Who are you?\n", 826 | "Joe\n", 827 | "Hello, Joe. What is the password?(It is a fish.)\n", 828 | "a\n", 829 | "Who are you?\n", 830 | "a\n", 831 | "Who are you?\n", 832 | "a\n", 833 | "Who are you?\n", 834 | "a\n", 835 | "Who are you?\n", 836 | "Joe\n", 837 | "Hello, Joe. What is the password?(It is a fish.)\n", 838 | "swordfish\n", 839 | "Access granted\n" 840 | ] 841 | } 842 | ], 843 | "source": [ 844 | "while True:\n", 845 | " print('Who are you?')\n", 846 | " name = input()\n", 847 | " if name != 'Joe':\n", 848 | " continue \n", 849 | " print('Hello, Joe. What is the password?(It is a fish.)')\n", 850 | " password = input()\n", 851 | " if password == 'swordfish':\n", 852 | " break\n", 853 | "print('Access granted')" 854 | ] 855 | }, 856 | { 857 | "cell_type": "markdown", 858 | "metadata": {}, 859 | "source": [ 860 | "### truthy and falsey values\n", 861 | "\n", 862 | "There are some values in other data types(not Boolean type) that conditions will consider euivalent to `True` and `False`. When used in conditions, `0`, `0.0` and `''`(empty string) are considered `False`, while all other values are considered `True`. \n", 863 | "\n", 864 | "\n", 865 | "Using truthy and false values can make program more readable and time-saving.\n", 866 | "\n", 867 | "For example, looke at the following programe:" 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "execution_count": 146, 873 | "metadata": { 874 | "ExecuteTime": { 875 | "end_time": "2020-01-27T11:22:22.401479Z", 876 | "start_time": "2020-01-27T11:22:01.703188Z" 877 | } 878 | }, 879 | "outputs": [ 880 | { 881 | "name": "stdout", 882 | "output_type": "stream", 883 | "text": [ 884 | "Enter your name:\n", 885 | "1\n", 886 | "How many guests will you have?\n", 887 | "12\n", 888 | "Be sure to have enough room for all your guests.\n", 889 | "Done\n" 890 | ] 891 | } 892 | ], 893 | "source": [ 894 | "name = '' \n", 895 | "while not name:\n", 896 | " print('Enter your name:')\n", 897 | " name = input()\n", 898 | "print('How many guests will you have?')\n", 899 | "numOfGuests = int(input())\n", 900 | "if numOfGuests:\n", 901 | " print('Be sure to have enough room for all your guests.')\n", 902 | "print('Done')" 903 | ] 904 | }, 905 | { 906 | "cell_type": "markdown", 907 | "metadata": {}, 908 | "source": [ 909 | "### Equivalent of for and while loop\n", 910 | "\n", 911 | "We can write equivalent for loop and while loop." 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 149, 917 | "metadata": { 918 | "ExecuteTime": { 919 | "end_time": "2020-01-27T11:26:50.865544Z", 920 | "start_time": "2020-01-27T11:26:50.862519Z" 921 | } 922 | }, 923 | "outputs": [ 924 | { 925 | "name": "stdout", 926 | "output_type": "stream", 927 | "text": [ 928 | "0\n", 929 | "1\n", 930 | "2\n", 931 | "3\n", 932 | "4\n" 933 | ] 934 | } 935 | ], 936 | "source": [ 937 | "for i in range(5):\n", 938 | " print(i)" 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 150, 944 | "metadata": { 945 | "ExecuteTime": { 946 | "end_time": "2020-01-27T11:26:54.572258Z", 947 | "start_time": "2020-01-27T11:26:54.568269Z" 948 | } 949 | }, 950 | "outputs": [ 951 | { 952 | "name": "stdout", 953 | "output_type": "stream", 954 | "text": [ 955 | "0\n", 956 | "1\n", 957 | "2\n", 958 | "3\n", 959 | "4\n", 960 | "5\n" 961 | ] 962 | } 963 | ], 964 | "source": [ 965 | "i = 0\n", 966 | "while i <= 5:\n", 967 | " print(i)\n", 968 | " i += 1" 969 | ] 970 | }, 971 | { 972 | "cell_type": "markdown", 973 | "metadata": {}, 974 | "source": [ 975 | "### Import Modules\n", 976 | "\n", 977 | "All Python programs can call a basic set of functions called _built-in functions_, cincluding the print(), input(), and len() functions you've seen before. Python also comes with a set of modules called the _standard library_. Each module is a Python program that contains a related group of functions that can be embedded in your programs. For example, the math module has mathematics-related functions, the random model has random number-related functions, and so on.\n", 978 | "\n", 979 | "Befor you can use the functions in a module, you must import the module with an `import` statement. In code, an `import` statement consists of the following:\n", 980 | "- The `import` keyword\n", 981 | "- The name of the module\n", 982 | "- Optionally, more module namse as long as they are separated by commas" 983 | ] 984 | }, 985 | { 986 | "cell_type": "code", 987 | "execution_count": 169, 988 | "metadata": { 989 | "ExecuteTime": { 990 | "end_time": "2020-01-27T11:40:03.034805Z", 991 | "start_time": "2020-01-27T11:40:03.030814Z" 992 | } 993 | }, 994 | "outputs": [ 995 | { 996 | "name": "stdout", 997 | "output_type": "stream", 998 | "text": [ 999 | "3\n", 1000 | "10\n", 1001 | "2\n", 1002 | "5\n", 1003 | "2\n" 1004 | ] 1005 | } 1006 | ], 1007 | "source": [ 1008 | "import random\n", 1009 | "random.seed(1)\n", 1010 | "for i in range(5):\n", 1011 | " print(random.randint(1,10))" 1012 | ] 1013 | }, 1014 | { 1015 | "cell_type": "code", 1016 | "execution_count": 170, 1017 | "metadata": { 1018 | "ExecuteTime": { 1019 | "end_time": "2020-01-27T11:40:11.914602Z", 1020 | "start_time": "2020-01-27T11:40:11.911611Z" 1021 | } 1022 | }, 1023 | "outputs": [], 1024 | "source": [ 1025 | "import random,sys,os,math" 1026 | ] 1027 | }, 1028 | { 1029 | "cell_type": "markdown", 1030 | "metadata": {}, 1031 | "source": [ 1032 | "__from import Statements__" 1033 | ] 1034 | }, 1035 | { 1036 | "cell_type": "code", 1037 | "execution_count": 172, 1038 | "metadata": { 1039 | "ExecuteTime": { 1040 | "end_time": "2020-01-27T11:40:56.996023Z", 1041 | "start_time": "2020-01-27T11:40:56.991037Z" 1042 | } 1043 | }, 1044 | "outputs": [ 1045 | { 1046 | "data": { 1047 | "text/plain": [ 1048 | "2" 1049 | ] 1050 | }, 1051 | "execution_count": 172, 1052 | "metadata": {}, 1053 | "output_type": "execute_result" 1054 | } 1055 | ], 1056 | "source": [ 1057 | "from random import *\n", 1058 | "randint(1,2)" 1059 | ] 1060 | }, 1061 | { 1062 | "cell_type": "markdown", 1063 | "metadata": {}, 1064 | "source": [ 1065 | "### Ending a Program Early with `sys.exit()`" 1066 | ] 1067 | }, 1068 | { 1069 | "cell_type": "markdown", 1070 | "metadata": {}, 1071 | "source": [ 1072 | "THe last flow control concept to cover is how to terminate the program. This always happens if the program execution reaches the bottom of the instructions. However, you can cause the program to terminate, or exit, by calling the `sys.exit()` function. " 1073 | ] 1074 | }, 1075 | { 1076 | "cell_type": "code", 1077 | "execution_count": 177, 1078 | "metadata": { 1079 | "ExecuteTime": { 1080 | "end_time": "2020-01-27T11:55:14.488235Z", 1081 | "start_time": "2020-01-27T11:55:10.103574Z" 1082 | } 1083 | }, 1084 | "outputs": [ 1085 | { 1086 | "name": "stdout", 1087 | "output_type": "stream", 1088 | "text": [ 1089 | "Type exit to exit.\n", 1090 | "1\n", 1091 | "your typed 1.\n", 1092 | "Type exit to exit.\n", 1093 | "2\n", 1094 | "your typed 2.\n", 1095 | "Type exit to exit.\n", 1096 | "3\n", 1097 | "your typed 3.\n", 1098 | "Type exit to exit.\n", 1099 | "4\n", 1100 | "your typed 4.\n", 1101 | "Type exit to exit.\n", 1102 | "5\n", 1103 | "your typed 5.\n", 1104 | "Type exit to exit.\n", 1105 | "exit\n" 1106 | ] 1107 | }, 1108 | { 1109 | "ename": "SystemExit", 1110 | "evalue": "[1234]", 1111 | "output_type": "error", 1112 | "traceback": [ 1113 | "An exception has occurred, use %tb to see the full traceback.\n", 1114 | "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m [1234]\n" 1115 | ] 1116 | } 1117 | ], 1118 | "source": [ 1119 | "import sys\n", 1120 | "\n", 1121 | "while True:\n", 1122 | " print('Type exit to exit.')\n", 1123 | " response = input()\n", 1124 | " if response == 'exit':\n", 1125 | " sys.exit([1234]) # can take a status, always raise a SystemExit Error\n", 1126 | " print('your typed '+ response + '.')" 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "markdown", 1131 | "metadata": { 1132 | "ExecuteTime": { 1133 | "end_time": "2020-01-27T12:19:53.234492Z", 1134 | "start_time": "2020-01-27T12:19:53.230502Z" 1135 | } 1136 | }, 1137 | "source": [ 1138 | "### Practice Questions\n", 1139 | "\n", 1140 | "- 6. What is the difference between the equal to operator and the assignment opearator?\n", 1141 | "> The == operator(equal to) askes whether two values are the same as each other. The = operator(assignment operator) puts the value of right into the variable on the left.To help remember which is which, notice that the == operator (equal to)\n", 1142 | "consists of two characters, just like the != operator (not equal to) consists of\n", 1143 | "two characters.\n", 1144 | "\n", 1145 | "- 7. Explain what a conditon is and where you would use one.\n", 1146 | "> Conditon is an expression to be reduced to a Boolean value, which helps us choose what to do in a flow control.\n", 1147 | "\n" 1148 | ] 1149 | }, 1150 | { 1151 | "cell_type": "markdown", 1152 | "metadata": {}, 1153 | "source": [ 1154 | "## Functions" 1155 | ] 1156 | }, 1157 | { 1158 | "cell_type": "code", 1159 | "execution_count": 13, 1160 | "metadata": { 1161 | "ExecuteTime": { 1162 | "end_time": "2020-01-30T09:15:03.951765Z", 1163 | "start_time": "2020-01-30T09:15:03.943788Z" 1164 | } 1165 | }, 1166 | "outputs": [ 1167 | { 1168 | "ename": "UnboundLocalError", 1169 | "evalue": "local variable 'eggs' referenced before assignment", 1170 | "output_type": "error", 1171 | "traceback": [ 1172 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1173 | "\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", 1174 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0meggs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'global'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mspam\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1175 | "\u001b[1;32m\u001b[0m in \u001b[0;36mspam\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mspam\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0meggs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0meggs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'local'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0meggs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'global'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 1176 | "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'eggs' referenced before assignment" 1177 | ] 1178 | } 1179 | ], 1180 | "source": [ 1181 | "def spam():\n", 1182 | " print(eggs)\n", 1183 | " eggs = 'local'\n", 1184 | "\n", 1185 | "eggs = 'global'\n", 1186 | "spam()" 1187 | ] 1188 | }, 1189 | { 1190 | "cell_type": "markdown", 1191 | "metadata": {}, 1192 | "source": [ 1193 | "## Lists" 1194 | ] 1195 | }, 1196 | { 1197 | "cell_type": "markdown", 1198 | "metadata": {}, 1199 | "source": [ 1200 | "## Dictionaries and structuring data" 1201 | ] 1202 | }, 1203 | { 1204 | "cell_type": "code", 1205 | "execution_count": 5, 1206 | "metadata": { 1207 | "ExecuteTime": { 1208 | "end_time": "2020-01-31T08:23:45.461041Z", 1209 | "start_time": "2020-01-31T08:23:45.458049Z" 1210 | } 1211 | }, 1212 | "outputs": [], 1213 | "source": [ 1214 | "eggs = [1,2,3]\n", 1215 | "eggs2 = eggs.copy()" 1216 | ] 1217 | }, 1218 | { 1219 | "cell_type": "code", 1220 | "execution_count": 6, 1221 | "metadata": { 1222 | "ExecuteTime": { 1223 | "end_time": "2020-01-31T08:23:46.093312Z", 1224 | "start_time": "2020-01-31T08:23:46.090310Z" 1225 | } 1226 | }, 1227 | "outputs": [], 1228 | "source": [ 1229 | "eggs2[0] = '1'" 1230 | ] 1231 | }, 1232 | { 1233 | "cell_type": "code", 1234 | "execution_count": 7, 1235 | "metadata": { 1236 | "ExecuteTime": { 1237 | "end_time": "2020-01-31T08:23:46.653401Z", 1238 | "start_time": "2020-01-31T08:23:46.650406Z" 1239 | } 1240 | }, 1241 | "outputs": [ 1242 | { 1243 | "data": { 1244 | "text/plain": [ 1245 | "(['1', 2, 3], [1, 2, 3])" 1246 | ] 1247 | }, 1248 | "execution_count": 7, 1249 | "metadata": {}, 1250 | "output_type": "execute_result" 1251 | } 1252 | ], 1253 | "source": [ 1254 | "eggs2,eggs" 1255 | ] 1256 | }, 1257 | { 1258 | "cell_type": "code", 1259 | "execution_count": 9, 1260 | "metadata": { 1261 | "ExecuteTime": { 1262 | "end_time": "2020-01-31T08:38:48.990730Z", 1263 | "start_time": "2020-01-31T08:38:48.986764Z" 1264 | } 1265 | }, 1266 | "outputs": [ 1267 | { 1268 | "data": { 1269 | "text/plain": [ 1270 | "(2160054314056, 2160055178632)" 1271 | ] 1272 | }, 1273 | "execution_count": 9, 1274 | "metadata": {}, 1275 | "output_type": "execute_result" 1276 | } 1277 | ], 1278 | "source": [ 1279 | "id(eggs),id(eggs2)" 1280 | ] 1281 | }, 1282 | { 1283 | "cell_type": "code", 1284 | "execution_count": 27, 1285 | "metadata": { 1286 | "ExecuteTime": { 1287 | "end_time": "2020-01-31T09:08:21.830987Z", 1288 | "start_time": "2020-01-31T09:08:21.825998Z" 1289 | } 1290 | }, 1291 | "outputs": [ 1292 | { 1293 | "data": { 1294 | "text/plain": [ 1295 | "([1, 2, [3, 'missing', ['inner_missing', [10, 2, 3]]]],\n", 1296 | " [1, 2, [3, 2, ['inner1', [1, 2, 3]]]])" 1297 | ] 1298 | }, 1299 | "execution_count": 27, 1300 | "metadata": {}, 1301 | "output_type": "execute_result" 1302 | } 1303 | ], 1304 | "source": [ 1305 | "import copy\n", 1306 | "m_3 = [1,2,3]\n", 1307 | "m_2 = ['inner1',m_3]\n", 1308 | "m_1 = [3,2,m_2]\n", 1309 | "eggs = [1,2,m_1]\n", 1310 | "eggs2 = copy.deepcopy(eggs)\n", 1311 | "m_3[0] = 10\n", 1312 | "m_2[0] = 'inner_missing'\n", 1313 | "m_1[1] = 'missing'\n", 1314 | "eggs,eggs2" 1315 | ] 1316 | }, 1317 | { 1318 | "cell_type": "code", 1319 | "execution_count": 23, 1320 | "metadata": { 1321 | "ExecuteTime": { 1322 | "end_time": "2020-01-31T08:44:24.517768Z", 1323 | "start_time": "2020-01-31T08:44:24.514742Z" 1324 | } 1325 | }, 1326 | "outputs": [ 1327 | { 1328 | "data": { 1329 | "text/plain": [ 1330 | "['inner_missing', 'inner2']" 1331 | ] 1332 | }, 1333 | "execution_count": 23, 1334 | "metadata": {}, 1335 | "output_type": "execute_result" 1336 | } 1337 | ], 1338 | "source": [ 1339 | "m_2" 1340 | ] 1341 | }, 1342 | { 1343 | "cell_type": "code", 1344 | "execution_count": 29, 1345 | "metadata": { 1346 | "ExecuteTime": { 1347 | "end_time": "2020-01-31T09:14:33.373932Z", 1348 | "start_time": "2020-01-31T09:14:33.369915Z" 1349 | } 1350 | }, 1351 | "outputs": [ 1352 | { 1353 | "data": { 1354 | "text/plain": [ 1355 | "array([3, 8, 5, 5, 4, 7, 8, 2, 5, 1, 8, 6, 7, 1, 4, 1, 5, 6, 7, 3, 2, 9,\n", 1356 | " 7, 1, 1, 8, 5, 4, 1, 9, 5, 0, 1, 6, 3, 5, 2, 1, 5, 5, 9, 9, 7, 9,\n", 1357 | " 7, 5, 7, 6, 9, 4, 8, 6, 5, 0, 2, 5, 6, 8, 8, 1, 7, 3, 3, 6, 0, 8,\n", 1358 | " 6, 9, 4, 4, 8, 1, 9, 7, 2, 5, 6, 6, 8, 8, 3, 4, 7, 0, 0, 7, 2, 8,\n", 1359 | " 8, 3, 0, 9, 3, 5, 9, 5, 6, 6, 9, 5])" 1360 | ] 1361 | }, 1362 | "execution_count": 29, 1363 | "metadata": {}, 1364 | "output_type": "execute_result" 1365 | } 1366 | ], 1367 | "source": [ 1368 | "list_random = np.random.randint(0,10,100)\n", 1369 | "list_random" 1370 | ] 1371 | }, 1372 | { 1373 | "cell_type": "code", 1374 | "execution_count": 58, 1375 | "metadata": { 1376 | "ExecuteTime": { 1377 | "end_time": "2020-01-31T09:20:24.518305Z", 1378 | "start_time": "2020-01-31T09:20:22.953489Z" 1379 | } 1380 | }, 1381 | "outputs": [ 1382 | { 1383 | "name": "stdout", 1384 | "output_type": "stream", 1385 | "text": [ 1386 | "19.1 µs ± 708 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" 1387 | ] 1388 | } 1389 | ], 1390 | "source": [ 1391 | "%%timeit\n", 1392 | "[i for i, element in enumerate(list_random) if element == 1]" 1393 | ] 1394 | }, 1395 | { 1396 | "cell_type": "code", 1397 | "execution_count": 60, 1398 | "metadata": { 1399 | "ExecuteTime": { 1400 | "end_time": "2020-01-31T09:20:41.771992Z", 1401 | "start_time": "2020-01-31T09:20:40.144317Z" 1402 | } 1403 | }, 1404 | "outputs": [ 1405 | { 1406 | "name": "stdout", 1407 | "output_type": "stream", 1408 | "text": [ 1409 | "20 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" 1410 | ] 1411 | } 1412 | ], 1413 | "source": [ 1414 | "%%timeit\n", 1415 | "g = (i for i, element in enumerate(list_random) if element == 1)\n", 1416 | "list(g)" 1417 | ] 1418 | }, 1419 | { 1420 | "cell_type": "code", 1421 | "execution_count": 189, 1422 | "metadata": { 1423 | "ExecuteTime": { 1424 | "end_time": "2020-01-31T11:34:10.553053Z", 1425 | "start_time": "2020-01-31T11:34:10.550034Z" 1426 | } 1427 | }, 1428 | "outputs": [], 1429 | "source": [ 1430 | "list_random = np.random.randint(0,10000,10000)" 1431 | ] 1432 | }, 1433 | { 1434 | "cell_type": "code", 1435 | "execution_count": 190, 1436 | "metadata": { 1437 | "ExecuteTime": { 1438 | "end_time": "2020-01-31T11:34:26.276708Z", 1439 | "start_time": "2020-01-31T11:34:11.190907Z" 1440 | } 1441 | }, 1442 | "outputs": [ 1443 | { 1444 | "name": "stdout", 1445 | "output_type": "stream", 1446 | "text": [ 1447 | "1.86 ms ± 1.53 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" 1448 | ] 1449 | } 1450 | ], 1451 | "source": [ 1452 | "%%timeit\n", 1453 | "[i for i, element in enumerate(list_random) if element == 1]" 1454 | ] 1455 | }, 1456 | { 1457 | "cell_type": "code", 1458 | "execution_count": 191, 1459 | "metadata": { 1460 | "ExecuteTime": { 1461 | "end_time": "2020-01-31T11:34:42.376683Z", 1462 | "start_time": "2020-01-31T11:34:26.286682Z" 1463 | } 1464 | }, 1465 | "outputs": [ 1466 | { 1467 | "name": "stdout", 1468 | "output_type": "stream", 1469 | "text": [ 1470 | "1.98 ms ± 3.24 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" 1471 | ] 1472 | } 1473 | ], 1474 | "source": [ 1475 | "%%timeit\n", 1476 | "g = (i for i, element in enumerate(list_random) if element == 1)\n", 1477 | "list(g)" 1478 | ] 1479 | }, 1480 | { 1481 | "cell_type": "code", 1482 | "execution_count": 192, 1483 | "metadata": { 1484 | "ExecuteTime": { 1485 | "end_time": "2020-01-31T11:34:54.537041Z", 1486 | "start_time": "2020-01-31T11:34:42.385632Z" 1487 | } 1488 | }, 1489 | "outputs": [ 1490 | { 1491 | "name": "stdout", 1492 | "output_type": "stream", 1493 | "text": [ 1494 | "1.5 ms ± 7.29 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" 1495 | ] 1496 | } 1497 | ], 1498 | "source": [ 1499 | "%%timeit\n", 1500 | "get_indices(list(list_random),1)" 1501 | ] 1502 | }, 1503 | { 1504 | "cell_type": "code", 1505 | "execution_count": 176, 1506 | "metadata": { 1507 | "ExecuteTime": { 1508 | "end_time": "2020-01-31T11:12:25.237476Z", 1509 | "start_time": "2020-01-31T11:12:25.232514Z" 1510 | } 1511 | }, 1512 | "outputs": [], 1513 | "source": [ 1514 | "# https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list\n", 1515 | "def get_indices(x: list, value: int) -> list:\n", 1516 | " indices = list()\n", 1517 | " i = 0\n", 1518 | " while True:\n", 1519 | " try:\n", 1520 | " # find an occurrence of value and update i to that index\n", 1521 | " i = x.index(value, i)\n", 1522 | " # add i to the list\n", 1523 | " indices.append(i)\n", 1524 | " # advance i by 1\n", 1525 | " i += 1\n", 1526 | " except ValueError as e:\n", 1527 | " break\n", 1528 | " return indices" 1529 | ] 1530 | }, 1531 | { 1532 | "cell_type": "markdown", 1533 | "metadata": {}, 1534 | "source": [ 1535 | "## Manipulating strings" 1536 | ] 1537 | }, 1538 | { 1539 | "cell_type": "code", 1540 | "execution_count": 11, 1541 | "metadata": { 1542 | "ExecuteTime": { 1543 | "end_time": "2020-01-31T15:13:10.503226Z", 1544 | "start_time": "2020-01-31T15:13:10.499260Z" 1545 | } 1546 | }, 1547 | "outputs": [], 1548 | "source": [ 1549 | "def comma_code(x):\n", 1550 | " x[-1] = 'and ' + str(x[-1])\n", 1551 | " res = ''\n", 1552 | " for i in x:\n", 1553 | " res += str(i) + ', '\n", 1554 | "\n", 1555 | " return res[:-2]" 1556 | ] 1557 | }, 1558 | { 1559 | "cell_type": "code", 1560 | "execution_count": 14, 1561 | "metadata": { 1562 | "ExecuteTime": { 1563 | "end_time": "2020-01-31T15:22:42.806339Z", 1564 | "start_time": "2020-01-31T15:22:42.801351Z" 1565 | } 1566 | }, 1567 | "outputs": [], 1568 | "source": [ 1569 | "grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'],\n", 1570 | " ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'],\n", 1571 | " ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'],\n", 1572 | " ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'],\n", 1573 | " ['.', '.', '.', '.', '.', '.']]" 1574 | ] 1575 | }, 1576 | { 1577 | "cell_type": "code", 1578 | "execution_count": 28, 1579 | "metadata": { 1580 | "ExecuteTime": { 1581 | "end_time": "2020-01-31T15:27:10.462565Z", 1582 | "start_time": "2020-01-31T15:27:10.459565Z" 1583 | } 1584 | }, 1585 | "outputs": [], 1586 | "source": [ 1587 | "nrow = len(grid)\n", 1588 | "ncol = len(grid[0])" 1589 | ] 1590 | }, 1591 | { 1592 | "cell_type": "code", 1593 | "execution_count": 29, 1594 | "metadata": { 1595 | "ExecuteTime": { 1596 | "end_time": "2020-01-31T15:27:11.173841Z", 1597 | "start_time": "2020-01-31T15:27:11.170824Z" 1598 | } 1599 | }, 1600 | "outputs": [ 1601 | { 1602 | "name": "stdout", 1603 | "output_type": "stream", 1604 | "text": [ 1605 | "..OO.OO..\n", 1606 | ".OOOOOOO.\n", 1607 | ".OOOOOOO.\n", 1608 | "..OOOOO..\n", 1609 | "...OOO...\n", 1610 | "....O....\n" 1611 | ] 1612 | } 1613 | ], 1614 | "source": [ 1615 | "for i in range(ncol):\n", 1616 | " for j in range(nrow):\n", 1617 | " print(grid[8-j][i],sep = '',end = '')\n", 1618 | " print('\\n',end = '')" 1619 | ] 1620 | }, 1621 | { 1622 | "cell_type": "code", 1623 | "execution_count": null, 1624 | "metadata": {}, 1625 | "outputs": [], 1626 | "source": [] 1627 | }, 1628 | { 1629 | "cell_type": "code", 1630 | "execution_count": null, 1631 | "metadata": {}, 1632 | "outputs": [], 1633 | "source": [] 1634 | }, 1635 | { 1636 | "cell_type": "code", 1637 | "execution_count": null, 1638 | "metadata": {}, 1639 | "outputs": [], 1640 | "source": [] 1641 | }, 1642 | { 1643 | "cell_type": "code", 1644 | "execution_count": null, 1645 | "metadata": {}, 1646 | "outputs": [], 1647 | "source": [] 1648 | }, 1649 | { 1650 | "cell_type": "code", 1651 | "execution_count": null, 1652 | "metadata": {}, 1653 | "outputs": [], 1654 | "source": [] 1655 | }, 1656 | { 1657 | "cell_type": "code", 1658 | "execution_count": null, 1659 | "metadata": {}, 1660 | "outputs": [], 1661 | "source": [] 1662 | } 1663 | ], 1664 | "metadata": { 1665 | "kernelspec": { 1666 | "display_name": "Python 3", 1667 | "language": "python", 1668 | "name": "python3" 1669 | }, 1670 | "language_info": { 1671 | "codemirror_mode": { 1672 | "name": "ipython", 1673 | "version": 3 1674 | }, 1675 | "file_extension": ".py", 1676 | "mimetype": "text/x-python", 1677 | "name": "python", 1678 | "nbconvert_exporter": "python", 1679 | "pygments_lexer": "ipython3", 1680 | "version": "3.7.1" 1681 | }, 1682 | "toc": { 1683 | "base_numbering": 1, 1684 | "nav_menu": {}, 1685 | "number_sections": true, 1686 | "sideBar": true, 1687 | "skip_h1_title": false, 1688 | "title_cell": "Table of Contents", 1689 | "title_sidebar": "Contents", 1690 | "toc_cell": false, 1691 | "toc_position": {}, 1692 | "toc_section_display": true, 1693 | "toc_window_display": true 1694 | }, 1695 | "varInspector": { 1696 | "cols": { 1697 | "lenName": 16, 1698 | "lenType": 16, 1699 | "lenVar": 40 1700 | }, 1701 | "kernels_config": { 1702 | "python": { 1703 | "delete_cmd_postfix": "", 1704 | "delete_cmd_prefix": "del ", 1705 | "library": "var_list.py", 1706 | "varRefreshCmd": "print(var_dic_list())" 1707 | }, 1708 | "r": { 1709 | "delete_cmd_postfix": ") ", 1710 | "delete_cmd_prefix": "rm(", 1711 | "library": "var_list.r", 1712 | "varRefreshCmd": "cat(var_dic_list()) " 1713 | } 1714 | }, 1715 | "types_to_exclude": [ 1716 | "module", 1717 | "function", 1718 | "builtin_function_or_method", 1719 | "instance", 1720 | "_Feature" 1721 | ], 1722 | "window_display": false 1723 | } 1724 | }, 1725 | "nbformat": 4, 1726 | "nbformat_minor": 2 1727 | } 1728 | --------------------------------------------------------------------------------