├── .ipynb_checkpoints ├── Lesson 10 -- 2020 May 22-checkpoint.ipynb ├── Lesson 11 -- 2020 June 5-checkpoint.ipynb ├── Lesson 12 -- 2020 June 12-checkpoint.ipynb ├── Lesson 13 -- 2020 June 19-checkpoint.ipynb ├── Lesson 14 -- 2020 June 26-checkpoint.ipynb ├── Lesson 15 -- 2020 July 3-checkpoint.ipynb ├── Lesson 5 -- 2020 April 17-checkpoint.ipynb ├── Lesson 6 -- 2020 Apr 24-checkpoint.ipynb ├── Lesson 7 -- 2020 May 1-checkpoint.ipynb ├── Lesson 9 -- 2020 May 15-checkpoint.ipynb ├── Non-programmers 2020 April 10-checkpoint.ipynb ├── Non-programmers 2020 April 3-checkpoint.ipynb ├── Non-programmers 2020 March 20-checkpoint.ipynb ├── Non-programmers 2020 March 27-checkpoint.ipynb ├── Non-programmers, 2020 May 8-checkpoint.ipynb └── Untitled-checkpoint.ipynb ├── Lesson 10 -- 2020 May 22.ipynb ├── Lesson 11 -- 2020 June 5.ipynb ├── Lesson 12 -- 2020 June 12.ipynb ├── Lesson 13 -- 2020 June 19.ipynb ├── Lesson 14 -- 2020 June 26.ipynb ├── Lesson 15 -- 2020 July 3.ipynb ├── Lesson 5 -- 2020 April 17.ipynb ├── Lesson 6 -- 2020 Apr 24.ipynb ├── Lesson 7 -- 2020 May 1.ipynb ├── Lesson 9 -- 2020 May 15.ipynb ├── Non-programmers 2020 April 10.ipynb ├── Non-programmers 2020 April 3.ipynb ├── Non-programmers 2020 March 20.ipynb ├── Non-programmers 2020 March 27.ipynb ├── Non-programmers, 2020 May 8.ipynb ├── README.md ├── Untitled.ipynb ├── alice-in-wonderland.txt ├── dict-counts.txt ├── even-nums.txt ├── even_numbers.txt ├── ip-address-report.txt ├── linux-etc-passwd.txt ├── mini-access-log.txt ├── movies.dat ├── mydict.csv ├── myfile.txt ├── myoutput.txt ├── nums.txt ├── odd-nums.txt ├── odd_number.txt ├── sales-report.csv ├── selected-nums.txt ├── shoe-data.txt ├── taxi.csv ├── testprog.py ├── vowel_counts.txt └── wcfile.txt /.ipynb_checkpoints/Lesson 11 -- 2020 June 5-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Lesson 12 -- 2020 June 12-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Lesson 13 -- 2020 June 19-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Lesson 14 -- 2020 June 26-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Lesson 15 -- 2020 July 3-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 4 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Lesson 5 -- 2020 April 17-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Lesson 6 -- 2020 Apr 24-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Lesson 7 -- 2020 May 1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Lesson 9 -- 2020 May 15-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | ". We have previously written a program that takes a single word from the user and translates it into Pig Latin. I now want you to take an entire sentence from the user (all lower-case letters, and without punctuation) and print the translation of each word into Pig Latin. So if the user enters this is a test, the output will be histay isway away esttay." 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 5, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "Enter a sentence: this is a test\n", 20 | "histay isway away esttay\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "sentence = input(\"Enter a sentence: \").strip()\n", 26 | "all_words = sentence.split()\n", 27 | "\n", 28 | "output = []\n", 29 | "for word in all_words:\n", 30 | " if word[0] in 'aeiou':\n", 31 | " output.append(f'{word}way')\n", 32 | " else:\n", 33 | " output.append(f'{word[1:]}{word[0]}ay')\n", 34 | "print(' '.join(output))" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "sentence = input(\"Enter a sentence: \").strip()\n", 44 | "\n", 45 | "output = []\n", 46 | "for word in sentence.split():\n", 47 | " if word[0] in 'aeiou':\n", 48 | " output.append(f'{word}way')\n", 49 | " else:\n", 50 | " output.append(f'{word[1:]}{word[0]}ay')\n", 51 | "print(' '.join(output))" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 6, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "Enter a sentence: this is a test\n", 64 | "histay isway away esttay\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "all_words = input(\"Enter a sentence: \").split()\n", 70 | "\n", 71 | "output = []\n", 72 | "for word in all_words:\n", 73 | " if word[0] in 'aeiou':\n", 74 | " output.append(f'{word}way')\n", 75 | " else:\n", 76 | " output.append(f'{word[1:]}{word[0]}ay')\n", 77 | "print(' '.join(output))" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "output = []\n", 87 | "for word in input(\"Enter a sentence: \").split():\n", 88 | " if word[0] in 'aeiou':\n", 89 | " output.append(f'{word}way')\n", 90 | " else:\n", 91 | " output.append(f'{word[1:]}{word[0]}ay')\n", 92 | "print(' '.join(output))" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | ". Ask the user to enter several words separated by whitespace. Print those words separated by * characters. So if the user enters this is a test, the program should print this*is*a*test. You should not use str.replace, but rather str.split and str.join." 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 9, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "Enter a sentence: this is a test\n", 112 | "this*is*a*test\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "s = input(\"Enter a sentence: \").strip()\n", 118 | "\n", 119 | "words = s.split()\n", 120 | "print('*'.join(words))" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 11, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "Enter a sentence: this is a better test\n", 133 | "this*is*a*better*test\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "s = input(\"Enter a sentence: \").strip()\n", 139 | "\n", 140 | "print('*'.join(s.split()))" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "# strip -- removes whitespace from the edges of a string, and returns a new string\n", 150 | "# split -- returns a list of strings, based on a string\n" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | ". Ask the user to enter numbers, separated by spaces, on a single line. Append each number to one of two lists, evens or odds. Then print the even numbers, one per line (i.e., \\n characters between each) and then the odd numbers, one per line. This will require not just str.join and str.split, but also some moving between int and str, so you have been warned!" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 15, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "Enter numbers, separated by spaces: 10 11 12 abcd 1a 15 16 17\n", 170 | "abcd is not numeric; ignoring\n", 171 | "1a is not numeric; ignoring\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "evens = []\n", 177 | "odds = []\n", 178 | "\n", 179 | "numbers = input(\"Enter numbers, separated by spaces: \").split()\n", 180 | "\n", 181 | "for one_number in numbers:\n", 182 | " if not one_number.isdigit():\n", 183 | " print(f'{one_number} is not numeric; ignoring')\n", 184 | " continue\n", 185 | " \n", 186 | " one_number = int(one_number)\n", 187 | " \n", 188 | " if one_number % 2 == 0:\n", 189 | " evens.append(str(one_number))\n", 190 | " else:\n", 191 | " odds.append(str(one_number))\n", 192 | " \n" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 16, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "['10', '12', '16']" 204 | ] 205 | }, 206 | "execution_count": 16, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "evens" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 17, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "data": { 222 | "text/plain": [ 223 | "['11', '15', '17']" 224 | ] 225 | }, 226 | "execution_count": 17, 227 | "metadata": {}, 228 | "output_type": "execute_result" 229 | } 230 | ], 231 | "source": [ 232 | "odds" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 18, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "10\n", 245 | "12\n", 246 | "16\n" 247 | ] 248 | } 249 | ], 250 | "source": [ 251 | "print('\\n'.join(evens))" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 19, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "11\n", 264 | "15\n", 265 | "17\n" 266 | ] 267 | } 268 | ], 269 | "source": [ 270 | "print('\\n'.join(odds))" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 20, 276 | "metadata": {}, 277 | "outputs": [ 278 | { 279 | "data": { 280 | "text/plain": [ 281 | "'abcd'" 282 | ] 283 | }, 284 | "execution_count": 20, 285 | "metadata": {}, 286 | "output_type": "execute_result" 287 | } 288 | ], 289 | "source": [ 290 | "mylist = ['abcd', 'efgh', 'ijkl']\n", 291 | "\n", 292 | "mylist[0]" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 21, 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "data": { 302 | "text/plain": [ 303 | "'efgh'" 304 | ] 305 | }, 306 | "execution_count": 21, 307 | "metadata": {}, 308 | "output_type": "execute_result" 309 | } 310 | ], 311 | "source": [ 312 | "mylist[1]" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 22, 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "data": { 322 | "text/plain": [ 323 | "'ijkl'" 324 | ] 325 | }, 326 | "execution_count": 22, 327 | "metadata": {}, 328 | "output_type": "execute_result" 329 | } 330 | ], 331 | "source": [ 332 | "mylist[2]" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 23, 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | "person = ['Reuven', 'Lerner', 46]" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 24, 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "data": { 351 | "text/plain": [ 352 | "'Reuven'" 353 | ] 354 | }, 355 | "execution_count": 24, 356 | "metadata": {}, 357 | "output_type": "execute_result" 358 | } 359 | ], 360 | "source": [ 361 | "person[0]" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 25, 367 | "metadata": {}, 368 | "outputs": [ 369 | { 370 | "data": { 371 | "text/plain": [ 372 | "'Lerner'" 373 | ] 374 | }, 375 | "execution_count": 25, 376 | "metadata": {}, 377 | "output_type": "execute_result" 378 | } 379 | ], 380 | "source": [ 381 | "person[1]" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": 26, 387 | "metadata": {}, 388 | "outputs": [ 389 | { 390 | "data": { 391 | "text/plain": [ 392 | "46" 393 | ] 394 | }, 395 | "execution_count": 26, 396 | "metadata": {}, 397 | "output_type": "execute_result" 398 | } 399 | ], 400 | "source": [ 401 | "person[2]" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": 27, 407 | "metadata": {}, 408 | "outputs": [], 409 | "source": [ 410 | "# dictionaries: like lists, but we can use either ints or strings as the keys/indexes\n", 411 | "\n", 412 | "# key-value pairs\n", 413 | "# name-value pairs\n", 414 | "\n", 415 | "d = {'first':'Reuven', 'last':'Lerner', 'shoesize':46}" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 28, 421 | "metadata": {}, 422 | "outputs": [ 423 | { 424 | "data": { 425 | "text/plain": [ 426 | "{'first': 'Reuven', 'last': 'Lerner', 'shoesize': 46}" 427 | ] 428 | }, 429 | "execution_count": 28, 430 | "metadata": {}, 431 | "output_type": "execute_result" 432 | } 433 | ], 434 | "source": [ 435 | "d" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 29, 441 | "metadata": {}, 442 | "outputs": [ 443 | { 444 | "data": { 445 | "text/plain": [ 446 | "'Reuven'" 447 | ] 448 | }, 449 | "execution_count": 29, 450 | "metadata": {}, 451 | "output_type": "execute_result" 452 | } 453 | ], 454 | "source": [ 455 | "d['first']" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 30, 461 | "metadata": {}, 462 | "outputs": [ 463 | { 464 | "data": { 465 | "text/plain": [ 466 | "'Lerner'" 467 | ] 468 | }, 469 | "execution_count": 30, 470 | "metadata": {}, 471 | "output_type": "execute_result" 472 | } 473 | ], 474 | "source": [ 475 | "d['last']" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 31, 481 | "metadata": {}, 482 | "outputs": [ 483 | { 484 | "data": { 485 | "text/plain": [ 486 | "46" 487 | ] 488 | }, 489 | "execution_count": 31, 490 | "metadata": {}, 491 | "output_type": "execute_result" 492 | } 493 | ], 494 | "source": [ 495 | "d['shoesize']" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 32, 501 | "metadata": {}, 502 | "outputs": [ 503 | { 504 | "ename": "KeyError", 505 | "evalue": "'email'", 506 | "output_type": "error", 507 | "traceback": [ 508 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 509 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 510 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'email'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 511 | "\u001b[0;31mKeyError\u001b[0m: 'email'" 512 | ] 513 | } 514 | ], 515 | "source": [ 516 | "d['email']" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": 33, 522 | "metadata": {}, 523 | "outputs": [ 524 | { 525 | "ename": "IndexError", 526 | "evalue": "list index out of range", 527 | "output_type": "error", 528 | "traceback": [ 529 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 530 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 531 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmylist\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m999\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 532 | "\u001b[0;31mIndexError\u001b[0m: list index out of range" 533 | ] 534 | } 535 | ], 536 | "source": [ 537 | "mylist[999]" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 34, 543 | "metadata": {}, 544 | "outputs": [ 545 | { 546 | "data": { 547 | "text/plain": [ 548 | "{'first': 'Reuven', 'last': 'Lerner', 'shoesize': 46}" 549 | ] 550 | }, 551 | "execution_count": 34, 552 | "metadata": {}, 553 | "output_type": "execute_result" 554 | } 555 | ], 556 | "source": [ 557 | "d" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 35, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "data": { 567 | "text/plain": [ 568 | "3" 569 | ] 570 | }, 571 | "execution_count": 35, 572 | "metadata": {}, 573 | "output_type": "execute_result" 574 | } 575 | ], 576 | "source": [ 577 | "len(d)" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 36, 583 | "metadata": {}, 584 | "outputs": [ 585 | { 586 | "data": { 587 | "text/plain": [ 588 | "True" 589 | ] 590 | }, 591 | "execution_count": 36, 592 | "metadata": {}, 593 | "output_type": "execute_result" 594 | } 595 | ], 596 | "source": [ 597 | "'a' in 'aeiou'" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": 37, 603 | "metadata": {}, 604 | "outputs": [ 605 | { 606 | "data": { 607 | "text/plain": [ 608 | "True" 609 | ] 610 | }, 611 | "execution_count": 37, 612 | "metadata": {}, 613 | "output_type": "execute_result" 614 | } 615 | ], 616 | "source": [ 617 | "10 in [10, 20, 30]" 618 | ] 619 | }, 620 | { 621 | "cell_type": "code", 622 | "execution_count": 38, 623 | "metadata": {}, 624 | "outputs": [ 625 | { 626 | "data": { 627 | "text/plain": [ 628 | "{'first': 'Reuven', 'last': 'Lerner', 'shoesize': 46}" 629 | ] 630 | }, 631 | "execution_count": 38, 632 | "metadata": {}, 633 | "output_type": "execute_result" 634 | } 635 | ], 636 | "source": [ 637 | "d" 638 | ] 639 | }, 640 | { 641 | "cell_type": "code", 642 | "execution_count": 39, 643 | "metadata": {}, 644 | "outputs": [ 645 | { 646 | "data": { 647 | "text/plain": [ 648 | "False" 649 | ] 650 | }, 651 | "execution_count": 39, 652 | "metadata": {}, 653 | "output_type": "execute_result" 654 | } 655 | ], 656 | "source": [ 657 | "'Reuven' in d" 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": 40, 663 | "metadata": {}, 664 | "outputs": [ 665 | { 666 | "data": { 667 | "text/plain": [ 668 | "True" 669 | ] 670 | }, 671 | "execution_count": 40, 672 | "metadata": {}, 673 | "output_type": "execute_result" 674 | } 675 | ], 676 | "source": [ 677 | "# searching in dicts happens on the keys ONLY\n", 678 | "'first' in d" 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": 41, 684 | "metadata": {}, 685 | "outputs": [], 686 | "source": [ 687 | "d = {} # empty dict\n", 688 | "d['a'] = 100 # added the key-value pair 'a':100" 689 | ] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": 42, 694 | "metadata": {}, 695 | "outputs": [ 696 | { 697 | "data": { 698 | "text/plain": [ 699 | "{'a': 100}" 700 | ] 701 | }, 702 | "execution_count": 42, 703 | "metadata": {}, 704 | "output_type": "execute_result" 705 | } 706 | ], 707 | "source": [ 708 | "d" 709 | ] 710 | }, 711 | { 712 | "cell_type": "code", 713 | "execution_count": 43, 714 | "metadata": {}, 715 | "outputs": [ 716 | { 717 | "data": { 718 | "text/plain": [ 719 | "{'a': 100, 'b': 200}" 720 | ] 721 | }, 722 | "execution_count": 43, 723 | "metadata": {}, 724 | "output_type": "execute_result" 725 | } 726 | ], 727 | "source": [ 728 | "d['b'] = 200\n", 729 | "d" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 45, 735 | "metadata": {}, 736 | "outputs": [ 737 | { 738 | "data": { 739 | "text/plain": [ 740 | "{'a': 999, 'b': 200}" 741 | ] 742 | }, 743 | "execution_count": 45, 744 | "metadata": {}, 745 | "output_type": "execute_result" 746 | } 747 | ], 748 | "source": [ 749 | "d['a'] = 999 # replace the value associated with 'a'\n", 750 | "d" 751 | ] 752 | }, 753 | { 754 | "cell_type": "code", 755 | "execution_count": 46, 756 | "metadata": {}, 757 | "outputs": [ 758 | { 759 | "data": { 760 | "text/plain": [ 761 | "{'a': 999, 'b': 200, 'x': 123, 'q': 456}" 762 | ] 763 | }, 764 | "execution_count": 46, 765 | "metadata": {}, 766 | "output_type": "execute_result" 767 | } 768 | ], 769 | "source": [ 770 | "d['x'] = 123\n", 771 | "d['q'] = 456\n", 772 | "d" 773 | ] 774 | }, 775 | { 776 | "cell_type": "code", 777 | "execution_count": 47, 778 | "metadata": {}, 779 | "outputs": [], 780 | "source": [ 781 | "d['r'] = [10, 20, 30]" 782 | ] 783 | }, 784 | { 785 | "cell_type": "code", 786 | "execution_count": 48, 787 | "metadata": {}, 788 | "outputs": [ 789 | { 790 | "data": { 791 | "text/plain": [ 792 | "{'a': 999, 'b': 200, 'x': 123, 'q': 456, 'r': [10, 20, 30]}" 793 | ] 794 | }, 795 | "execution_count": 48, 796 | "metadata": {}, 797 | "output_type": "execute_result" 798 | } 799 | ], 800 | "source": [ 801 | "d" 802 | ] 803 | }, 804 | { 805 | "cell_type": "code", 806 | "execution_count": 51, 807 | "metadata": {}, 808 | "outputs": [ 809 | { 810 | "data": { 811 | "text/plain": [ 812 | "{'a': 999, 'b': 200, 'x': 123, 'q': 456, 'r': [10, 20, 30]}" 813 | ] 814 | }, 815 | "execution_count": 51, 816 | "metadata": {}, 817 | "output_type": "execute_result" 818 | } 819 | ], 820 | "source": [ 821 | "d" 822 | ] 823 | }, 824 | { 825 | "cell_type": "code", 826 | "execution_count": 52, 827 | "metadata": {}, 828 | "outputs": [ 829 | { 830 | "data": { 831 | "text/plain": [ 832 | "999" 833 | ] 834 | }, 835 | "execution_count": 52, 836 | "metadata": {}, 837 | "output_type": "execute_result" 838 | } 839 | ], 840 | "source": [ 841 | "d.pop('a') # removes the key 'a', and returns its value" 842 | ] 843 | }, 844 | { 845 | "cell_type": "code", 846 | "execution_count": 53, 847 | "metadata": {}, 848 | "outputs": [ 849 | { 850 | "data": { 851 | "text/plain": [ 852 | "{'b': 200, 'x': 123, 'q': 456, 'r': [10, 20, 30]}" 853 | ] 854 | }, 855 | "execution_count": 53, 856 | "metadata": {}, 857 | "output_type": "execute_result" 858 | } 859 | ], 860 | "source": [ 861 | "d" 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": null, 867 | "metadata": {}, 868 | "outputs": [], 869 | "source": [] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": 54, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "country_codes = {'USA':1,\n", 878 | " 'Israel':972,\n", 879 | " 'UK':44}" 880 | ] 881 | }, 882 | { 883 | "cell_type": "code", 884 | "execution_count": 55, 885 | "metadata": {}, 886 | "outputs": [ 887 | { 888 | "name": "stdout", 889 | "output_type": "stream", 890 | "text": [ 891 | "Enter country name: Israel\n", 892 | "Code for Israel is 972\n", 893 | "Enter country name: France\n", 894 | "No info for country France\n", 895 | "Enter country name: \n" 896 | ] 897 | } 898 | ], 899 | "source": [ 900 | "while True:\n", 901 | " country_name = input(\"Enter country name: \").strip()\n", 902 | " \n", 903 | " if not country_name: # meaning: blank country name? exit the loop!\n", 904 | " break\n", 905 | " \n", 906 | " if country_name in country_codes:\n", 907 | " print(f'Code for {country_name} is {country_codes[country_name]}')\n", 908 | " \n", 909 | " else:\n", 910 | " print(f'No info for country {country_name}')" 911 | ] 912 | }, 913 | { 914 | "cell_type": "code", 915 | "execution_count": 56, 916 | "metadata": {}, 917 | "outputs": [ 918 | { 919 | "name": "stdout", 920 | "output_type": "stream", 921 | "text": [ 922 | "Enter a number: 10\n", 923 | "Enter a number: 11\n", 924 | "Enter a number: 12\n", 925 | "Enter a number: 14\n", 926 | "Enter a number: 16\n", 927 | "Enter a number: 18\n", 928 | "Enter a number: \n" 929 | ] 930 | } 931 | ], 932 | "source": [ 933 | "counts = {'evens':0,\n", 934 | " 'odds':0}\n", 935 | "\n", 936 | "while True:\n", 937 | " n = input(\"Enter a number: \").strip()\n", 938 | " \n", 939 | " if not n: # if empty/blank, then break\n", 940 | " break\n", 941 | " \n", 942 | " if not n.isdigit():\n", 943 | " print(f'{n} is not an integer. Try again.')\n", 944 | " continue\n", 945 | " \n", 946 | " n = int(n)\n", 947 | " \n", 948 | " if n % 2 == 0:\n", 949 | " counts['evens'] += 1\n", 950 | " else:\n", 951 | " counts['odds'] += 1" 952 | ] 953 | }, 954 | { 955 | "cell_type": "code", 956 | "execution_count": 57, 957 | "metadata": {}, 958 | "outputs": [ 959 | { 960 | "data": { 961 | "text/plain": [ 962 | "{'evens': 5, 'odds': 1}" 963 | ] 964 | }, 965 | "execution_count": 57, 966 | "metadata": {}, 967 | "output_type": "execute_result" 968 | } 969 | ], 970 | "source": [ 971 | "counts" 972 | ] 973 | }, 974 | { 975 | "cell_type": "code", 976 | "execution_count": 59, 977 | "metadata": {}, 978 | "outputs": [ 979 | { 980 | "data": { 981 | "text/plain": [ 982 | "True" 983 | ] 984 | }, 985 | "execution_count": 59, 986 | "metadata": {}, 987 | "output_type": "execute_result" 988 | } 989 | ], 990 | "source": [ 991 | "mylist = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\n", 992 | "\n", 993 | "50 in mylist # O(n) -- search speed is proportional to the lists's length\n", 994 | " # the longer the list, the longer you'll wait" 995 | ] 996 | }, 997 | { 998 | "cell_type": "code", 999 | "execution_count": 61, 1000 | "metadata": {}, 1001 | "outputs": [ 1002 | { 1003 | "data": { 1004 | "text/plain": [ 1005 | "True" 1006 | ] 1007 | }, 1008 | "execution_count": 61, 1009 | "metadata": {}, 1010 | "output_type": "execute_result" 1011 | } 1012 | ], 1013 | "source": [ 1014 | "d = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}\n", 1015 | "\n", 1016 | "'c' in d # O(1) -- constant time!\n", 1017 | " # no matter how big or small, it'll take the same time to find something in d" 1018 | ] 1019 | }, 1020 | { 1021 | "cell_type": "code", 1022 | "execution_count": 62, 1023 | "metadata": {}, 1024 | "outputs": [], 1025 | "source": [ 1026 | "# hash function makes it all possible!" 1027 | ] 1028 | }, 1029 | { 1030 | "cell_type": "code", 1031 | "execution_count": 63, 1032 | "metadata": {}, 1033 | "outputs": [], 1034 | "source": [ 1035 | "# in other languages, \"dictionaries\" are called:\n", 1036 | "# - hash tables\n", 1037 | "# - hashes\n", 1038 | "# - associative arrays\n", 1039 | "# - maps\n", 1040 | "# - hashmaps\n", 1041 | "# - (JavaScript) objects" 1042 | ] 1043 | }, 1044 | { 1045 | "cell_type": "code", 1046 | "execution_count": 64, 1047 | "metadata": {}, 1048 | "outputs": [], 1049 | "source": [ 1050 | "# A = 1\n", 1051 | "# B = 2\n", 1052 | "# C = 3\n", 1053 | "\n", 1054 | "# S = 19" 1055 | ] 1056 | }, 1057 | { 1058 | "cell_type": "code", 1059 | "execution_count": 65, 1060 | "metadata": {}, 1061 | "outputs": [ 1062 | { 1063 | "name": "stdout", 1064 | "output_type": "stream", 1065 | "text": [ 1066 | "Enter a string: this is a test\n", 1067 | "{'t': 3, 'h': 1, 'i': 2, 's': 3, ' ': 3, 'a': 1, 'e': 1}\n" 1068 | ] 1069 | } 1070 | ], 1071 | "source": [ 1072 | "counts = {}\n", 1073 | "\n", 1074 | "s = input(\"Enter a string: \").strip()\n", 1075 | "\n", 1076 | "for one_character in s:\n", 1077 | " if one_character in counts:\n", 1078 | " counts[one_character] += 1\n", 1079 | " else:\n", 1080 | " counts[one_character] = 1\n", 1081 | " \n", 1082 | "print(counts)\n", 1083 | " " 1084 | ] 1085 | }, 1086 | { 1087 | "cell_type": "code", 1088 | "execution_count": 67, 1089 | "metadata": {}, 1090 | "outputs": [ 1091 | { 1092 | "name": "stdout", 1093 | "output_type": "stream", 1094 | "text": [ 1095 | "t\n", 1096 | "h\n", 1097 | "i\n", 1098 | "s\n", 1099 | " \n", 1100 | "a\n", 1101 | "e\n" 1102 | ] 1103 | } 1104 | ], 1105 | "source": [ 1106 | "for one_item in counts: # iterating over a dict gives you the *keys*\n", 1107 | " print(one_item)" 1108 | ] 1109 | }, 1110 | { 1111 | "cell_type": "code", 1112 | "execution_count": 68, 1113 | "metadata": {}, 1114 | "outputs": [ 1115 | { 1116 | "name": "stdout", 1117 | "output_type": "stream", 1118 | "text": [ 1119 | "t: 3\n", 1120 | "h: 1\n", 1121 | "i: 2\n", 1122 | "s: 3\n", 1123 | " : 3\n", 1124 | "a: 1\n", 1125 | "e: 1\n" 1126 | ] 1127 | } 1128 | ], 1129 | "source": [ 1130 | "for key in counts:\n", 1131 | " print(f'{key}: {counts[key]}')" 1132 | ] 1133 | }, 1134 | { 1135 | "cell_type": "code", 1136 | "execution_count": 69, 1137 | "metadata": {}, 1138 | "outputs": [ 1139 | { 1140 | "name": "stdout", 1141 | "output_type": "stream", 1142 | "text": [ 1143 | "('t', 3)\n", 1144 | "('h', 1)\n", 1145 | "('i', 2)\n", 1146 | "('s', 3)\n", 1147 | "(' ', 3)\n", 1148 | "('a', 1)\n", 1149 | "('e', 1)\n" 1150 | ] 1151 | } 1152 | ], 1153 | "source": [ 1154 | "# the \"items\" method returns a key-value tuple with each iteration\n", 1155 | "for one_thing in counts.items():\n", 1156 | " print(one_thing)" 1157 | ] 1158 | }, 1159 | { 1160 | "cell_type": "code", 1161 | "execution_count": 70, 1162 | "metadata": {}, 1163 | "outputs": [ 1164 | { 1165 | "name": "stdout", 1166 | "output_type": "stream", 1167 | "text": [ 1168 | "t: 3\n", 1169 | "h: 1\n", 1170 | "i: 2\n", 1171 | "s: 3\n", 1172 | " : 3\n", 1173 | "a: 1\n", 1174 | "e: 1\n" 1175 | ] 1176 | } 1177 | ], 1178 | "source": [ 1179 | "for key, value in counts.items():\n", 1180 | " print(f'{key}: {value}')" 1181 | ] 1182 | }, 1183 | { 1184 | "cell_type": "code", 1185 | "execution_count": 71, 1186 | "metadata": {}, 1187 | "outputs": [ 1188 | { 1189 | "data": { 1190 | "text/plain": [ 1191 | "[' ', 'a', 'e', 'h', 'i', 's', 't']" 1192 | ] 1193 | }, 1194 | "execution_count": 71, 1195 | "metadata": {}, 1196 | "output_type": "execute_result" 1197 | } 1198 | ], 1199 | "source": [ 1200 | "sorted(counts)" 1201 | ] 1202 | }, 1203 | { 1204 | "cell_type": "code", 1205 | "execution_count": 72, 1206 | "metadata": {}, 1207 | "outputs": [], 1208 | "source": [ 1209 | "# https://www.youtube.com/watch?v=QR9W81P7yTw\n", 1210 | " \n", 1211 | "# FunctionDissectionLab.com" 1212 | ] 1213 | }, 1214 | { 1215 | "cell_type": "code", 1216 | "execution_count": null, 1217 | "metadata": {}, 1218 | "outputs": [], 1219 | "source": [] 1220 | }, 1221 | { 1222 | "cell_type": "code", 1223 | "execution_count": 74, 1224 | "metadata": {}, 1225 | "outputs": [ 1226 | { 1227 | "name": "stdout", 1228 | "output_type": "stream", 1229 | "text": [ 1230 | "Enter a sentence: this is test number 3 for my program\n", 1231 | "digits: 1\n", 1232 | "vowels: 8\n", 1233 | "other: 27\n" 1234 | ] 1235 | } 1236 | ], 1237 | "source": [ 1238 | "# ask the user to enter a sentence / words /whatever\n", 1239 | "# count how many (a) digits, (b) vowels, and (c) other characters are\n", 1240 | "# in the user's input\n", 1241 | "# keep track of them in a dict\n", 1242 | "# print each count when we're done\n", 1243 | "\n", 1244 | "\n", 1245 | "counts = {'digits':0,\n", 1246 | " 'vowels':0,\n", 1247 | " 'other':0}\n", 1248 | "\n", 1249 | "s = input(\"Enter a sentence: \").strip()\n", 1250 | "\n", 1251 | "for one_character in s:\n", 1252 | " if one_character.isdigit():\n", 1253 | " counts['digits'] += 1\n", 1254 | " elif one_character in 'aeiou':\n", 1255 | " counts['vowels'] += 1\n", 1256 | " else:\n", 1257 | " counts['other'] += 1\n", 1258 | " \n", 1259 | "for key, value in counts.items():\n", 1260 | " print(f'{key}: {value}')" 1261 | ] 1262 | }, 1263 | { 1264 | "cell_type": "code", 1265 | "execution_count": null, 1266 | "metadata": {}, 1267 | "outputs": [], 1268 | "source": [] 1269 | } 1270 | ], 1271 | "metadata": { 1272 | "kernelspec": { 1273 | "display_name": "Python 3", 1274 | "language": "python", 1275 | "name": "python3" 1276 | }, 1277 | "language_info": { 1278 | "codemirror_mode": { 1279 | "name": "ipython", 1280 | "version": 3 1281 | }, 1282 | "file_extension": ".py", 1283 | "mimetype": "text/x-python", 1284 | "name": "python", 1285 | "nbconvert_exporter": "python", 1286 | "pygments_lexer": "ipython3", 1287 | "version": "3.7.7" 1288 | } 1289 | }, 1290 | "nbformat": 4, 1291 | "nbformat_minor": 2 1292 | } 1293 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Non-programmers 2020 April 10-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Non-programmers 2020 April 3-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Non-programmers 2020 March 20-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Non-programmers 2020 March 27-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Non-programmers, 2020 May 8-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/Untitled-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /Lesson 15 -- 2020 July 3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Write a function that gets a directory name, and returns a dict whose keys are the file extensions (i.e., the stuff at the end, like .txt or .jpg, but following the dot) and whose values are the number of files with such an extension. You’ll likely want to use os.listdir or glob.glob, which work a bit differently; the first is easier to use, if you’re not sure!\n", 8 | " " 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 2, 14 | "metadata": {}, 15 | "outputs": [ 16 | { 17 | "data": { 18 | "text/plain": [ 19 | "{'d': 9,\n", 20 | " 'd-migrated2launchd': 1,\n", 21 | " 'system_default': 3,\n", 22 | " 'applesaved': 14,\n", 23 | " 'common': 1,\n", 24 | " 'logout~orig': 1,\n", 25 | " '2-previous~orig': 1,\n", 26 | " 'login': 1,\n", 27 | " 'conf': 16,\n", 28 | " 'conf~previous': 3,\n", 29 | " 'keytab': 1,\n", 30 | " 'logout': 1,\n", 31 | " 'db': 1,\n", 32 | " 'lpd': 1,\n", 33 | " 'passwd~orig': 1,\n", 34 | " 'user_modified': 1,\n", 35 | " 'login~orig': 1,\n", 36 | " 'default-previous': 1,\n", 37 | " 'conf-previous': 1,\n", 38 | " 'common~previous': 1,\n", 39 | " 'default': 2,\n", 40 | " 'cshrc~orig': 1,\n", 41 | " 'codes~orig': 1,\n", 42 | " 'equiv': 1,\n", 43 | " 'codes': 1,\n", 44 | " 'ini': 1,\n", 45 | " 'cshrc': 1,\n", 46 | " 'hd~previous': 1,\n", 47 | " 'deprecated': 1,\n", 48 | " 'rc': 2,\n", 49 | " 'netboot': 1,\n", 50 | " 'cfg~orig': 1,\n", 51 | " 'conf~orig': 1,\n", 52 | " 'old': 1,\n", 53 | " 'rc~previous': 1,\n", 54 | " 'os': 1,\n", 55 | " 'anchors': 1,\n", 56 | " 'default-previous~orig': 1,\n", 57 | " '2-previous': 1,\n", 58 | " 'passwd': 1,\n", 59 | " 'hd': 1,\n", 60 | " 'launchd': 1,\n", 61 | " 'rc~orig': 1,\n", 62 | " 'cfg': 1,\n", 63 | " 'pg': 1}" 64 | ] 65 | }, 66 | "execution_count": 2, 67 | "metadata": {}, 68 | "output_type": "execute_result" 69 | } 70 | ], 71 | "source": [ 72 | "import os\n", 73 | "\n", 74 | "def ext_counter(dirname):\n", 75 | " output = {}\n", 76 | " \n", 77 | " for one_filename in os.listdir(dirname):\n", 78 | " if '.' not in one_filename:\n", 79 | " continue\n", 80 | " \n", 81 | " ext = one_filename.split('.')[-1]\n", 82 | " \n", 83 | " if ext in output: # have we seen this extension before?\n", 84 | " output[ext] += 1\n", 85 | " else:\n", 86 | " output[ext] = 1\n", 87 | " \n", 88 | " return output\n", 89 | "\n", 90 | "ext_counter('/etc/')" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "The random module includes a function, random.choice, that can return a random element from any sequence (including a string). Write a function that takes a list, chooses a random element from it, and then asks the user to repeatedly guess which element has been chosen. When the user guesses correctly, the function returns the number of guesses it took." 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 10, 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "import random\n", 107 | "\n", 108 | "def count_guesses(options):\n", 109 | " chosen = random.choice(options)\n", 110 | " count = 0\n", 111 | "\n", 112 | " while True:\n", 113 | " count += 1\n", 114 | " guess = input(\"Enter your guess: \").strip()\n", 115 | " \n", 116 | " if guess == chosen:\n", 117 | " print(f'Right!')\n", 118 | " break\n", 119 | " \n", 120 | " print(f'Wrong!')\n", 121 | " \n", 122 | " return count" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 14, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "Enter your guess: a\n", 135 | "Wrong!\n", 136 | "Enter your guess: b\n", 137 | "Wrong!\n", 138 | "Enter your guess: c\n", 139 | "Wrong!\n", 140 | "Enter your guess: d\n", 141 | "Right!\n" 142 | ] 143 | }, 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "4" 148 | ] 149 | }, 150 | "execution_count": 14, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "count_guesses(['a', 'b', 'c', 'd', 'e', 'f'])" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "This is a bit harder, but worth trying a bit: The pickle module lets you turn data structures into bytestrings, and back. That’s normally used for storing data on disk and then retrieving it later. You take a data structure and turn it into a bytestring with pickle.dumps, which returns a bytestring. You can then take a bytestring and turn it back into data with pickle.loads. Create a dict in which the keys are strings and the values are ints, strings, and lists (not too many). Use pickle.dumps to turn it into a bytestring. Then turn it back into a data structure… are they the same?" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 15, 169 | "metadata": {}, 170 | "outputs": [], 171 | "source": [ 172 | "import pickle\n", 173 | "\n", 174 | "d = {'a':1, 'b':2, 'c':'this is a test', 'd':[10, 20, 30]}\n", 175 | "\n", 176 | "b = pickle.dumps(d)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 16, 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "data": { 186 | "text/plain": [ 187 | "b'\\x80\\x04\\x954\\x00\\x00\\x00\\x00\\x00\\x00\\x00}\\x94(\\x8c\\x01a\\x94K\\x01\\x8c\\x01b\\x94K\\x02\\x8c\\x01c\\x94\\x8c\\x0ethis is a test\\x94\\x8c\\x01d\\x94]\\x94(K\\nK\\x14K\\x1eeu.'" 188 | ] 189 | }, 190 | "execution_count": 16, 191 | "metadata": {}, 192 | "output_type": "execute_result" 193 | } 194 | ], 195 | "source": [ 196 | "b" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 17, 202 | "metadata": {}, 203 | "outputs": [], 204 | "source": [ 205 | "also_a_d = pickle.loads(b)" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 18, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "data": { 215 | "text/plain": [ 216 | "{'a': 1, 'b': 2, 'c': 'this is a test', 'd': [10, 20, 30]}" 217 | ] 218 | }, 219 | "execution_count": 18, 220 | "metadata": {}, 221 | "output_type": "execute_result" 222 | } 223 | ], 224 | "source": [ 225 | "also_a_d" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 21, 231 | "metadata": {}, 232 | "outputs": [], 233 | "source": [ 234 | "import random\n", 235 | "\n", 236 | "def count_guesses(options):\n", 237 | " chosen = random.choice(options)\n", 238 | " print(f'Shh, do not tell the user, but {chosen=}')\n", 239 | " already_guessed = []\n", 240 | " count = 0\n", 241 | "\n", 242 | " while True:\n", 243 | " count += 1\n", 244 | " guess = input(\"Enter your guess: \").strip()\n", 245 | " \n", 246 | " if guess in already_guessed:\n", 247 | " print(f'You already guessed {guess} -- try something new!')\n", 248 | " count -= 1\n", 249 | " continue\n", 250 | " \n", 251 | " already_guessed.append(guess)\n", 252 | "\n", 253 | " if guess == chosen:\n", 254 | " print(f'Right!')\n", 255 | " break\n", 256 | " \n", 257 | " print(f'Wrong!')\n", 258 | " \n", 259 | " return count" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 23, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "name": "stdout", 269 | "output_type": "stream", 270 | "text": [ 271 | "Shh, do not tell the user, but chosen='b'\n", 272 | "Enter your guess: c\n", 273 | "Wrong!\n", 274 | "Enter your guess: d\n", 275 | "Wrong!\n", 276 | "Enter your guess: d\n", 277 | "You already guessed d -- try something new!\n", 278 | "Enter your guess: d\n", 279 | "You already guessed d -- try something new!\n", 280 | "Enter your guess: d\n", 281 | "You already guessed d -- try something new!\n", 282 | "Enter your guess: d\n", 283 | "You already guessed d -- try something new!\n", 284 | "Enter your guess: b\n", 285 | "Right!\n" 286 | ] 287 | }, 288 | { 289 | "data": { 290 | "text/plain": [ 291 | "3" 292 | ] 293 | }, 294 | "execution_count": 23, 295 | "metadata": {}, 296 | "output_type": "execute_result" 297 | } 298 | ], 299 | "source": [ 300 | "count_guesses(['a', 'b', 'c', 'd', 'e', 'f'])" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 25, 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "name": "stdout", 310 | "output_type": "stream", 311 | "text": [ 312 | "Hey, I found a problem. You should address it.\n" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "x = [10, 20, 30]\n", 318 | "\n", 319 | "try:\n", 320 | " print(x[99999])\n", 321 | "except:\n", 322 | " print(f'Hey, I found a problem. You should address it.')" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": null, 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [ 331 | "# comprehension" 332 | ] 333 | } 334 | ], 335 | "metadata": { 336 | "kernelspec": { 337 | "display_name": "Python 3", 338 | "language": "python", 339 | "name": "python3" 340 | }, 341 | "language_info": { 342 | "codemirror_mode": { 343 | "name": "ipython", 344 | "version": 3 345 | }, 346 | "file_extension": ".py", 347 | "mimetype": "text/x-python", 348 | "name": "python", 349 | "nbconvert_exporter": "python", 350 | "pygments_lexer": "ipython3", 351 | "version": "3.8.3" 352 | } 353 | }, 354 | "nbformat": 4, 355 | "nbformat_minor": 4 356 | } 357 | -------------------------------------------------------------------------------- /Lesson 6 -- 2020 Apr 24.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 18, 6 | "metadata": { 7 | "scrolled": true 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Enter a string: abcdefg\n", 15 | "Enter a letter: g\n", 16 | "I found g at index 6\n", 17 | "Its neighbors are at fg\n" 18 | ] 19 | } 20 | ], 21 | "source": [ 22 | "# ask user to enter a string\n", 23 | "# ask the user to enter a letter\n", 24 | "\n", 25 | "# print the index at which the letter is located.\n", 26 | "# also print the letter preceding and following that letter\n", 27 | "\n", 28 | "s = input(\"Enter a string: \").strip()\n", 29 | "look_for = input(\"Enter a letter: \").strip()\n", 30 | "\n", 31 | "i = s.find(look_for)\n", 32 | "\n", 33 | "if i == -1:\n", 34 | " print(f'{look_for} is not in {s}')\n", 35 | "else:\n", 36 | " print(f'I found {s[i]} at index {i}')\n", 37 | " if i > 0:\n", 38 | " print(f'Its neighbors are at {s[i-1:i+2]}') # start:stop+1\n", 39 | " else:\n", 40 | " print(f'Its neighbors are at {s[i:i+2]}')" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 16, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "''" 52 | ] 53 | }, 54 | "execution_count": 16, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "s[-1:2] # index of -1 is the FINAL CHARACTER in the string" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 22, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "'f'" 72 | ] 73 | }, 74 | "execution_count": 22, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "s[i-1]" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 24, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "ename": "IndexError", 90 | "evalue": "string index out of range", 91 | "output_type": "error", 92 | "traceback": [ 93 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 94 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 95 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# you cannot request an INDEX greater than len(s)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 96 | "\u001b[0;31mIndexError\u001b[0m: string index out of range" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "s[i+2] # you cannot request an INDEX greater than len(s)\n" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 25, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "'g'" 113 | ] 114 | }, 115 | "execution_count": 25, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "# but you can have a slice whose index goes beyond the string\n", 122 | "s[i:10000000]" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 4, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "-1" 134 | ] 135 | }, 136 | "execution_count": 4, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "s.find('!')" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 32, 148 | "metadata": {}, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "Enter a string: abcdefg\n", 155 | "Enter start index: \n", 156 | "Enter stop index: \n", 157 | "You must enter numbers for the start and stop!\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "# Ask the user to enter a string\n", 163 | "# Ask the user to enter two numbers (separate inputs)\n", 164 | "# Print the string from the first to the second\n", 165 | "\n", 166 | "s = input(\"Enter a string: \").strip()\n", 167 | "start = input(\"Enter start index: \").strip()\n", 168 | "stop = input(\"Enter stop index: \").strip()\n", 169 | "\n", 170 | "if start.isdigit() and stop.isdigit():\n", 171 | " start = int(start)\n", 172 | " stop = int(stop)\n", 173 | " \n", 174 | " if start < 0:\n", 175 | " print(f'{start} is < 0')\n", 176 | " elif start >= len(s):\n", 177 | " print(f'{start} is too big')\n", 178 | " elif stop < 0:\n", 179 | " print(f'{stop} is < 0')\n", 180 | " elif stop >= len(s):\n", 181 | " print(f'{stop} is too big')\n", 182 | " elif start > stop:\n", 183 | " print(f'{start} is greater than {stop}')\n", 184 | " else:\n", 185 | " print(s[start:stop])\n", 186 | "else:\n", 187 | " print(f'You must enter numbers for the start and stop!')\n" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [ 196 | "0123456\n", 197 | "abcdefg" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 33, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "7" 209 | ] 210 | }, 211 | "execution_count": 33, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "len(s)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 34, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "ename": "IndexError", 227 | "evalue": "string index out of range", 228 | "output_type": "error", 229 | "traceback": [ 230 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 231 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 232 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 233 | "\u001b[0;31mIndexError\u001b[0m: string index out of range" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "s[7]" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 36, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "# loops!\n", 248 | "# for loops\n", 249 | "# looping over strings\n", 250 | "# looping a number of times\n", 251 | "# while loops" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 38, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "a\n", 264 | "b\n", 265 | "c\n", 266 | "d\n", 267 | "e\n", 268 | "f\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "s = 'abcdef'\n", 274 | "\n", 275 | "# DRY -- don't repeat yourself!\n", 276 | "\n", 277 | "print(s[0])\n", 278 | "print(s[1])\n", 279 | "print(s[2])\n", 280 | "print(s[3])\n", 281 | "print(s[4])\n", 282 | "print(s[5])" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 40, 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "name": "stdout", 292 | "output_type": "stream", 293 | "text": [ 294 | "before\n", 295 | "a\n", 296 | "b\n", 297 | "c\n", 298 | "d\n", 299 | "e\n", 300 | "f\n", 301 | "after\n" 302 | ] 303 | } 304 | ], 305 | "source": [ 306 | "# print each character of s,\n", 307 | "# starting at the beginning and ending at the end\n", 308 | "\n", 309 | "# for loop -- executes some code for every element of a string\n", 310 | "\n", 311 | "print('before')\n", 312 | "for one_character in s: # for asks s: are you iterable?\n", 313 | " print(one_character)\n", 314 | "print('after')" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 41, 320 | "metadata": {}, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "before\n", 327 | "a\n", 328 | "b\n", 329 | "c\n", 330 | "d\n", 331 | "e\n", 332 | "f\n", 333 | "after\n" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "\n", 339 | "print('before')\n", 340 | "for one_gb in s: # for asks s: are you iterable?\n", 341 | " print(one_gb)\n", 342 | "print('after')" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "for VARIABLE in COLLECTION:\n", 352 | " body_line_1\n", 353 | " body_line_2\n", 354 | " " 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 42, 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "name": "stdout", 364 | "output_type": "stream", 365 | "text": [ 366 | "a\n", 367 | "b\n", 368 | "c\n", 369 | "d\n", 370 | "e\n", 371 | "f\n" 372 | ] 373 | } 374 | ], 375 | "source": [ 376 | "for thingee in s:\n", 377 | " print(thingee)" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 43, 383 | "metadata": {}, 384 | "outputs": [ 385 | { 386 | "data": { 387 | "text/plain": [ 388 | "'abcdef'" 389 | ] 390 | }, 391 | "execution_count": 43, 392 | "metadata": {}, 393 | "output_type": "execute_result" 394 | } 395 | ], 396 | "source": [ 397 | "s" 398 | ] 399 | }, 400 | { 401 | "cell_type": "code", 402 | "execution_count": 44, 403 | "metadata": {}, 404 | "outputs": [ 405 | { 406 | "data": { 407 | "text/plain": [ 408 | "True" 409 | ] 410 | }, 411 | "execution_count": 44, 412 | "metadata": {}, 413 | "output_type": "execute_result" 414 | } 415 | ], 416 | "source": [ 417 | "'c' in s # return True or False" 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": 46, 423 | "metadata": {}, 424 | "outputs": [ 425 | { 426 | "name": "stdout", 427 | "output_type": "stream", 428 | "text": [ 429 | "I found c in abcdabcdabcdccc!\n", 430 | "I found c in abcdabcdabcdccc!\n", 431 | "I found c in abcdabcdabcdccc!\n", 432 | "I found c in abcdabcdabcdccc!\n", 433 | "I found c in abcdabcdabcdccc!\n", 434 | "I found c in abcdabcdabcdccc!\n" 435 | ] 436 | } 437 | ], 438 | "source": [ 439 | "s = 'abcdabcdabcdccc'\n", 440 | "look_for = 'c'\n", 441 | "\n", 442 | "for one_character in s:\n", 443 | " if one_character == look_for:\n", 444 | " print(f'I found {one_character} in {s}!')" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 47, 450 | "metadata": {}, 451 | "outputs": [ 452 | { 453 | "name": "stdout", 454 | "output_type": "stream", 455 | "text": [ 456 | "Enter a word: elephant\n", 457 | "elephant contains 3 vowels\n" 458 | ] 459 | } 460 | ], 461 | "source": [ 462 | "# let's count vowels in the user's input!\n", 463 | "\n", 464 | "count = 0\n", 465 | "s = input(\"Enter a word: \")\n", 466 | "\n", 467 | "for one_letter in s:\n", 468 | " if one_letter in 'aeiou':\n", 469 | " count += 1 # add 1 to the \"count\" variable\n", 470 | " \n", 471 | "print(f'{s} contains {count} vowels')" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 48, 477 | "metadata": {}, 478 | "outputs": [ 479 | { 480 | "name": "stdout", 481 | "output_type": "stream", 482 | "text": [ 483 | "Hooray!\n", 484 | "Hooray!\n", 485 | "Hooray!\n", 486 | "Hooray!\n", 487 | "Hooray!\n" 488 | ] 489 | } 490 | ], 491 | "source": [ 492 | "print('Hooray!')\n", 493 | "print('Hooray!')\n", 494 | "print('Hooray!')\n", 495 | "print('Hooray!')\n", 496 | "print('Hooray!')\n" 497 | ] 498 | }, 499 | { 500 | "cell_type": "code", 501 | "execution_count": 50, 502 | "metadata": {}, 503 | "outputs": [ 504 | { 505 | "name": "stdout", 506 | "output_type": "stream", 507 | "text": [ 508 | "Hooray!\n", 509 | "Hooray!\n", 510 | "Hooray!\n", 511 | "Hooray!\n", 512 | "Hooray!\n" 513 | ] 514 | } 515 | ], 516 | "source": [ 517 | "for number in range(5):\n", 518 | " print('Hooray!')" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 51, 524 | "metadata": {}, 525 | "outputs": [], 526 | "source": [ 527 | "# to iterate over each character in a string, use\n", 528 | "# for one_character in s\n", 529 | "\n", 530 | "# to iterate a certain number of times, use\n", 531 | "# for one_item in range(n)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 52, 537 | "metadata": {}, 538 | "outputs": [ 539 | { 540 | "name": "stdout", 541 | "output_type": "stream", 542 | "text": [ 543 | "0 Hooray!\n", 544 | "1 Hooray!\n", 545 | "2 Hooray!\n", 546 | "3 Hooray!\n", 547 | "4 Hooray!\n" 548 | ] 549 | } 550 | ], 551 | "source": [ 552 | "for number in range(5): # from 0 until (and not including) 5\n", 553 | " print(f'{number} Hooray!')" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 56, 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "name": "stdout", 563 | "output_type": "stream", 564 | "text": [ 565 | "Enter a number: 123abc4!5\n", 566 | "Ignoring non-digit a\n", 567 | "Ignoring non-digit b\n", 568 | "Ignoring non-digit c\n", 569 | "Ignoring non-digit !\n", 570 | "total = 15\n" 571 | ] 572 | } 573 | ], 574 | "source": [ 575 | "# ask the user to enter a \"word\" containing only digits\n", 576 | "# sum the digits in that word\n", 577 | "\n", 578 | "# So if the user enters 123\n", 579 | "# you should print 6 at the end\n", 580 | "\n", 581 | "# bonus: check to see if each digit is really a digit\n", 582 | "\n", 583 | "s = input(\"Enter a number: \").strip()\n", 584 | "total = 0\n", 585 | "\n", 586 | "for one_character in s:\n", 587 | " if one_character.isdigit():\n", 588 | " total += int(one_character)\n", 589 | " else:\n", 590 | " print(f'Ignoring non-digit {one_character}')\n", 591 | " \n", 592 | "print(f'total = {total}')" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": 57, 598 | "metadata": {}, 599 | "outputs": [ 600 | { 601 | "data": { 602 | "text/plain": [ 603 | "'123abc4!5'" 604 | ] 605 | }, 606 | "execution_count": 57, 607 | "metadata": {}, 608 | "output_type": "execute_result" 609 | } 610 | ], 611 | "source": [ 612 | "s" 613 | ] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": 58, 618 | "metadata": {}, 619 | "outputs": [ 620 | { 621 | "name": "stdout", 622 | "output_type": "stream", 623 | "text": [ 624 | "index is 0, character is a\n", 625 | "index is 1, character is b\n", 626 | "index is 2, character is c\n", 627 | "index is 3, character is d\n", 628 | "index is 4, character is e\n", 629 | "index is 5, character is f\n", 630 | "index is 6, character is g\n" 631 | ] 632 | } 633 | ], 634 | "source": [ 635 | "s = 'abcdefg'\n", 636 | "\n", 637 | "for index in range(len(s)):\n", 638 | " print(f'index is {index}, character is {s[index]}')" 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "execution_count": 59, 644 | "metadata": {}, 645 | "outputs": [ 646 | { 647 | "name": "stdout", 648 | "output_type": "stream", 649 | "text": [ 650 | "x is 5\n" 651 | ] 652 | } 653 | ], 654 | "source": [ 655 | "# while loops\n", 656 | "\n", 657 | "# while loops are just like \"if\" statements...\n", 658 | "# except that so long as the condition is True,\n", 659 | "# the loop executes additional times.\n", 660 | "\n", 661 | "x = 5\n", 662 | "\n", 663 | "if x > 0:\n", 664 | " print(f'x is {x}')\n", 665 | " x -= 1 # reduce x by 1" 666 | ] 667 | }, 668 | { 669 | "cell_type": "code", 670 | "execution_count": 60, 671 | "metadata": {}, 672 | "outputs": [ 673 | { 674 | "data": { 675 | "text/plain": [ 676 | "4" 677 | ] 678 | }, 679 | "execution_count": 60, 680 | "metadata": {}, 681 | "output_type": "execute_result" 682 | } 683 | ], 684 | "source": [ 685 | "x" 686 | ] 687 | }, 688 | { 689 | "cell_type": "code", 690 | "execution_count": 61, 691 | "metadata": {}, 692 | "outputs": [ 693 | { 694 | "name": "stdout", 695 | "output_type": "stream", 696 | "text": [ 697 | "x is 5\n", 698 | "x is 4\n", 699 | "x is 3\n", 700 | "x is 2\n", 701 | "x is 1\n" 702 | ] 703 | } 704 | ], 705 | "source": [ 706 | "\n", 707 | "x = 5\n", 708 | "\n", 709 | "while x > 0:\n", 710 | " print(f'x is {x}')\n", 711 | " x -= 1 # reduce x by 1" 712 | ] 713 | }, 714 | { 715 | "cell_type": "code", 716 | "execution_count": 62, 717 | "metadata": {}, 718 | "outputs": [], 719 | "source": [ 720 | "# infinite loop == never stops, goes on forever\n", 721 | "\n", 722 | "# (1) Apple's HQ used to be at 1 Infinite Loop\n", 723 | "# (2) loop, infinite -- see infinite loop\n", 724 | "# infinite loop -- see loop, infinite\n" 725 | ] 726 | }, 727 | { 728 | "cell_type": "code", 729 | "execution_count": 63, 730 | "metadata": {}, 731 | "outputs": [], 732 | "source": [ 733 | "# these words can be used in either for or while loops:\n", 734 | "\n", 735 | "# break -- exit the loop RIGHT NOW\n", 736 | "# continue -- exit the current iteration, continue with the next one" 737 | ] 738 | }, 739 | { 740 | "cell_type": "code", 741 | "execution_count": 65, 742 | "metadata": {}, 743 | "outputs": [ 744 | { 745 | "name": "stdout", 746 | "output_type": "stream", 747 | "text": [ 748 | "start\n", 749 | "a\n", 750 | "b\n", 751 | "c\n", 752 | "end\n" 753 | ] 754 | } 755 | ], 756 | "source": [ 757 | "s = 'abcdefg'\n", 758 | "look_for = 'd'\n", 759 | "\n", 760 | "print(f'start')\n", 761 | "for one_letter in s:\n", 762 | " if one_letter == look_for:\n", 763 | " break # if we encountered 'd', stop the loop right away\n", 764 | " print(one_letter)\n", 765 | "print('end')" 766 | ] 767 | }, 768 | { 769 | "cell_type": "code", 770 | "execution_count": 66, 771 | "metadata": {}, 772 | "outputs": [ 773 | { 774 | "name": "stdout", 775 | "output_type": "stream", 776 | "text": [ 777 | "start\n", 778 | "a\n", 779 | "b\n", 780 | "c\n", 781 | "e\n", 782 | "f\n", 783 | "g\n", 784 | "end\n" 785 | ] 786 | } 787 | ], 788 | "source": [ 789 | "s = 'abcdefg'\n", 790 | "look_for = 'd'\n", 791 | "\n", 792 | "print(f'start')\n", 793 | "for one_letter in s:\n", 794 | " if one_letter == look_for:\n", 795 | " continue # stop this iteration, but go onto the next one\n", 796 | " print(one_letter)\n", 797 | "print('end')" 798 | ] 799 | }, 800 | { 801 | "cell_type": "code", 802 | "execution_count": 67, 803 | "metadata": {}, 804 | "outputs": [ 805 | { 806 | "name": "stdout", 807 | "output_type": "stream", 808 | "text": [ 809 | "Enter a number: 10\n", 810 | "Enter a number: 20\n", 811 | "Enter a number: 30\n", 812 | "Total is 60\n" 813 | ] 814 | } 815 | ], 816 | "source": [ 817 | "total = 0\n", 818 | "\n", 819 | "for i in range(3):\n", 820 | " n = input(\"Enter a number: \").strip()\n", 821 | " if n.isdigit():\n", 822 | " total += int(n)\n", 823 | "print(f'Total is {total}')" 824 | ] 825 | }, 826 | { 827 | "cell_type": "code", 828 | "execution_count": 68, 829 | "metadata": {}, 830 | "outputs": [ 831 | { 832 | "name": "stdout", 833 | "output_type": "stream", 834 | "text": [ 835 | "Enter a number: 10\n", 836 | "Enter a number: 20\n", 837 | "Enter a number: 30\n", 838 | "Enter a number: 100\n", 839 | "Enter a number: 5126\n", 840 | "Enter a number: \n", 841 | "total = 5286\n" 842 | ] 843 | } 844 | ], 845 | "source": [ 846 | "total = 0\n", 847 | "\n", 848 | "while True: # an infinite loop!\n", 849 | " n = input(\"Enter a number: \").strip()\n", 850 | " if n.isdigit():\n", 851 | " total += int(n)\n", 852 | " else:\n", 853 | " break # if we got a non-number, stop asking + leave the loop\n", 854 | "\n", 855 | "print(f'total = {total}')" 856 | ] 857 | }, 858 | { 859 | "cell_type": "code", 860 | "execution_count": null, 861 | "metadata": {}, 862 | "outputs": [], 863 | "source": [ 864 | "# let's count vowels in the user's input!\n", 865 | "\n", 866 | "count = 0\n", 867 | "s = input(\"Enter a word: \")\n", 868 | "\n", 869 | "for one_letter in s:\n", 870 | " if one_letter in 'aeiou':\n", 871 | " count += 1 # add 1 to the \"count\" variable\n", 872 | " \n", 873 | "print(f'{s} contains {count} vowels')" 874 | ] 875 | }, 876 | { 877 | "cell_type": "code", 878 | "execution_count": 69, 879 | "metadata": {}, 880 | "outputs": [ 881 | { 882 | "name": "stdout", 883 | "output_type": "stream", 884 | "text": [ 885 | "Enter a word: hello\n", 886 | "Enter a word: text\n", 887 | "Enter a word: pretend\n", 888 | "Enter a word: octopus\n", 889 | "Enter a word: elephant\n", 890 | "Enter a word: \n", 891 | " contains 11 vowels\n" 892 | ] 893 | } 894 | ], 895 | "source": [ 896 | "# write a program that asks the user to enter\n", 897 | "# any number of words. When they enter an \n", 898 | "# empty string, stop asking, and print\n", 899 | "# the number of vowels in all words entered.\n", 900 | "\n", 901 | "count = 0\n", 902 | "\n", 903 | "while True:\n", 904 | " s = input(\"Enter a word: \").strip()\n", 905 | " \n", 906 | " if s == '': # stop when we get an empty string\n", 907 | " break\n", 908 | " \n", 909 | " for one_letter in s:\n", 910 | " if one_letter in 'aeiou':\n", 911 | " count += 1 # add 1 to the \"count\" variable\n", 912 | " \n", 913 | "print(f'{s} contains {count} vowels')" 914 | ] 915 | }, 916 | { 917 | "cell_type": "code", 918 | "execution_count": null, 919 | "metadata": {}, 920 | "outputs": [], 921 | "source": [ 922 | "# for loops -- iterate over (a string)\n", 923 | "# for .. in range(n) -- iterate n times\n", 924 | "# while loops -- if statements that repeat\n", 925 | "\n", 926 | "# break -- stop the loop\n", 927 | "# continue -- stop the iteration\n" 928 | ] 929 | } 930 | ], 931 | "metadata": { 932 | "kernelspec": { 933 | "display_name": "Python 3", 934 | "language": "python", 935 | "name": "python3" 936 | }, 937 | "language_info": { 938 | "codemirror_mode": { 939 | "name": "ipython", 940 | "version": 3 941 | }, 942 | "file_extension": ".py", 943 | "mimetype": "text/x-python", 944 | "name": "python", 945 | "nbconvert_exporter": "python", 946 | "pygments_lexer": "ipython3", 947 | "version": "3.7.7" 948 | } 949 | }, 950 | "nbformat": 4, 951 | "nbformat_minor": 2 952 | } 953 | -------------------------------------------------------------------------------- /Lesson 9 -- 2020 May 15.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | ". We have previously written a program that takes a single word from the user and translates it into Pig Latin. I now want you to take an entire sentence from the user (all lower-case letters, and without punctuation) and print the translation of each word into Pig Latin. So if the user enters this is a test, the output will be histay isway away esttay." 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 5, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "Enter a sentence: this is a test\n", 20 | "histay isway away esttay\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "sentence = input(\"Enter a sentence: \").strip()\n", 26 | "all_words = sentence.split()\n", 27 | "\n", 28 | "output = []\n", 29 | "for word in all_words:\n", 30 | " if word[0] in 'aeiou':\n", 31 | " output.append(f'{word}way')\n", 32 | " else:\n", 33 | " output.append(f'{word[1:]}{word[0]}ay')\n", 34 | "print(' '.join(output))" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "sentence = input(\"Enter a sentence: \").strip()\n", 44 | "\n", 45 | "output = []\n", 46 | "for word in sentence.split():\n", 47 | " if word[0] in 'aeiou':\n", 48 | " output.append(f'{word}way')\n", 49 | " else:\n", 50 | " output.append(f'{word[1:]}{word[0]}ay')\n", 51 | "print(' '.join(output))" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 6, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "Enter a sentence: this is a test\n", 64 | "histay isway away esttay\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "all_words = input(\"Enter a sentence: \").split()\n", 70 | "\n", 71 | "output = []\n", 72 | "for word in all_words:\n", 73 | " if word[0] in 'aeiou':\n", 74 | " output.append(f'{word}way')\n", 75 | " else:\n", 76 | " output.append(f'{word[1:]}{word[0]}ay')\n", 77 | "print(' '.join(output))" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "output = []\n", 87 | "for word in input(\"Enter a sentence: \").split():\n", 88 | " if word[0] in 'aeiou':\n", 89 | " output.append(f'{word}way')\n", 90 | " else:\n", 91 | " output.append(f'{word[1:]}{word[0]}ay')\n", 92 | "print(' '.join(output))" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | ". Ask the user to enter several words separated by whitespace. Print those words separated by * characters. So if the user enters this is a test, the program should print this*is*a*test. You should not use str.replace, but rather str.split and str.join." 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 9, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "Enter a sentence: this is a test\n", 112 | "this*is*a*test\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "s = input(\"Enter a sentence: \").strip()\n", 118 | "\n", 119 | "words = s.split()\n", 120 | "print('*'.join(words))" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 11, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "Enter a sentence: this is a better test\n", 133 | "this*is*a*better*test\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "s = input(\"Enter a sentence: \").strip()\n", 139 | "\n", 140 | "print('*'.join(s.split()))" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": {}, 147 | "outputs": [], 148 | "source": [ 149 | "# strip -- removes whitespace from the edges of a string, and returns a new string\n", 150 | "# split -- returns a list of strings, based on a string\n" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | ". Ask the user to enter numbers, separated by spaces, on a single line. Append each number to one of two lists, evens or odds. Then print the even numbers, one per line (i.e., \\n characters between each) and then the odd numbers, one per line. This will require not just str.join and str.split, but also some moving between int and str, so you have been warned!" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 15, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "Enter numbers, separated by spaces: 10 11 12 abcd 1a 15 16 17\n", 170 | "abcd is not numeric; ignoring\n", 171 | "1a is not numeric; ignoring\n" 172 | ] 173 | } 174 | ], 175 | "source": [ 176 | "evens = []\n", 177 | "odds = []\n", 178 | "\n", 179 | "numbers = input(\"Enter numbers, separated by spaces: \").split()\n", 180 | "\n", 181 | "for one_number in numbers:\n", 182 | " if not one_number.isdigit():\n", 183 | " print(f'{one_number} is not numeric; ignoring')\n", 184 | " continue\n", 185 | " \n", 186 | " one_number = int(one_number)\n", 187 | " \n", 188 | " if one_number % 2 == 0:\n", 189 | " evens.append(str(one_number))\n", 190 | " else:\n", 191 | " odds.append(str(one_number))\n", 192 | " \n" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 16, 198 | "metadata": {}, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/plain": [ 203 | "['10', '12', '16']" 204 | ] 205 | }, 206 | "execution_count": 16, 207 | "metadata": {}, 208 | "output_type": "execute_result" 209 | } 210 | ], 211 | "source": [ 212 | "evens" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 17, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "data": { 222 | "text/plain": [ 223 | "['11', '15', '17']" 224 | ] 225 | }, 226 | "execution_count": 17, 227 | "metadata": {}, 228 | "output_type": "execute_result" 229 | } 230 | ], 231 | "source": [ 232 | "odds" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 18, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "10\n", 245 | "12\n", 246 | "16\n" 247 | ] 248 | } 249 | ], 250 | "source": [ 251 | "print('\\n'.join(evens))" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 19, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "11\n", 264 | "15\n", 265 | "17\n" 266 | ] 267 | } 268 | ], 269 | "source": [ 270 | "print('\\n'.join(odds))" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 20, 276 | "metadata": {}, 277 | "outputs": [ 278 | { 279 | "data": { 280 | "text/plain": [ 281 | "'abcd'" 282 | ] 283 | }, 284 | "execution_count": 20, 285 | "metadata": {}, 286 | "output_type": "execute_result" 287 | } 288 | ], 289 | "source": [ 290 | "mylist = ['abcd', 'efgh', 'ijkl']\n", 291 | "\n", 292 | "mylist[0]" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 21, 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "data": { 302 | "text/plain": [ 303 | "'efgh'" 304 | ] 305 | }, 306 | "execution_count": 21, 307 | "metadata": {}, 308 | "output_type": "execute_result" 309 | } 310 | ], 311 | "source": [ 312 | "mylist[1]" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 22, 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "data": { 322 | "text/plain": [ 323 | "'ijkl'" 324 | ] 325 | }, 326 | "execution_count": 22, 327 | "metadata": {}, 328 | "output_type": "execute_result" 329 | } 330 | ], 331 | "source": [ 332 | "mylist[2]" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 23, 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | "person = ['Reuven', 'Lerner', 46]" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 24, 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "data": { 351 | "text/plain": [ 352 | "'Reuven'" 353 | ] 354 | }, 355 | "execution_count": 24, 356 | "metadata": {}, 357 | "output_type": "execute_result" 358 | } 359 | ], 360 | "source": [ 361 | "person[0]" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 25, 367 | "metadata": {}, 368 | "outputs": [ 369 | { 370 | "data": { 371 | "text/plain": [ 372 | "'Lerner'" 373 | ] 374 | }, 375 | "execution_count": 25, 376 | "metadata": {}, 377 | "output_type": "execute_result" 378 | } 379 | ], 380 | "source": [ 381 | "person[1]" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": 26, 387 | "metadata": {}, 388 | "outputs": [ 389 | { 390 | "data": { 391 | "text/plain": [ 392 | "46" 393 | ] 394 | }, 395 | "execution_count": 26, 396 | "metadata": {}, 397 | "output_type": "execute_result" 398 | } 399 | ], 400 | "source": [ 401 | "person[2]" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": 27, 407 | "metadata": {}, 408 | "outputs": [], 409 | "source": [ 410 | "# dictionaries: like lists, but we can use either ints or strings as the keys/indexes\n", 411 | "\n", 412 | "# key-value pairs\n", 413 | "# name-value pairs\n", 414 | "\n", 415 | "d = {'first':'Reuven', 'last':'Lerner', 'shoesize':46}" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 28, 421 | "metadata": {}, 422 | "outputs": [ 423 | { 424 | "data": { 425 | "text/plain": [ 426 | "{'first': 'Reuven', 'last': 'Lerner', 'shoesize': 46}" 427 | ] 428 | }, 429 | "execution_count": 28, 430 | "metadata": {}, 431 | "output_type": "execute_result" 432 | } 433 | ], 434 | "source": [ 435 | "d" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 29, 441 | "metadata": {}, 442 | "outputs": [ 443 | { 444 | "data": { 445 | "text/plain": [ 446 | "'Reuven'" 447 | ] 448 | }, 449 | "execution_count": 29, 450 | "metadata": {}, 451 | "output_type": "execute_result" 452 | } 453 | ], 454 | "source": [ 455 | "d['first']" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 30, 461 | "metadata": {}, 462 | "outputs": [ 463 | { 464 | "data": { 465 | "text/plain": [ 466 | "'Lerner'" 467 | ] 468 | }, 469 | "execution_count": 30, 470 | "metadata": {}, 471 | "output_type": "execute_result" 472 | } 473 | ], 474 | "source": [ 475 | "d['last']" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 31, 481 | "metadata": {}, 482 | "outputs": [ 483 | { 484 | "data": { 485 | "text/plain": [ 486 | "46" 487 | ] 488 | }, 489 | "execution_count": 31, 490 | "metadata": {}, 491 | "output_type": "execute_result" 492 | } 493 | ], 494 | "source": [ 495 | "d['shoesize']" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 32, 501 | "metadata": {}, 502 | "outputs": [ 503 | { 504 | "ename": "KeyError", 505 | "evalue": "'email'", 506 | "output_type": "error", 507 | "traceback": [ 508 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 509 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 510 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'email'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 511 | "\u001b[0;31mKeyError\u001b[0m: 'email'" 512 | ] 513 | } 514 | ], 515 | "source": [ 516 | "d['email']" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": 33, 522 | "metadata": {}, 523 | "outputs": [ 524 | { 525 | "ename": "IndexError", 526 | "evalue": "list index out of range", 527 | "output_type": "error", 528 | "traceback": [ 529 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 530 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 531 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmylist\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m999\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 532 | "\u001b[0;31mIndexError\u001b[0m: list index out of range" 533 | ] 534 | } 535 | ], 536 | "source": [ 537 | "mylist[999]" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 34, 543 | "metadata": {}, 544 | "outputs": [ 545 | { 546 | "data": { 547 | "text/plain": [ 548 | "{'first': 'Reuven', 'last': 'Lerner', 'shoesize': 46}" 549 | ] 550 | }, 551 | "execution_count": 34, 552 | "metadata": {}, 553 | "output_type": "execute_result" 554 | } 555 | ], 556 | "source": [ 557 | "d" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 35, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "data": { 567 | "text/plain": [ 568 | "3" 569 | ] 570 | }, 571 | "execution_count": 35, 572 | "metadata": {}, 573 | "output_type": "execute_result" 574 | } 575 | ], 576 | "source": [ 577 | "len(d)" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 36, 583 | "metadata": {}, 584 | "outputs": [ 585 | { 586 | "data": { 587 | "text/plain": [ 588 | "True" 589 | ] 590 | }, 591 | "execution_count": 36, 592 | "metadata": {}, 593 | "output_type": "execute_result" 594 | } 595 | ], 596 | "source": [ 597 | "'a' in 'aeiou'" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": 37, 603 | "metadata": {}, 604 | "outputs": [ 605 | { 606 | "data": { 607 | "text/plain": [ 608 | "True" 609 | ] 610 | }, 611 | "execution_count": 37, 612 | "metadata": {}, 613 | "output_type": "execute_result" 614 | } 615 | ], 616 | "source": [ 617 | "10 in [10, 20, 30]" 618 | ] 619 | }, 620 | { 621 | "cell_type": "code", 622 | "execution_count": 38, 623 | "metadata": {}, 624 | "outputs": [ 625 | { 626 | "data": { 627 | "text/plain": [ 628 | "{'first': 'Reuven', 'last': 'Lerner', 'shoesize': 46}" 629 | ] 630 | }, 631 | "execution_count": 38, 632 | "metadata": {}, 633 | "output_type": "execute_result" 634 | } 635 | ], 636 | "source": [ 637 | "d" 638 | ] 639 | }, 640 | { 641 | "cell_type": "code", 642 | "execution_count": 39, 643 | "metadata": {}, 644 | "outputs": [ 645 | { 646 | "data": { 647 | "text/plain": [ 648 | "False" 649 | ] 650 | }, 651 | "execution_count": 39, 652 | "metadata": {}, 653 | "output_type": "execute_result" 654 | } 655 | ], 656 | "source": [ 657 | "'Reuven' in d" 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": 40, 663 | "metadata": {}, 664 | "outputs": [ 665 | { 666 | "data": { 667 | "text/plain": [ 668 | "True" 669 | ] 670 | }, 671 | "execution_count": 40, 672 | "metadata": {}, 673 | "output_type": "execute_result" 674 | } 675 | ], 676 | "source": [ 677 | "# searching in dicts happens on the keys ONLY\n", 678 | "'first' in d" 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": 41, 684 | "metadata": {}, 685 | "outputs": [], 686 | "source": [ 687 | "d = {} # empty dict\n", 688 | "d['a'] = 100 # added the key-value pair 'a':100" 689 | ] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": 42, 694 | "metadata": {}, 695 | "outputs": [ 696 | { 697 | "data": { 698 | "text/plain": [ 699 | "{'a': 100}" 700 | ] 701 | }, 702 | "execution_count": 42, 703 | "metadata": {}, 704 | "output_type": "execute_result" 705 | } 706 | ], 707 | "source": [ 708 | "d" 709 | ] 710 | }, 711 | { 712 | "cell_type": "code", 713 | "execution_count": 43, 714 | "metadata": {}, 715 | "outputs": [ 716 | { 717 | "data": { 718 | "text/plain": [ 719 | "{'a': 100, 'b': 200}" 720 | ] 721 | }, 722 | "execution_count": 43, 723 | "metadata": {}, 724 | "output_type": "execute_result" 725 | } 726 | ], 727 | "source": [ 728 | "d['b'] = 200\n", 729 | "d" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 45, 735 | "metadata": {}, 736 | "outputs": [ 737 | { 738 | "data": { 739 | "text/plain": [ 740 | "{'a': 999, 'b': 200}" 741 | ] 742 | }, 743 | "execution_count": 45, 744 | "metadata": {}, 745 | "output_type": "execute_result" 746 | } 747 | ], 748 | "source": [ 749 | "d['a'] = 999 # replace the value associated with 'a'\n", 750 | "d" 751 | ] 752 | }, 753 | { 754 | "cell_type": "code", 755 | "execution_count": 46, 756 | "metadata": {}, 757 | "outputs": [ 758 | { 759 | "data": { 760 | "text/plain": [ 761 | "{'a': 999, 'b': 200, 'x': 123, 'q': 456}" 762 | ] 763 | }, 764 | "execution_count": 46, 765 | "metadata": {}, 766 | "output_type": "execute_result" 767 | } 768 | ], 769 | "source": [ 770 | "d['x'] = 123\n", 771 | "d['q'] = 456\n", 772 | "d" 773 | ] 774 | }, 775 | { 776 | "cell_type": "code", 777 | "execution_count": 47, 778 | "metadata": {}, 779 | "outputs": [], 780 | "source": [ 781 | "d['r'] = [10, 20, 30]" 782 | ] 783 | }, 784 | { 785 | "cell_type": "code", 786 | "execution_count": 48, 787 | "metadata": {}, 788 | "outputs": [ 789 | { 790 | "data": { 791 | "text/plain": [ 792 | "{'a': 999, 'b': 200, 'x': 123, 'q': 456, 'r': [10, 20, 30]}" 793 | ] 794 | }, 795 | "execution_count": 48, 796 | "metadata": {}, 797 | "output_type": "execute_result" 798 | } 799 | ], 800 | "source": [ 801 | "d" 802 | ] 803 | }, 804 | { 805 | "cell_type": "code", 806 | "execution_count": 51, 807 | "metadata": {}, 808 | "outputs": [ 809 | { 810 | "data": { 811 | "text/plain": [ 812 | "{'a': 999, 'b': 200, 'x': 123, 'q': 456, 'r': [10, 20, 30]}" 813 | ] 814 | }, 815 | "execution_count": 51, 816 | "metadata": {}, 817 | "output_type": "execute_result" 818 | } 819 | ], 820 | "source": [ 821 | "d" 822 | ] 823 | }, 824 | { 825 | "cell_type": "code", 826 | "execution_count": 52, 827 | "metadata": {}, 828 | "outputs": [ 829 | { 830 | "data": { 831 | "text/plain": [ 832 | "999" 833 | ] 834 | }, 835 | "execution_count": 52, 836 | "metadata": {}, 837 | "output_type": "execute_result" 838 | } 839 | ], 840 | "source": [ 841 | "d.pop('a') # removes the key 'a', and returns its value" 842 | ] 843 | }, 844 | { 845 | "cell_type": "code", 846 | "execution_count": 53, 847 | "metadata": {}, 848 | "outputs": [ 849 | { 850 | "data": { 851 | "text/plain": [ 852 | "{'b': 200, 'x': 123, 'q': 456, 'r': [10, 20, 30]}" 853 | ] 854 | }, 855 | "execution_count": 53, 856 | "metadata": {}, 857 | "output_type": "execute_result" 858 | } 859 | ], 860 | "source": [ 861 | "d" 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": null, 867 | "metadata": {}, 868 | "outputs": [], 869 | "source": [] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": 54, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "country_codes = {'USA':1,\n", 878 | " 'Israel':972,\n", 879 | " 'UK':44}" 880 | ] 881 | }, 882 | { 883 | "cell_type": "code", 884 | "execution_count": 55, 885 | "metadata": {}, 886 | "outputs": [ 887 | { 888 | "name": "stdout", 889 | "output_type": "stream", 890 | "text": [ 891 | "Enter country name: Israel\n", 892 | "Code for Israel is 972\n", 893 | "Enter country name: France\n", 894 | "No info for country France\n", 895 | "Enter country name: \n" 896 | ] 897 | } 898 | ], 899 | "source": [ 900 | "while True:\n", 901 | " country_name = input(\"Enter country name: \").strip()\n", 902 | " \n", 903 | " if not country_name: # meaning: blank country name? exit the loop!\n", 904 | " break\n", 905 | " \n", 906 | " if country_name in country_codes:\n", 907 | " print(f'Code for {country_name} is {country_codes[country_name]}')\n", 908 | " \n", 909 | " else:\n", 910 | " print(f'No info for country {country_name}')" 911 | ] 912 | }, 913 | { 914 | "cell_type": "code", 915 | "execution_count": 56, 916 | "metadata": {}, 917 | "outputs": [ 918 | { 919 | "name": "stdout", 920 | "output_type": "stream", 921 | "text": [ 922 | "Enter a number: 10\n", 923 | "Enter a number: 11\n", 924 | "Enter a number: 12\n", 925 | "Enter a number: 14\n", 926 | "Enter a number: 16\n", 927 | "Enter a number: 18\n", 928 | "Enter a number: \n" 929 | ] 930 | } 931 | ], 932 | "source": [ 933 | "counts = {'evens':0,\n", 934 | " 'odds':0}\n", 935 | "\n", 936 | "while True:\n", 937 | " n = input(\"Enter a number: \").strip()\n", 938 | " \n", 939 | " if not n: # if empty/blank, then break\n", 940 | " break\n", 941 | " \n", 942 | " if not n.isdigit():\n", 943 | " print(f'{n} is not an integer. Try again.')\n", 944 | " continue\n", 945 | " \n", 946 | " n = int(n)\n", 947 | " \n", 948 | " if n % 2 == 0:\n", 949 | " counts['evens'] += 1\n", 950 | " else:\n", 951 | " counts['odds'] += 1" 952 | ] 953 | }, 954 | { 955 | "cell_type": "code", 956 | "execution_count": 57, 957 | "metadata": {}, 958 | "outputs": [ 959 | { 960 | "data": { 961 | "text/plain": [ 962 | "{'evens': 5, 'odds': 1}" 963 | ] 964 | }, 965 | "execution_count": 57, 966 | "metadata": {}, 967 | "output_type": "execute_result" 968 | } 969 | ], 970 | "source": [ 971 | "counts" 972 | ] 973 | }, 974 | { 975 | "cell_type": "code", 976 | "execution_count": 59, 977 | "metadata": {}, 978 | "outputs": [ 979 | { 980 | "data": { 981 | "text/plain": [ 982 | "True" 983 | ] 984 | }, 985 | "execution_count": 59, 986 | "metadata": {}, 987 | "output_type": "execute_result" 988 | } 989 | ], 990 | "source": [ 991 | "mylist = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\n", 992 | "\n", 993 | "50 in mylist # O(n) -- search speed is proportional to the lists's length\n", 994 | " # the longer the list, the longer you'll wait" 995 | ] 996 | }, 997 | { 998 | "cell_type": "code", 999 | "execution_count": 61, 1000 | "metadata": {}, 1001 | "outputs": [ 1002 | { 1003 | "data": { 1004 | "text/plain": [ 1005 | "True" 1006 | ] 1007 | }, 1008 | "execution_count": 61, 1009 | "metadata": {}, 1010 | "output_type": "execute_result" 1011 | } 1012 | ], 1013 | "source": [ 1014 | "d = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}\n", 1015 | "\n", 1016 | "'c' in d # O(1) -- constant time!\n", 1017 | " # no matter how big or small, it'll take the same time to find something in d" 1018 | ] 1019 | }, 1020 | { 1021 | "cell_type": "code", 1022 | "execution_count": 62, 1023 | "metadata": {}, 1024 | "outputs": [], 1025 | "source": [ 1026 | "# hash function makes it all possible!" 1027 | ] 1028 | }, 1029 | { 1030 | "cell_type": "code", 1031 | "execution_count": 63, 1032 | "metadata": {}, 1033 | "outputs": [], 1034 | "source": [ 1035 | "# in other languages, \"dictionaries\" are called:\n", 1036 | "# - hash tables\n", 1037 | "# - hashes\n", 1038 | "# - associative arrays\n", 1039 | "# - maps\n", 1040 | "# - hashmaps\n", 1041 | "# - (JavaScript) objects" 1042 | ] 1043 | }, 1044 | { 1045 | "cell_type": "code", 1046 | "execution_count": 64, 1047 | "metadata": {}, 1048 | "outputs": [], 1049 | "source": [ 1050 | "# A = 1\n", 1051 | "# B = 2\n", 1052 | "# C = 3\n", 1053 | "\n", 1054 | "# S = 19" 1055 | ] 1056 | }, 1057 | { 1058 | "cell_type": "code", 1059 | "execution_count": 65, 1060 | "metadata": {}, 1061 | "outputs": [ 1062 | { 1063 | "name": "stdout", 1064 | "output_type": "stream", 1065 | "text": [ 1066 | "Enter a string: this is a test\n", 1067 | "{'t': 3, 'h': 1, 'i': 2, 's': 3, ' ': 3, 'a': 1, 'e': 1}\n" 1068 | ] 1069 | } 1070 | ], 1071 | "source": [ 1072 | "counts = {}\n", 1073 | "\n", 1074 | "s = input(\"Enter a string: \").strip()\n", 1075 | "\n", 1076 | "for one_character in s:\n", 1077 | " if one_character in counts:\n", 1078 | " counts[one_character] += 1\n", 1079 | " else:\n", 1080 | " counts[one_character] = 1\n", 1081 | " \n", 1082 | "print(counts)\n", 1083 | " " 1084 | ] 1085 | }, 1086 | { 1087 | "cell_type": "code", 1088 | "execution_count": 67, 1089 | "metadata": {}, 1090 | "outputs": [ 1091 | { 1092 | "name": "stdout", 1093 | "output_type": "stream", 1094 | "text": [ 1095 | "t\n", 1096 | "h\n", 1097 | "i\n", 1098 | "s\n", 1099 | " \n", 1100 | "a\n", 1101 | "e\n" 1102 | ] 1103 | } 1104 | ], 1105 | "source": [ 1106 | "for one_item in counts: # iterating over a dict gives you the *keys*\n", 1107 | " print(one_item)" 1108 | ] 1109 | }, 1110 | { 1111 | "cell_type": "code", 1112 | "execution_count": 68, 1113 | "metadata": {}, 1114 | "outputs": [ 1115 | { 1116 | "name": "stdout", 1117 | "output_type": "stream", 1118 | "text": [ 1119 | "t: 3\n", 1120 | "h: 1\n", 1121 | "i: 2\n", 1122 | "s: 3\n", 1123 | " : 3\n", 1124 | "a: 1\n", 1125 | "e: 1\n" 1126 | ] 1127 | } 1128 | ], 1129 | "source": [ 1130 | "for key in counts:\n", 1131 | " print(f'{key}: {counts[key]}')" 1132 | ] 1133 | }, 1134 | { 1135 | "cell_type": "code", 1136 | "execution_count": 69, 1137 | "metadata": {}, 1138 | "outputs": [ 1139 | { 1140 | "name": "stdout", 1141 | "output_type": "stream", 1142 | "text": [ 1143 | "('t', 3)\n", 1144 | "('h', 1)\n", 1145 | "('i', 2)\n", 1146 | "('s', 3)\n", 1147 | "(' ', 3)\n", 1148 | "('a', 1)\n", 1149 | "('e', 1)\n" 1150 | ] 1151 | } 1152 | ], 1153 | "source": [ 1154 | "# the \"items\" method returns a key-value tuple with each iteration\n", 1155 | "for one_thing in counts.items():\n", 1156 | " print(one_thing)" 1157 | ] 1158 | }, 1159 | { 1160 | "cell_type": "code", 1161 | "execution_count": 70, 1162 | "metadata": {}, 1163 | "outputs": [ 1164 | { 1165 | "name": "stdout", 1166 | "output_type": "stream", 1167 | "text": [ 1168 | "t: 3\n", 1169 | "h: 1\n", 1170 | "i: 2\n", 1171 | "s: 3\n", 1172 | " : 3\n", 1173 | "a: 1\n", 1174 | "e: 1\n" 1175 | ] 1176 | } 1177 | ], 1178 | "source": [ 1179 | "for key, value in counts.items():\n", 1180 | " print(f'{key}: {value}')" 1181 | ] 1182 | }, 1183 | { 1184 | "cell_type": "code", 1185 | "execution_count": 71, 1186 | "metadata": {}, 1187 | "outputs": [ 1188 | { 1189 | "data": { 1190 | "text/plain": [ 1191 | "[' ', 'a', 'e', 'h', 'i', 's', 't']" 1192 | ] 1193 | }, 1194 | "execution_count": 71, 1195 | "metadata": {}, 1196 | "output_type": "execute_result" 1197 | } 1198 | ], 1199 | "source": [ 1200 | "sorted(counts)" 1201 | ] 1202 | }, 1203 | { 1204 | "cell_type": "code", 1205 | "execution_count": 72, 1206 | "metadata": {}, 1207 | "outputs": [], 1208 | "source": [ 1209 | "# https://www.youtube.com/watch?v=QR9W81P7yTw\n", 1210 | " \n", 1211 | "# FunctionDissectionLab.com" 1212 | ] 1213 | }, 1214 | { 1215 | "cell_type": "code", 1216 | "execution_count": null, 1217 | "metadata": {}, 1218 | "outputs": [], 1219 | "source": [] 1220 | }, 1221 | { 1222 | "cell_type": "code", 1223 | "execution_count": 74, 1224 | "metadata": {}, 1225 | "outputs": [ 1226 | { 1227 | "name": "stdout", 1228 | "output_type": "stream", 1229 | "text": [ 1230 | "Enter a sentence: this is test number 3 for my program\n", 1231 | "digits: 1\n", 1232 | "vowels: 8\n", 1233 | "other: 27\n" 1234 | ] 1235 | } 1236 | ], 1237 | "source": [ 1238 | "# ask the user to enter a sentence / words /whatever\n", 1239 | "# count how many (a) digits, (b) vowels, and (c) other characters are\n", 1240 | "# in the user's input\n", 1241 | "# keep track of them in a dict\n", 1242 | "# print each count when we're done\n", 1243 | "\n", 1244 | "\n", 1245 | "counts = {'digits':0,\n", 1246 | " 'vowels':0,\n", 1247 | " 'other':0}\n", 1248 | "\n", 1249 | "s = input(\"Enter a sentence: \").strip()\n", 1250 | "\n", 1251 | "for one_character in s:\n", 1252 | " if one_character.isdigit():\n", 1253 | " counts['digits'] += 1\n", 1254 | " elif one_character in 'aeiou':\n", 1255 | " counts['vowels'] += 1\n", 1256 | " else:\n", 1257 | " counts['other'] += 1\n", 1258 | " \n", 1259 | "for key, value in counts.items():\n", 1260 | " print(f'{key}: {value}')" 1261 | ] 1262 | }, 1263 | { 1264 | "cell_type": "code", 1265 | "execution_count": null, 1266 | "metadata": {}, 1267 | "outputs": [], 1268 | "source": [] 1269 | } 1270 | ], 1271 | "metadata": { 1272 | "kernelspec": { 1273 | "display_name": "Python 3", 1274 | "language": "python", 1275 | "name": "python3" 1276 | }, 1277 | "language_info": { 1278 | "codemirror_mode": { 1279 | "name": "ipython", 1280 | "version": 3 1281 | }, 1282 | "file_extension": ".py", 1283 | "mimetype": "text/x-python", 1284 | "name": "python", 1285 | "nbconvert_exporter": "python", 1286 | "pygments_lexer": "ipython3", 1287 | "version": "3.7.7" 1288 | } 1289 | }, 1290 | "nbformat": 4, 1291 | "nbformat_minor": 2 1292 | } 1293 | -------------------------------------------------------------------------------- /Non-programmers 2020 April 10.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# https://github.com/reuven/python-for-non-programmers" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Enter your birthyear: 1970\n", 22 | "This year, you will turn 50\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "# Ask the user to enter their birth year. \n", 28 | "# Tell the user how old they’ll turn this year.\n", 29 | "# (That avoids issues of whether the birthday has already passed.)\n", 30 | "\n", 31 | "birthyear = input(\"Enter your birthyear: \")\n", 32 | "age_this_year = 2020 - int(birthyear)\n", 33 | "\n", 34 | "print(f'This year, you will turn {age_this_year}')" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 3, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "You are middle aged, believe or not\n" 47 | ] 48 | } 49 | ], 50 | "source": [ 51 | "# Add to this an if statement that says they’re young, \n", 52 | "# old, or just right, based on their age — \n", 53 | "# and be gentle to those of us who aren’t as young as they used to be!\n", 54 | "\n", 55 | "if age_this_year < 18:\n", 56 | " print(f'You are very young!')\n", 57 | "elif age_this_year > 65:\n", 58 | " print(f'You are amazing')\n", 59 | "else:\n", 60 | " print(f'You are middle aged, believe or not')" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 8, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "How far did you run (m)? 10000\n", 73 | "How many minutes did it take you? 45\n", 74 | "You ran at a pace of 13333.333333333334 m/hour\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "# Imagine a world in which people can go \n", 80 | "# outside for a walk or a run. (\n", 81 | "# I know, we’re fantasizing here.) \n", 82 | "# Have the user enter a distance they walked or ran, \n", 83 | "# and how many minutes it took them to go that far. \n", 84 | "# Tell them how fast they walk/run in meters per hour \n", 85 | "# (or miles per hour, if you’re from that kind of country).\n", 86 | "# Note that the user is going to enter minutes,\n", 87 | "# and you have to produce output per hour.\n", 88 | "\n", 89 | "distance = input(\"How far did you run (m)? \")\n", 90 | "minutes = input(\"How many minutes did it take you? \")\n", 91 | "\n", 92 | "hours = float(minutes) / 60\n", 93 | "\n", 94 | "pace = float(distance) / hours\n", 95 | "\n", 96 | "print(f'You ran at a pace of {pace} m/hour')" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 9, 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "13333.333333333334" 108 | ] 109 | }, 110 | "execution_count": 9, 111 | "metadata": {}, 112 | "output_type": "execute_result" 113 | } 114 | ], 115 | "source": [ 116 | "pace" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 10, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "13333" 128 | ] 129 | }, 130 | "execution_count": 10, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "round(pace)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 11, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "10" 148 | ] 149 | }, 150 | "execution_count": 11, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "round(10.5)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 12, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "11" 168 | ] 169 | }, 170 | "execution_count": 12, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "round(10.6)" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 13, 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "Help on built-in function round in module builtins:\n", 189 | "\n", 190 | "round(number, ndigits=None)\n", 191 | " Round a number to a given precision in decimal digits.\n", 192 | " \n", 193 | " The return value is an integer if ndigits is omitted or None. Otherwise\n", 194 | " the return value has the same type as the number. ndigits may be negative.\n", 195 | "\n" 196 | ] 197 | } 198 | ], 199 | "source": [ 200 | "help(round)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 14, 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "s = 'abcdefghijklmnopqrstuvwxyz'\n" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 15, 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "data": { 219 | "text/plain": [ 220 | "str" 221 | ] 222 | }, 223 | "execution_count": 15, 224 | "metadata": {}, 225 | "output_type": "execute_result" 226 | } 227 | ], 228 | "source": [ 229 | "type(s) # what type of value is stored in the variable s?" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 16, 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "data": { 239 | "text/plain": [ 240 | "int" 241 | ] 242 | }, 243 | "execution_count": 16, 244 | "metadata": {}, 245 | "output_type": "execute_result" 246 | } 247 | ], 248 | "source": [ 249 | "x = 100 # assign the value 100 to the variable x\n", 250 | "type(x) # what type of value is stored in the variable x?" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 17, 256 | "metadata": {}, 257 | "outputs": [], 258 | "source": [ 259 | "x = 'abcd' # now, assign 'abcd' -- a string! -- to x" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 18, 265 | "metadata": {}, 266 | "outputs": [ 267 | { 268 | "data": { 269 | "text/plain": [ 270 | "str" 271 | ] 272 | }, 273 | "execution_count": 18, 274 | "metadata": {}, 275 | "output_type": "execute_result" 276 | } 277 | ], 278 | "source": [ 279 | "type(x)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 19, 285 | "metadata": {}, 286 | "outputs": [ 287 | { 288 | "name": "stdout", 289 | "output_type": "stream", 290 | "text": [ 291 | "abcdefghijklmnopqrstuvwxyz\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "print(s)" 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 20, 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "data": { 306 | "text/plain": [ 307 | "26" 308 | ] 309 | }, 310 | "execution_count": 20, 311 | "metadata": {}, 312 | "output_type": "execute_result" 313 | } 314 | ], 315 | "source": [ 316 | "# get the length of the string\n", 317 | "len(s) # returns an integer" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 21, 323 | "metadata": {}, 324 | "outputs": [ 325 | { 326 | "data": { 327 | "text/plain": [ 328 | "'a'" 329 | ] 330 | }, 331 | "execution_count": 21, 332 | "metadata": {}, 333 | "output_type": "execute_result" 334 | } 335 | ], 336 | "source": [ 337 | "# retrieve the first letter from s\n", 338 | "# use square brackets, and put the index we want in the brackets\n", 339 | "s[0]" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 22, 345 | "metadata": {}, 346 | "outputs": [ 347 | { 348 | "data": { 349 | "text/plain": [ 350 | "'k'" 351 | ] 352 | }, 353 | "execution_count": 22, 354 | "metadata": {}, 355 | "output_type": "execute_result" 356 | } 357 | ], 358 | "source": [ 359 | "s[10] # gives me the 11th letter" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 23, 365 | "metadata": {}, 366 | "outputs": [ 367 | { 368 | "data": { 369 | "text/plain": [ 370 | "'u'" 371 | ] 372 | }, 373 | "execution_count": 23, 374 | "metadata": {}, 375 | "output_type": "execute_result" 376 | } 377 | ], 378 | "source": [ 379 | "s[20] # gives me the 21st letter" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 24, 385 | "metadata": {}, 386 | "outputs": [ 387 | { 388 | "data": { 389 | "text/plain": [ 390 | "'z'" 391 | ] 392 | }, 393 | "execution_count": 24, 394 | "metadata": {}, 395 | "output_type": "execute_result" 396 | } 397 | ], 398 | "source": [ 399 | "s[25] # gives me the final letter" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 25, 405 | "metadata": {}, 406 | "outputs": [ 407 | { 408 | "ename": "IndexError", 409 | "evalue": "string index out of range", 410 | "output_type": "error", 411 | "traceback": [ 412 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 413 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 414 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 415 | "\u001b[0;31mIndexError\u001b[0m: string index out of range" 416 | ] 417 | } 418 | ], 419 | "source": [ 420 | "s[100]" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 26, 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "data": { 430 | "text/plain": [ 431 | "'abcdefghijklmnopqrstuvwxyz'" 432 | ] 433 | }, 434 | "execution_count": 26, 435 | "metadata": {}, 436 | "output_type": "execute_result" 437 | } 438 | ], 439 | "source": [ 440 | "s" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": 27, 446 | "metadata": {}, 447 | "outputs": [ 448 | { 449 | "data": { 450 | "text/plain": [ 451 | "'klmnopqrst'" 452 | ] 453 | }, 454 | "execution_count": 27, 455 | "metadata": {}, 456 | "output_type": "execute_result" 457 | } 458 | ], 459 | "source": [ 460 | "# I want a bunch of letters from s\n", 461 | "# I want everything from index 10 to index 20\n", 462 | "\n", 463 | "# in Python, I can ask for a \"slice\"\n", 464 | "s[10:20] # returns the \"substring\" from index 10 to 20 (not including 20)" 465 | ] 466 | }, 467 | { 468 | "cell_type": "code", 469 | "execution_count": 28, 470 | "metadata": {}, 471 | "outputs": [ 472 | { 473 | "data": { 474 | "text/plain": [ 475 | "'abcdefghij'" 476 | ] 477 | }, 478 | "execution_count": 28, 479 | "metadata": {}, 480 | "output_type": "execute_result" 481 | } 482 | ], 483 | "source": [ 484 | "s[:10] # from the start to (not including) index 10" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": 29, 490 | "metadata": {}, 491 | "outputs": [ 492 | { 493 | "data": { 494 | "text/plain": [ 495 | "'uvwxyz'" 496 | ] 497 | }, 498 | "execution_count": 29, 499 | "metadata": {}, 500 | "output_type": "execute_result" 501 | } 502 | ], 503 | "source": [ 504 | "s[20:] # from index 20 to the end" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 30, 510 | "metadata": {}, 511 | "outputs": [ 512 | { 513 | "data": { 514 | "text/plain": [ 515 | "'klmnopqrst'" 516 | ] 517 | }, 518 | "execution_count": 30, 519 | "metadata": {}, 520 | "output_type": "execute_result" 521 | } 522 | ], 523 | "source": [ 524 | "# I want every other letter from 10 to 20\n", 525 | "# Use a three-part slice: start:end:step\n", 526 | "\n", 527 | "s[10:20:1] # the default -- step size of 1" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 31, 533 | "metadata": {}, 534 | "outputs": [ 535 | { 536 | "data": { 537 | "text/plain": [ 538 | "'kmoqs'" 539 | ] 540 | }, 541 | "execution_count": 31, 542 | "metadata": {}, 543 | "output_type": "execute_result" 544 | } 545 | ], 546 | "source": [ 547 | "s[10:20:2] # step size of 2" 548 | ] 549 | }, 550 | { 551 | "cell_type": "code", 552 | "execution_count": 32, 553 | "metadata": {}, 554 | "outputs": [ 555 | { 556 | "data": { 557 | "text/plain": [ 558 | "'acegikmoqsuwy'" 559 | ] 560 | }, 561 | "execution_count": 32, 562 | "metadata": {}, 563 | "output_type": "execute_result" 564 | } 565 | ], 566 | "source": [ 567 | "# if I want every other letter in all of s\n", 568 | "s[::2] # from the start, to the end, step size of 2" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "execution_count": 33, 574 | "metadata": {}, 575 | "outputs": [ 576 | { 577 | "data": { 578 | "text/plain": [ 579 | "'abcdefghijklmnopqrstuvwxyz'" 580 | ] 581 | }, 582 | "execution_count": 33, 583 | "metadata": {}, 584 | "output_type": "execute_result" 585 | } 586 | ], 587 | "source": [ 588 | "s" 589 | ] 590 | }, 591 | { 592 | "cell_type": "code", 593 | "execution_count": 34, 594 | "metadata": {}, 595 | "outputs": [], 596 | "source": [ 597 | "s = 'abcdefghijklmnopqrstuvwxyz'" 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": 35, 603 | "metadata": {}, 604 | "outputs": [ 605 | { 606 | "data": { 607 | "text/plain": [ 608 | "'ue'" 609 | ] 610 | }, 611 | "execution_count": 35, 612 | "metadata": {}, 613 | "output_type": "execute_result" 614 | } 615 | ], 616 | "source": [ 617 | "s = 'Reuven'\n", 618 | "s[2:5:2]" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": 36, 624 | "metadata": {}, 625 | "outputs": [ 626 | { 627 | "data": { 628 | "text/plain": [ 629 | "'Reuven'" 630 | ] 631 | }, 632 | "execution_count": 36, 633 | "metadata": {}, 634 | "output_type": "execute_result" 635 | } 636 | ], 637 | "source": [ 638 | "s" 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "execution_count": 37, 644 | "metadata": {}, 645 | "outputs": [ 646 | { 647 | "data": { 648 | "text/plain": [ 649 | "'R'" 650 | ] 651 | }, 652 | "execution_count": 37, 653 | "metadata": {}, 654 | "output_type": "execute_result" 655 | } 656 | ], 657 | "source": [ 658 | "s[0]" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 38, 664 | "metadata": {}, 665 | "outputs": [ 666 | { 667 | "data": { 668 | "text/plain": [ 669 | "'e'" 670 | ] 671 | }, 672 | "execution_count": 38, 673 | "metadata": {}, 674 | "output_type": "execute_result" 675 | } 676 | ], 677 | "source": [ 678 | "s[1]" 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": 39, 684 | "metadata": {}, 685 | "outputs": [ 686 | { 687 | "data": { 688 | "text/plain": [ 689 | "'Reuven'" 690 | ] 691 | }, 692 | "execution_count": 39, 693 | "metadata": {}, 694 | "output_type": "execute_result" 695 | } 696 | ], 697 | "source": [ 698 | "s" 699 | ] 700 | }, 701 | { 702 | "cell_type": "code", 703 | "execution_count": 40, 704 | "metadata": {}, 705 | "outputs": [ 706 | { 707 | "ename": "TypeError", 708 | "evalue": "'str' object does not support item assignment", 709 | "output_type": "error", 710 | "traceback": [ 711 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 712 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 713 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'Q'\u001b[0m \u001b[0;31m# replace the first letter of s with 'Q'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 714 | "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" 715 | ] 716 | } 717 | ], 718 | "source": [ 719 | "s[0] = 'Q' # replace the first letter of s with 'Q'" 720 | ] 721 | }, 722 | { 723 | "cell_type": "code", 724 | "execution_count": 41, 725 | "metadata": {}, 726 | "outputs": [], 727 | "source": [ 728 | "# strings are immutable \n", 729 | "# once created, they can NEVER EVER EVER be changed\n" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 42, 735 | "metadata": {}, 736 | "outputs": [ 737 | { 738 | "data": { 739 | "text/plain": [ 740 | "'Reuven'" 741 | ] 742 | }, 743 | "execution_count": 42, 744 | "metadata": {}, 745 | "output_type": "execute_result" 746 | } 747 | ], 748 | "source": [ 749 | "s" 750 | ] 751 | }, 752 | { 753 | "cell_type": "code", 754 | "execution_count": 43, 755 | "metadata": {}, 756 | "outputs": [], 757 | "source": [ 758 | "s = 'abcd'" 759 | ] 760 | }, 761 | { 762 | "cell_type": "code", 763 | "execution_count": 44, 764 | "metadata": {}, 765 | "outputs": [], 766 | "source": [ 767 | "s = 'abcd'" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": 45, 773 | "metadata": {}, 774 | "outputs": [], 775 | "source": [ 776 | "s = 'efgh'" 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": 46, 782 | "metadata": {}, 783 | "outputs": [ 784 | { 785 | "data": { 786 | "text/plain": [ 787 | "'efgh'" 788 | ] 789 | }, 790 | "execution_count": 46, 791 | "metadata": {}, 792 | "output_type": "execute_result" 793 | } 794 | ], 795 | "source": [ 796 | "s" 797 | ] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "execution_count": 47, 802 | "metadata": {}, 803 | "outputs": [ 804 | { 805 | "name": "stdout", 806 | "output_type": "stream", 807 | "text": [ 808 | "efgh\n" 809 | ] 810 | } 811 | ], 812 | "source": [ 813 | "print(s)" 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "execution_count": 48, 819 | "metadata": {}, 820 | "outputs": [], 821 | "source": [ 822 | "# immutable ≠ constant" 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "execution_count": 49, 828 | "metadata": {}, 829 | "outputs": [ 830 | { 831 | "data": { 832 | "text/plain": [ 833 | "'abcdabcd'" 834 | ] 835 | }, 836 | "execution_count": 49, 837 | "metadata": {}, 838 | "output_type": "execute_result" 839 | } 840 | ], 841 | "source": [ 842 | "s = 'abcd'\n", 843 | "s = s + s\n", 844 | "\n", 845 | "s" 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "execution_count": 50, 851 | "metadata": {}, 852 | "outputs": [ 853 | { 854 | "name": "stdout", 855 | "output_type": "stream", 856 | "text": [ 857 | "10\n" 858 | ] 859 | } 860 | ], 861 | "source": [ 862 | "x = 10\n", 863 | "print(x)" 864 | ] 865 | }, 866 | { 867 | "cell_type": "code", 868 | "execution_count": 51, 869 | "metadata": {}, 870 | "outputs": [ 871 | { 872 | "name": "stdout", 873 | "output_type": "stream", 874 | "text": [ 875 | "20\n" 876 | ] 877 | } 878 | ], 879 | "source": [ 880 | "x = 20\n", 881 | "print(x)" 882 | ] 883 | }, 884 | { 885 | "cell_type": "code", 886 | "execution_count": 52, 887 | "metadata": {}, 888 | "outputs": [ 889 | { 890 | "name": "stdout", 891 | "output_type": "stream", 892 | "text": [ 893 | "abcd\n" 894 | ] 895 | } 896 | ], 897 | "source": [ 898 | "x = 'abcd'\n", 899 | "print(x)\n", 900 | "\n" 901 | ] 902 | }, 903 | { 904 | "cell_type": "code", 905 | "execution_count": 53, 906 | "metadata": {}, 907 | "outputs": [ 908 | { 909 | "name": "stdout", 910 | "output_type": "stream", 911 | "text": [ 912 | "efgh\n" 913 | ] 914 | } 915 | ], 916 | "source": [ 917 | "x = 'efgh'\n", 918 | "print(x)" 919 | ] 920 | }, 921 | { 922 | "cell_type": "code", 923 | "execution_count": 54, 924 | "metadata": {}, 925 | "outputs": [], 926 | "source": [ 927 | "x = 10\n", 928 | "x = 20\n" 929 | ] 930 | }, 931 | { 932 | "cell_type": "code", 933 | "execution_count": 55, 934 | "metadata": {}, 935 | "outputs": [ 936 | { 937 | "name": "stdout", 938 | "output_type": "stream", 939 | "text": [ 940 | "20\n" 941 | ] 942 | } 943 | ], 944 | "source": [ 945 | "print(x)" 946 | ] 947 | }, 948 | { 949 | "cell_type": "code", 950 | "execution_count": 56, 951 | "metadata": {}, 952 | "outputs": [ 953 | { 954 | "name": "stdout", 955 | "output_type": "stream", 956 | "text": [ 957 | "abcde\n" 958 | ] 959 | } 960 | ], 961 | "source": [ 962 | "x = 'abcde'\n", 963 | "y = x # doesn't mean: y follows x wherever it goes!\n", 964 | "\n", 965 | "x = 'fgh'\n", 966 | "print(y)" 967 | ] 968 | }, 969 | { 970 | "cell_type": "code", 971 | "execution_count": 57, 972 | "metadata": {}, 973 | "outputs": [ 974 | { 975 | "data": { 976 | "text/plain": [ 977 | "'abcdabcd'" 978 | ] 979 | }, 980 | "execution_count": 57, 981 | "metadata": {}, 982 | "output_type": "execute_result" 983 | } 984 | ], 985 | "source": [ 986 | "s" 987 | ] 988 | }, 989 | { 990 | "cell_type": "code", 991 | "execution_count": 58, 992 | "metadata": {}, 993 | "outputs": [ 994 | { 995 | "data": { 996 | "text/plain": [ 997 | "26" 998 | ] 999 | }, 1000 | "execution_count": 58, 1001 | "metadata": {}, 1002 | "output_type": "execute_result" 1003 | } 1004 | ], 1005 | "source": [ 1006 | "s = 'abcdefghijklmnopqrstuvwxyz'\n", 1007 | "len(s)" 1008 | ] 1009 | }, 1010 | { 1011 | "cell_type": "code", 1012 | "execution_count": 59, 1013 | "metadata": {}, 1014 | "outputs": [ 1015 | { 1016 | "data": { 1017 | "text/plain": [ 1018 | "True" 1019 | ] 1020 | }, 1021 | "execution_count": 59, 1022 | "metadata": {}, 1023 | "output_type": "execute_result" 1024 | } 1025 | ], 1026 | "source": [ 1027 | "# is the letter 'q' in s?\n", 1028 | "\n", 1029 | "'q' in s # returns True or False" 1030 | ] 1031 | }, 1032 | { 1033 | "cell_type": "code", 1034 | "execution_count": 60, 1035 | "metadata": {}, 1036 | "outputs": [ 1037 | { 1038 | "data": { 1039 | "text/plain": [ 1040 | "True" 1041 | ] 1042 | }, 1043 | "execution_count": 60, 1044 | "metadata": {}, 1045 | "output_type": "execute_result" 1046 | } 1047 | ], 1048 | "source": [ 1049 | "'qrs' in s" 1050 | ] 1051 | }, 1052 | { 1053 | "cell_type": "code", 1054 | "execution_count": 61, 1055 | "metadata": {}, 1056 | "outputs": [ 1057 | { 1058 | "data": { 1059 | "text/plain": [ 1060 | "False" 1061 | ] 1062 | }, 1063 | "execution_count": 61, 1064 | "metadata": {}, 1065 | "output_type": "execute_result" 1066 | } 1067 | ], 1068 | "source": [ 1069 | "'qs' in s " 1070 | ] 1071 | }, 1072 | { 1073 | "cell_type": "code", 1074 | "execution_count": 64, 1075 | "metadata": {}, 1076 | "outputs": [ 1077 | { 1078 | "name": "stdout", 1079 | "output_type": "stream", 1080 | "text": [ 1081 | "Enter your name: Reuven\n", 1082 | "Enter a letter: x\n" 1083 | ] 1084 | } 1085 | ], 1086 | "source": [ 1087 | "name = input(\"Enter your name: \")\n", 1088 | "letter = input(\"Enter a letter: \")" 1089 | ] 1090 | }, 1091 | { 1092 | "cell_type": "code", 1093 | "execution_count": 65, 1094 | "metadata": {}, 1095 | "outputs": [ 1096 | { 1097 | "name": "stdout", 1098 | "output_type": "stream", 1099 | "text": [ 1100 | "Sorry, I did not find x in Reuven\n" 1101 | ] 1102 | } 1103 | ], 1104 | "source": [ 1105 | "if letter in name:\n", 1106 | " print(f'Yes, I found {letter} in {name}!')\n", 1107 | "else:\n", 1108 | " print(f'Sorry, I did not find {letter} in {name}')" 1109 | ] 1110 | }, 1111 | { 1112 | "cell_type": "code", 1113 | "execution_count": 66, 1114 | "metadata": {}, 1115 | "outputs": [ 1116 | { 1117 | "data": { 1118 | "text/plain": [ 1119 | "True" 1120 | ] 1121 | }, 1122 | "execution_count": 66, 1123 | "metadata": {}, 1124 | "output_type": "execute_result" 1125 | } 1126 | ], 1127 | "source": [ 1128 | "# Python is case sensitive!\n", 1129 | "\n", 1130 | "'v' in name" 1131 | ] 1132 | }, 1133 | { 1134 | "cell_type": "code", 1135 | "execution_count": 67, 1136 | "metadata": {}, 1137 | "outputs": [ 1138 | { 1139 | "data": { 1140 | "text/plain": [ 1141 | "False" 1142 | ] 1143 | }, 1144 | "execution_count": 67, 1145 | "metadata": {}, 1146 | "output_type": "execute_result" 1147 | } 1148 | ], 1149 | "source": [ 1150 | "'V' in name" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "execution_count": 69, 1156 | "metadata": {}, 1157 | "outputs": [ 1158 | { 1159 | "name": "stdout", 1160 | "output_type": "stream", 1161 | "text": [ 1162 | "Enter your name: Reuven\n", 1163 | "Enter a letter: S\n", 1164 | "No, your name starts with R, not with S\n" 1165 | ] 1166 | } 1167 | ], 1168 | "source": [ 1169 | "name = input(\"Enter your name: \")\n", 1170 | "letter = input(\"Enter a letter: \")\n", 1171 | "\n", 1172 | "if letter == name[0]:\n", 1173 | " print(f\"Yes, your name {name} starts with {letter}\")\n", 1174 | "else:\n", 1175 | " print(f'No, your name starts with {name[0]}, not with {letter}')" 1176 | ] 1177 | }, 1178 | { 1179 | "cell_type": "code", 1180 | "execution_count": 70, 1181 | "metadata": {}, 1182 | "outputs": [ 1183 | { 1184 | "data": { 1185 | "text/plain": [ 1186 | "'Reuven'" 1187 | ] 1188 | }, 1189 | "execution_count": 70, 1190 | "metadata": {}, 1191 | "output_type": "execute_result" 1192 | } 1193 | ], 1194 | "source": [ 1195 | "name" 1196 | ] 1197 | }, 1198 | { 1199 | "cell_type": "code", 1200 | "execution_count": 71, 1201 | "metadata": { 1202 | "scrolled": true 1203 | }, 1204 | "outputs": [ 1205 | { 1206 | "data": { 1207 | "text/plain": [ 1208 | "'n'" 1209 | ] 1210 | }, 1211 | "execution_count": 71, 1212 | "metadata": {}, 1213 | "output_type": "execute_result" 1214 | } 1215 | ], 1216 | "source": [ 1217 | "name[len(name)-1]" 1218 | ] 1219 | }, 1220 | { 1221 | "cell_type": "code", 1222 | "execution_count": 72, 1223 | "metadata": {}, 1224 | "outputs": [ 1225 | { 1226 | "data": { 1227 | "text/plain": [ 1228 | "'n'" 1229 | ] 1230 | }, 1231 | "execution_count": 72, 1232 | "metadata": {}, 1233 | "output_type": "execute_result" 1234 | } 1235 | ], 1236 | "source": [ 1237 | "# cool Python trick -- negative indexes!\n", 1238 | "name[-1] # means: read the 1st character from THE RIGHT SIDE" 1239 | ] 1240 | }, 1241 | { 1242 | "cell_type": "code", 1243 | "execution_count": 73, 1244 | "metadata": {}, 1245 | "outputs": [ 1246 | { 1247 | "data": { 1248 | "text/plain": [ 1249 | "'e'" 1250 | ] 1251 | }, 1252 | "execution_count": 73, 1253 | "metadata": {}, 1254 | "output_type": "execute_result" 1255 | } 1256 | ], 1257 | "source": [ 1258 | "name[-2]" 1259 | ] 1260 | }, 1261 | { 1262 | "cell_type": "code", 1263 | "execution_count": 78, 1264 | "metadata": {}, 1265 | "outputs": [ 1266 | { 1267 | "name": "stdout", 1268 | "output_type": "stream", 1269 | "text": [ 1270 | "Enter your name: Atara\n" 1271 | ] 1272 | } 1273 | ], 1274 | "source": [ 1275 | "# Ask the user to enter their name\n", 1276 | "# Indicate if their name ends with a vowel ('a', 'e', 'i', 'o', or 'u')\n", 1277 | "\n", 1278 | "# Hint 1: Check the last name with a negative index\n", 1279 | "# Hint 2: Use \"in\" to find out if the letter is a vowel\n", 1280 | "\n", 1281 | "name = input(\"Enter your name: \")\n", 1282 | "\n" 1283 | ] 1284 | }, 1285 | { 1286 | "cell_type": "code", 1287 | "execution_count": 79, 1288 | "metadata": {}, 1289 | "outputs": [ 1290 | { 1291 | "name": "stdout", 1292 | "output_type": "stream", 1293 | "text": [ 1294 | "Yes, your name ends with a vowel!\n" 1295 | ] 1296 | } 1297 | ], 1298 | "source": [ 1299 | "if name[-1] == 'a' or name[-1] == 'e' or name[-1] == 'i' or name[-1] == 'o' or name[-1] == 'u':\n", 1300 | " print(\"Yes, your name ends with a vowel!\")\n", 1301 | "else:\n", 1302 | " print(f'Your name {name} does not end with a vowel')" 1303 | ] 1304 | }, 1305 | { 1306 | "cell_type": "code", 1307 | "execution_count": 80, 1308 | "metadata": {}, 1309 | "outputs": [ 1310 | { 1311 | "name": "stdout", 1312 | "output_type": "stream", 1313 | "text": [ 1314 | "Yes, your name ends with a vowel!\n" 1315 | ] 1316 | } 1317 | ], 1318 | "source": [ 1319 | "if name[-1] in 'aeiou':\n", 1320 | " print(\"Yes, your name ends with a vowel!\")\n", 1321 | "else:\n", 1322 | " print(f'Your name {name} does not end with a vowel') " 1323 | ] 1324 | }, 1325 | { 1326 | "cell_type": "code", 1327 | "execution_count": 81, 1328 | "metadata": {}, 1329 | "outputs": [], 1330 | "source": [ 1331 | "# (1) Defining strings -- with either '' or \"\"\n", 1332 | "# (2) Retrieve one item from a string using []\n", 1333 | "# e.g., s[5]\n", 1334 | "# (3) Indexes start at 0, negative indexes go from the right\n", 1335 | "# (4) Slices are of the form start:end\n", 1336 | "# e.g., s[3:5] -- note that it's until (not including) the end\n", 1337 | "# (5) We can use \"in\" to check if a string is in another string\n", 1338 | "# e.g., 'x' in 'abcxyz' # it's always LITTLE in BIG\n", 1339 | "# (6) Strings are immutable -- never be changed" 1340 | ] 1341 | }, 1342 | { 1343 | "cell_type": "code", 1344 | "execution_count": null, 1345 | "metadata": {}, 1346 | "outputs": [], 1347 | "source": [] 1348 | } 1349 | ], 1350 | "metadata": { 1351 | "kernelspec": { 1352 | "display_name": "Python 3", 1353 | "language": "python", 1354 | "name": "python3" 1355 | }, 1356 | "language_info": { 1357 | "codemirror_mode": { 1358 | "name": "ipython", 1359 | "version": 3 1360 | }, 1361 | "file_extension": ".py", 1362 | "mimetype": "text/x-python", 1363 | "name": "python", 1364 | "nbconvert_exporter": "python", 1365 | "pygments_lexer": "ipython3", 1366 | "version": "3.7.7" 1367 | } 1368 | }, 1369 | "nbformat": 4, 1370 | "nbformat_minor": 2 1371 | } 1372 | -------------------------------------------------------------------------------- /Non-programmers 2020 March 20.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Hello, world!!!!!\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "# this is a comment!\n", 18 | "\n", 19 | "print(\"Hello, world!!!!!\") # shift + enter to execute the cell" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 3, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "# jupyter " 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 4, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "Hello, Reuven!\n" 41 | ] 42 | } 43 | ], 44 | "source": [ 45 | "print(\"Hello, Reuven!\")" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 5, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "name": "stdout", 55 | "output_type": "stream", 56 | "text": [ 57 | "Reuven\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "print(\"Reuven\")" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 7, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "# to create a new varible, assign a value to it\n", 72 | "name = 'Reuven' # shift + enter executes + creates a new one" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 8, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "Reuven\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "print('Reuven') " 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 9, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "name = 'someone else'" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 10, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "someone else\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "print(name)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 11, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "name": "stdout", 125 | "output_type": "stream", 126 | "text": [ 127 | "whatever you want\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "name = 'whatever you want'\n", 133 | "print(name)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 12, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "100\n" 146 | ] 147 | } 148 | ], 149 | "source": [ 150 | "x = 100\n", 151 | "print(x)" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 13, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "300\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "x = 100\n", 169 | "y = 200\n", 170 | "\n", 171 | "print(x + y) # shift + enter to execute the cell" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 14, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "600\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "print(x + x + y + y)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 15, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "data": { 198 | "text/plain": [ 199 | "300" 200 | ] 201 | }, 202 | "execution_count": 15, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "x + y" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 17, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "100" 220 | ] 221 | }, 222 | "execution_count": 17, 223 | "metadata": {}, 224 | "output_type": "execute_result" 225 | } 226 | ], 227 | "source": [ 228 | "x" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 18, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "200" 240 | ] 241 | }, 242 | "execution_count": 18, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "y" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 20, 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "data": { 258 | "text/plain": [ 259 | "200" 260 | ] 261 | }, 262 | "execution_count": 20, 263 | "metadata": {}, 264 | "output_type": "execute_result" 265 | } 266 | ], 267 | "source": [ 268 | "x # expression by itself -- not printed -- not last line -- ignored\n", 269 | "y # expression by itself -- on the last line -- we get its value" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 21, 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "ename": "NameError", 279 | "evalue": "name 's' is not defined", 280 | "output_type": "error", 281 | "traceback": [ 282 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 283 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 284 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 285 | "\u001b[0;31mNameError\u001b[0m: name 's' is not defined" 286 | ] 287 | } 288 | ], 289 | "source": [ 290 | "s" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 22, 296 | "metadata": {}, 297 | "outputs": [ 298 | { 299 | "data": { 300 | "text/plain": [ 301 | "300" 302 | ] 303 | }, 304 | "execution_count": 22, 305 | "metadata": {}, 306 | "output_type": "execute_result" 307 | } 308 | ], 309 | "source": [ 310 | "x = 100\n", 311 | "y = 200\n", 312 | "\n", 313 | "x + y " 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 23, 319 | "metadata": {}, 320 | "outputs": [ 321 | { 322 | "data": { 323 | "text/plain": [ 324 | "'100200'" 325 | ] 326 | }, 327 | "execution_count": 23, 328 | "metadata": {}, 329 | "output_type": "execute_result" 330 | } 331 | ], 332 | "source": [ 333 | "x = '100'\n", 334 | "y = '200'\n", 335 | "\n", 336 | "x + y" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 24, 342 | "metadata": {}, 343 | "outputs": [ 344 | { 345 | "ename": "TypeError", 346 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 347 | "output_type": "error", 348 | "traceback": [ 349 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 350 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 351 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'200'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 352 | "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 353 | ] 354 | } 355 | ], 356 | "source": [ 357 | "x = 100\n", 358 | "y = '200'\n", 359 | "\n", 360 | "x + y" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 25, 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "name": "stdout", 370 | "output_type": "stream", 371 | "text": [ 372 | "Reuven\n" 373 | ] 374 | } 375 | ], 376 | "source": [ 377 | "name = 'Reuven'\n", 378 | "print(name)" 379 | ] 380 | }, 381 | { 382 | "cell_type": "code", 383 | "execution_count": 26, 384 | "metadata": {}, 385 | "outputs": [ 386 | { 387 | "name": "stdout", 388 | "output_type": "stream", 389 | "text": [ 390 | "name\n" 391 | ] 392 | } 393 | ], 394 | "source": [ 395 | "print('name')" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 27, 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "Hello, Reuven\n" 408 | ] 409 | } 410 | ], 411 | "source": [ 412 | "name = 'Reuven'\n", 413 | "print('Hello, ' + name)" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 28, 419 | "metadata": {}, 420 | "outputs": [ 421 | { 422 | "name": "stdout", 423 | "output_type": "stream", 424 | "text": [ 425 | "Hello,Reuven\n" 426 | ] 427 | } 428 | ], 429 | "source": [ 430 | "print('Hello,' + name)" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 29, 436 | "metadata": {}, 437 | "outputs": [ 438 | { 439 | "name": "stdout", 440 | "output_type": "stream", 441 | "text": [ 442 | "Hello, Reuven\n" 443 | ] 444 | } 445 | ], 446 | "source": [ 447 | "print('Hello, ' + name)" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 31, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "data": { 457 | "text/plain": [ 458 | "300" 459 | ] 460 | }, 461 | "execution_count": 31, 462 | "metadata": {}, 463 | "output_type": "execute_result" 464 | } 465 | ], 466 | "source": [ 467 | "100+200" 468 | ] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "execution_count": 32, 473 | "metadata": {}, 474 | "outputs": [ 475 | { 476 | "name": "stdout", 477 | "output_type": "stream", 478 | "text": [ 479 | "Enter your name: Reuven\n", 480 | "Hello, Reuven\n" 481 | ] 482 | } 483 | ], 484 | "source": [ 485 | "name = input('Enter your name: ')\n", 486 | "print('Hello, ' + name)" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 33, 492 | "metadata": {}, 493 | "outputs": [ 494 | { 495 | "ename": "SyntaxError", 496 | "evalue": "can't assign to function call (, line 3)", 497 | "output_type": "error", 498 | "traceback": [ 499 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m input('Enter your name: ') = name\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m can't assign to function call\n" 500 | ] 501 | } 502 | ], 503 | "source": [ 504 | "# VARIABLE = VALUE\n", 505 | "\n", 506 | "input('Enter your name: ') = name" 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "execution_count": 34, 512 | "metadata": {}, 513 | "outputs": [], 514 | "source": [ 515 | "x = 100" 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "execution_count": 35, 521 | "metadata": {}, 522 | "outputs": [], 523 | "source": [ 524 | "x = 100 + 200" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 36, 530 | "metadata": {}, 531 | "outputs": [], 532 | "source": [ 533 | "x = 100" 534 | ] 535 | }, 536 | { 537 | "cell_type": "code", 538 | "execution_count": 37, 539 | "metadata": {}, 540 | "outputs": [], 541 | "source": [ 542 | "x = 'abcd'" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 38, 548 | "metadata": {}, 549 | "outputs": [ 550 | { 551 | "name": "stdout", 552 | "output_type": "stream", 553 | "text": [ 554 | "abcd\n" 555 | ] 556 | } 557 | ], 558 | "source": [ 559 | "print(x)" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 39, 565 | "metadata": {}, 566 | "outputs": [ 567 | { 568 | "data": { 569 | "text/plain": [ 570 | "str" 571 | ] 572 | }, 573 | "execution_count": 39, 574 | "metadata": {}, 575 | "output_type": "execute_result" 576 | } 577 | ], 578 | "source": [ 579 | "type(x)" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": 40, 585 | "metadata": {}, 586 | "outputs": [ 587 | { 588 | "data": { 589 | "text/plain": [ 590 | "int" 591 | ] 592 | }, 593 | "execution_count": 40, 594 | "metadata": {}, 595 | "output_type": "execute_result" 596 | } 597 | ], 598 | "source": [ 599 | "x = 100\n", 600 | "type(x)" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 41, 606 | "metadata": {}, 607 | "outputs": [], 608 | "source": [ 609 | "x = 100" 610 | ] 611 | }, 612 | { 613 | "cell_type": "code", 614 | "execution_count": 42, 615 | "metadata": {}, 616 | "outputs": [], 617 | "source": [ 618 | "x = 200" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": 43, 624 | "metadata": {}, 625 | "outputs": [], 626 | "source": [ 627 | "# memory leak" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": null, 633 | "metadata": {}, 634 | "outputs": [], 635 | "source": [ 636 | "x = 100\n", 637 | "print(x)\n", 638 | "print(x)\n", 639 | "\n", 640 | "x = 200\n", 641 | "print(x)\n", 642 | "print(x)" 643 | ] 644 | }, 645 | { 646 | "cell_type": "code", 647 | "execution_count": 44, 648 | "metadata": {}, 649 | "outputs": [ 650 | { 651 | "name": "stdout", 652 | "output_type": "stream", 653 | "text": [ 654 | "Enter one number: 5\n", 655 | "Enter a second number: 7\n", 656 | "57\n" 657 | ] 658 | } 659 | ], 660 | "source": [ 661 | "x = input(\"Enter one number: \")\n", 662 | "y = input(\"Enter a second number: \")\n", 663 | "\n", 664 | "print(x + y)" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": null, 670 | "metadata": {}, 671 | "outputs": [], 672 | "source": [ 673 | "# Ask the user for their name\n", 674 | "# Ask the user for their country\n", 675 | "# Print a greeting mentioning both\n", 676 | "\n", 677 | "Enter your name: Reuven\n", 678 | "Enter your country: Israel\n", 679 | " \n", 680 | "Hello, Reuven from Israel! " 681 | ] 682 | }, 683 | { 684 | "cell_type": "code", 685 | "execution_count": 45, 686 | "metadata": {}, 687 | "outputs": [ 688 | { 689 | "data": { 690 | "text/plain": [ 691 | "'ab'" 692 | ] 693 | }, 694 | "execution_count": 45, 695 | "metadata": {}, 696 | "output_type": "execute_result" 697 | } 698 | ], 699 | "source": [ 700 | "'a' + 'b'" 701 | ] 702 | }, 703 | { 704 | "cell_type": "code", 705 | "execution_count": 46, 706 | "metadata": {}, 707 | "outputs": [ 708 | { 709 | "data": { 710 | "text/plain": [ 711 | "'abcd'" 712 | ] 713 | }, 714 | "execution_count": 46, 715 | "metadata": {}, 716 | "output_type": "execute_result" 717 | } 718 | ], 719 | "source": [ 720 | "'a' + 'b' + 'c' + 'd'" 721 | ] 722 | }, 723 | { 724 | "cell_type": "code", 725 | "execution_count": 47, 726 | "metadata": {}, 727 | "outputs": [ 728 | { 729 | "data": { 730 | "text/plain": [ 731 | "5" 732 | ] 733 | }, 734 | "execution_count": 47, 735 | "metadata": {}, 736 | "output_type": "execute_result" 737 | } 738 | ], 739 | "source": [ 740 | "10 - 5" 741 | ] 742 | }, 743 | { 744 | "cell_type": "code", 745 | "execution_count": 48, 746 | "metadata": {}, 747 | "outputs": [ 748 | { 749 | "ename": "TypeError", 750 | "evalue": "unsupported operand type(s) for -: 'str' and 'str'", 751 | "output_type": "error", 752 | "traceback": [ 753 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 754 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 755 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m'a'\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;34m'b'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 756 | "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'str'" 757 | ] 758 | } 759 | ], 760 | "source": [ 761 | "'a' - 'b'" 762 | ] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "execution_count": 49, 767 | "metadata": {}, 768 | "outputs": [ 769 | { 770 | "name": "stdout", 771 | "output_type": "stream", 772 | "text": [ 773 | "Enter your name: Reuven\n", 774 | "Where do you come from? Israel\n", 775 | "Hello, Reuven from Israel!\n" 776 | ] 777 | } 778 | ], 779 | "source": [ 780 | "name = input(\"Enter your name: \")\n", 781 | "country = input(\"Where do you come from? \")\n", 782 | "print(\"Hello, \" + name + \" from \" + country + \"!\")" 783 | ] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "execution_count": 50, 788 | "metadata": {}, 789 | "outputs": [ 790 | { 791 | "name": "stdout", 792 | "output_type": "stream", 793 | "text": [ 794 | "Please enter your name:Reuven\n", 795 | "Please enter your country: Israel\n", 796 | "Hello, Reuven from Israel\n" 797 | ] 798 | } 799 | ], 800 | "source": [ 801 | "name = input(\"Please enter your name:\")\n", 802 | "country = input(\"Please enter your country: \")\n", 803 | "print(f\"Hello, {name} from {country}\")" 804 | ] 805 | }, 806 | { 807 | "cell_type": "code", 808 | "execution_count": null, 809 | "metadata": {}, 810 | "outputs": [], 811 | "source": [] 812 | } 813 | ], 814 | "metadata": { 815 | "kernelspec": { 816 | "display_name": "Python 3", 817 | "language": "python", 818 | "name": "python3" 819 | }, 820 | "language_info": { 821 | "codemirror_mode": { 822 | "name": "ipython", 823 | "version": 3 824 | }, 825 | "file_extension": ".py", 826 | "mimetype": "text/x-python", 827 | "name": "python", 828 | "nbconvert_exporter": "python", 829 | "pygments_lexer": "ipython3", 830 | "version": "3.7.7" 831 | } 832 | }, 833 | "nbformat": 4, 834 | "nbformat_minor": 2 835 | } 836 | -------------------------------------------------------------------------------- /Non-programmers 2020 March 27.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Define two variables, x and y, with integer values. \n", 10 | "# Multiply them together, and assign the result to a \n", 11 | "# third variable, z. Use print to display the value of z.\n", 12 | "# Hint: In programming, we typically use the * for multiplication.\n", 13 | "\n", 14 | "x = 100\n", 15 | "y = 200\n", 16 | "\n", 17 | "z = x * y # z = 20000\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 2, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "text/plain": [ 28 | "20000" 29 | ] 30 | }, 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "output_type": "execute_result" 34 | } 35 | ], 36 | "source": [ 37 | "z" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 3, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "Enter your name: Reuven\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "# Ask the user, via input, to enter their name.\n", 55 | "# Print a nice greeting to the person.\n", 56 | "\n", 57 | "# name = 'Reuven'\n", 58 | "name = input(\"Enter your name: \")" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 4, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "ename": "TypeError", 68 | "evalue": "can only concatenate str (not \"int\") to str", 69 | "output_type": "error", 70 | "traceback": [ 71 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 72 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 73 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 74 | "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "# Define two variables: x should contain a string,\n", 80 | "# and y should contain an integer. What happens\n", 81 | "# if you try to add them together? \n", 82 | "# What happens if you try to multiply them?\n", 83 | "\n", 84 | "x = 'abcd'\n", 85 | "y = 3\n", 86 | "\n", 87 | "x + y" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 5, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "ename": "TypeError", 97 | "evalue": "can only concatenate str (not \"int\") to str", 98 | "output_type": "error", 99 | "traceback": [ 100 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 101 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 102 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 103 | "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "x = '5'\n", 109 | "y = 10\n", 110 | "\n", 111 | "x + y" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 6, 117 | "metadata": {}, 118 | "outputs": [ 119 | { 120 | "data": { 121 | "text/plain": [ 122 | "'abcdabcdabcd'" 123 | ] 124 | }, 125 | "execution_count": 6, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "x = 'abcd'\n", 132 | "y = 3\n", 133 | "\n", 134 | "x * y # this returns a new string, with x concatenated y times" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 7, 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "'abcd'" 146 | ] 147 | }, 148 | "execution_count": 7, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "x" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 8, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "3" 166 | ] 167 | }, 168 | "execution_count": 8, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "y" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 9, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "data": { 184 | "text/plain": [ 185 | "'abcdabcdabcd'" 186 | ] 187 | }, 188 | "execution_count": 9, 189 | "metadata": {}, 190 | "output_type": "execute_result" 191 | } 192 | ], 193 | "source": [ 194 | "z = x * y\n", 195 | "z" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 10, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "name": "stdout", 205 | "output_type": "stream", 206 | "text": [ 207 | "------------------------------------------------------------\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "print('-' * 60)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 11, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "data": { 222 | "text/plain": [ 223 | "'1010'" 224 | ] 225 | }, 226 | "execution_count": 11, 227 | "metadata": {}, 228 | "output_type": "execute_result" 229 | } 230 | ], 231 | "source": [ 232 | "x = '10' # this is a string, containing digits\n", 233 | "y = 5\n", 234 | "\n", 235 | "x + x # adding two strings together, so we get a new string" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 12, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "data": { 245 | "text/plain": [ 246 | "10" 247 | ] 248 | }, 249 | "execution_count": 12, 250 | "metadata": {}, 251 | "output_type": "execute_result" 252 | } 253 | ], 254 | "source": [ 255 | "y + y # adding two integers together, gives us a new int" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 13, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "ename": "TypeError", 265 | "evalue": "can only concatenate str (not \"int\") to str", 266 | "output_type": "error", 267 | "traceback": [ 268 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 269 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 270 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0my\u001b[0m \u001b[0;31m# what should Python do here?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# (1) treat them both like numbers, and give us 15 (integer)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# (2) treat them both like strings, and give us '105' (string)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# (3) throw up your hands, complain, and say it was ambiguous\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 271 | "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" 272 | ] 273 | } 274 | ], 275 | "source": [ 276 | "x + y # what should Python do here?\n", 277 | "\n", 278 | "# (1) treat them both like numbers, and give us 15 (integer)\n", 279 | "# (2) treat them both like strings, and give us '105' (string)\n", 280 | "# (3) throw up your hands, complain, and say it was ambiguous" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 14, 286 | "metadata": {}, 287 | "outputs": [ 288 | { 289 | "data": { 290 | "text/plain": [ 291 | "'1010101010'" 292 | ] 293 | }, 294 | "execution_count": 14, 295 | "metadata": {}, 296 | "output_type": "execute_result" 297 | } 298 | ], 299 | "source": [ 300 | "x * y" 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": 15, 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "data": { 310 | "text/plain": [ 311 | "'1010101010'" 312 | ] 313 | }, 314 | "execution_count": 15, 315 | "metadata": {}, 316 | "output_type": "execute_result" 317 | } 318 | ], 319 | "source": [ 320 | "y * x" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 16, 326 | "metadata": {}, 327 | "outputs": [], 328 | "source": [ 329 | "# forum.lerner.co.il" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": 18, 335 | "metadata": {}, 336 | "outputs": [], 337 | "source": [ 338 | "x = 100 # one = sign means: assign the value on the right to the variable on the left" 339 | ] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "execution_count": 19, 344 | "metadata": {}, 345 | "outputs": [ 346 | { 347 | "data": { 348 | "text/plain": [ 349 | "True" 350 | ] 351 | }, 352 | "execution_count": 19, 353 | "metadata": {}, 354 | "output_type": "execute_result" 355 | } 356 | ], 357 | "source": [ 358 | "# is x equal to 100?\n", 359 | "# we're going to use ==\n", 360 | "x == 100" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 20, 366 | "metadata": {}, 367 | "outputs": [ 368 | { 369 | "data": { 370 | "text/plain": [ 371 | "False" 372 | ] 373 | }, 374 | "execution_count": 20, 375 | "metadata": {}, 376 | "output_type": "execute_result" 377 | } 378 | ], 379 | "source": [ 380 | "x == 200" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": 21, 386 | "metadata": {}, 387 | "outputs": [ 388 | { 389 | "data": { 390 | "text/plain": [ 391 | "True" 392 | ] 393 | }, 394 | "execution_count": 21, 395 | "metadata": {}, 396 | "output_type": "execute_result" 397 | } 398 | ], 399 | "source": [ 400 | "name = 'Reuven' # assigning\n", 401 | "\n", 402 | "name == 'Reuven' # question: gives True or False back" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": 22, 408 | "metadata": {}, 409 | "outputs": [ 410 | { 411 | "data": { 412 | "text/plain": [ 413 | "False" 414 | ] 415 | }, 416 | "execution_count": 22, 417 | "metadata": {}, 418 | "output_type": "execute_result" 419 | } 420 | ], 421 | "source": [ 422 | "name == 'reuven'" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 23, 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "False" 434 | ] 435 | }, 436 | "execution_count": 23, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "name == 'Reuven '" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 24, 448 | "metadata": {}, 449 | "outputs": [], 450 | "source": [ 451 | "x = 100\n" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": 25, 457 | "metadata": {}, 458 | "outputs": [ 459 | { 460 | "data": { 461 | "text/plain": [ 462 | "True" 463 | ] 464 | }, 465 | "execution_count": 25, 466 | "metadata": {}, 467 | "output_type": "execute_result" 468 | } 469 | ], 470 | "source": [ 471 | "x < 200" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 26, 477 | "metadata": {}, 478 | "outputs": [ 479 | { 480 | "data": { 481 | "text/plain": [ 482 | "True" 483 | ] 484 | }, 485 | "execution_count": 26, 486 | "metadata": {}, 487 | "output_type": "execute_result" 488 | } 489 | ], 490 | "source": [ 491 | "x > 50" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": 27, 497 | "metadata": {}, 498 | "outputs": [ 499 | { 500 | "data": { 501 | "text/plain": [ 502 | "True" 503 | ] 504 | }, 505 | "execution_count": 27, 506 | "metadata": {}, 507 | "output_type": "execute_result" 508 | } 509 | ], 510 | "source": [ 511 | "x <=400" 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "execution_count": 28, 517 | "metadata": {}, 518 | "outputs": [ 519 | { 520 | "data": { 521 | "text/plain": [ 522 | "True" 523 | ] 524 | }, 525 | "execution_count": 28, 526 | "metadata": {}, 527 | "output_type": "execute_result" 528 | } 529 | ], 530 | "source": [ 531 | "x >= 30" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 29, 537 | "metadata": {}, 538 | "outputs": [ 539 | { 540 | "data": { 541 | "text/plain": [ 542 | "True" 543 | ] 544 | }, 545 | "execution_count": 29, 546 | "metadata": {}, 547 | "output_type": "execute_result" 548 | } 549 | ], 550 | "source": [ 551 | "# the not-equals operator is !=, because they didn't have ≠\n", 552 | "name != 'hello'" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": 30, 558 | "metadata": {}, 559 | "outputs": [ 560 | { 561 | "name": "stdout", 562 | "output_type": "stream", 563 | "text": [ 564 | "Enter your name: Reuven\n", 565 | "Hello, master!\n", 566 | "It has been far too long!\n", 567 | "This will always print, regardless of the name\n" 568 | ] 569 | } 570 | ], 571 | "source": [ 572 | "name = input(\"Enter your name: \")\n", 573 | "\n", 574 | "if name == 'Reuven': # if the comparison returns True\n", 575 | " print(\"Hello, master!\")\n", 576 | " print(\"It has been far too long!\")\n", 577 | "else: # if the comparison returns False\n", 578 | " print(\"Hello, \" + name) \n", 579 | "\n", 580 | "print(\"This will always print, regardless of the name\")" 581 | ] 582 | }, 583 | { 584 | "cell_type": "code", 585 | "execution_count": 31, 586 | "metadata": {}, 587 | "outputs": [ 588 | { 589 | "data": { 590 | "text/plain": [ 591 | "True" 592 | ] 593 | }, 594 | "execution_count": 31, 595 | "metadata": {}, 596 | "output_type": "execute_result" 597 | } 598 | ], 599 | "source": [ 600 | "name == 'Reuven' # are the values the same?" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 32, 606 | "metadata": {}, 607 | "outputs": [ 608 | { 609 | "data": { 610 | "text/plain": [ 611 | "False" 612 | ] 613 | }, 614 | "execution_count": 32, 615 | "metadata": {}, 616 | "output_type": "execute_result" 617 | } 618 | ], 619 | "source": [ 620 | "name is 'Reuven' # are the objects the same?" 621 | ] 622 | }, 623 | { 624 | "cell_type": "code", 625 | "execution_count": 33, 626 | "metadata": {}, 627 | "outputs": [], 628 | "source": [ 629 | "x = 1000\n", 630 | "y = x" 631 | ] 632 | }, 633 | { 634 | "cell_type": "code", 635 | "execution_count": 36, 636 | "metadata": {}, 637 | "outputs": [ 638 | { 639 | "name": "stdout", 640 | "output_type": "stream", 641 | "text": [ 642 | "Enter your country: Fredonia\n", 643 | "I'm not from there.\n" 644 | ] 645 | } 646 | ], 647 | "source": [ 648 | "# Ask the user what country they are from\n", 649 | "# If they enter your country, then say, \"Me too!\"\n", 650 | "# Otherwise, say, I'm not from there\n", 651 | "\n", 652 | "country = input(\"Enter your country: \")\n", 653 | "\n", 654 | "if country == 'Israel':\n", 655 | " print(\"Me, too!\")\n", 656 | "else:\n", 657 | " print(\"I'm not from there.\")" 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": 34, 663 | "metadata": {}, 664 | "outputs": [], 665 | "source": [ 666 | "# blog post about using \"is\" vs \"==\"\n", 667 | "# https://lerner.co.il/2015/06/16/why-you-should-almost-never-use-is-in-python/" 668 | ] 669 | }, 670 | { 671 | "cell_type": "code", 672 | "execution_count": null, 673 | "metadata": {}, 674 | "outputs": [], 675 | "source": [ 676 | "# option 1, the person enters \"Reuven\"\n", 677 | "# option 2, the person enters \"whatever\" -- that's not a name!\n", 678 | "# all others, give some generic response\n", 679 | "\n", 680 | "name = input(\"Enter your name: \")\n", 681 | "\n", 682 | "if name == 'Reuven':\n", 683 | " print(\"Hello, my master!\")\n", 684 | "elif name == 'whatever': # The name isn't \"Reuven\"... check this also\n", 685 | " print(\"Hey, that's not a name!\")\n", 686 | "else:\n", 687 | " print(f\"Hello, {name}\")" 688 | ] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": null, 693 | "metadata": {}, 694 | "outputs": [], 695 | "source": [ 696 | "# option 1, the person enters \"Reuven\"\n", 697 | "# option 2, the person enters \"whatever\" -- that's not a name!\n", 698 | "# all others, give some generic response\n", 699 | "\n", 700 | "name = input(\"Enter your name: \")\n", 701 | "\n", 702 | "if name == 'Reuven':\n", 703 | " print(\"Hello, my master!\")\n", 704 | "else:\n", 705 | " if name == 'whatever': \n", 706 | " print(\"Hey, that's not a name!\")\n", 707 | " else:\n", 708 | " print(f\"Hello, {name}\")" 709 | ] 710 | }, 711 | { 712 | "cell_type": "code", 713 | "execution_count": 39, 714 | "metadata": {}, 715 | "outputs": [ 716 | { 717 | "name": "stdout", 718 | "output_type": "stream", 719 | "text": [ 720 | "Enter your country: Israel \n", 721 | "I'm not from there.\n" 722 | ] 723 | } 724 | ], 725 | "source": [ 726 | "# Ask the user what country they are from\n", 727 | "# If they enter your country, then say, \"Me too!\"\n", 728 | "# If they enter a country you have visited, say you have gone there\n", 729 | "# (You can have more than one elif clause, if you want.)\n", 730 | "# Otherwise, say, I'm not from there\n", 731 | "\n", 732 | "country = input(\"Enter your country: \")\n", 733 | "\n", 734 | "if country == 'Israel':\n", 735 | " print(\"Me, too!\")\n", 736 | "elif country == 'China':\n", 737 | " print(\"I'm not from there, but I've visited many times!\")\n", 738 | "elif country == 'USA':\n", 739 | " print(\"I'm originally from there, too!\")\n", 740 | "else:\n", 741 | " print(\"I'm not from there.\")" 742 | ] 743 | }, 744 | { 745 | "cell_type": "code", 746 | "execution_count": 40, 747 | "metadata": {}, 748 | "outputs": [ 749 | { 750 | "name": "stdout", 751 | "output_type": "stream", 752 | "text": [ 753 | "Both are what you want!\n" 754 | ] 755 | } 756 | ], 757 | "source": [ 758 | "x = 10\n", 759 | "y = 20\n", 760 | "\n", 761 | "# Is x equal to 10, and y equal to 20?\n", 762 | "# I can join two comparisons together with \"and\"\n", 763 | "if x == 10 and y == 20:\n", 764 | " print(\"Both are what you want!\")" 765 | ] 766 | }, 767 | { 768 | "cell_type": "code", 769 | "execution_count": 41, 770 | "metadata": {}, 771 | "outputs": [ 772 | { 773 | "data": { 774 | "text/plain": [ 775 | "True" 776 | ] 777 | }, 778 | "execution_count": 41, 779 | "metadata": {}, 780 | "output_type": "execute_result" 781 | } 782 | ], 783 | "source": [ 784 | "True and True" 785 | ] 786 | }, 787 | { 788 | "cell_type": "code", 789 | "execution_count": 42, 790 | "metadata": {}, 791 | "outputs": [ 792 | { 793 | "data": { 794 | "text/plain": [ 795 | "False" 796 | ] 797 | }, 798 | "execution_count": 42, 799 | "metadata": {}, 800 | "output_type": "execute_result" 801 | } 802 | ], 803 | "source": [ 804 | "False and True" 805 | ] 806 | }, 807 | { 808 | "cell_type": "code", 809 | "execution_count": 43, 810 | "metadata": {}, 811 | "outputs": [ 812 | { 813 | "name": "stdout", 814 | "output_type": "stream", 815 | "text": [ 816 | "At least one is what you want\n" 817 | ] 818 | } 819 | ], 820 | "source": [ 821 | "# Is x equal to 10 *or* is y equal to 20?\n", 822 | "# we can do that with \"or\"\n", 823 | "if x == 15 or y == 20:\n", 824 | " print(\"At least one is what you want\")" 825 | ] 826 | }, 827 | { 828 | "cell_type": "code", 829 | "execution_count": 44, 830 | "metadata": {}, 831 | "outputs": [ 832 | { 833 | "data": { 834 | "text/plain": [ 835 | "True" 836 | ] 837 | }, 838 | "execution_count": 44, 839 | "metadata": {}, 840 | "output_type": "execute_result" 841 | } 842 | ], 843 | "source": [ 844 | "# \"or\" returns True if one or more of its conditions is True\n", 845 | "False or True" 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "execution_count": 45, 851 | "metadata": {}, 852 | "outputs": [ 853 | { 854 | "name": "stdout", 855 | "output_type": "stream", 856 | "text": [ 857 | "It is either 10, 20, or 30\n" 858 | ] 859 | } 860 | ], 861 | "source": [ 862 | "x = 10\n", 863 | "\n", 864 | "if x == 15:\n", 865 | " print(\"Yes, x is 15\")\n", 866 | "elif (x == 10 or \n", 867 | " x == 20 or \n", 868 | " x == 30):\n", 869 | " print(\"It is either 10, 20, or 30\")\n", 870 | "else:\n", 871 | " print(\"x is something else entirely\")" 872 | ] 873 | }, 874 | { 875 | "cell_type": "code", 876 | "execution_count": null, 877 | "metadata": {}, 878 | "outputs": [], 879 | "source": [] 880 | }, 881 | { 882 | "cell_type": "code", 883 | "execution_count": 47, 884 | "metadata": {}, 885 | "outputs": [ 886 | { 887 | "name": "stdout", 888 | "output_type": "stream", 889 | "text": [ 890 | "Enter your country: India\n", 891 | "I'm not from there, but I've visited many times!\n" 892 | ] 893 | } 894 | ], 895 | "source": [ 896 | "# Ask the user what country they are from\n", 897 | "# If they enter your country, then say, \"Me too!\"\n", 898 | "# If they enter one of several countries you have visited, \n", 899 | "# then say, \"I have visited there, too!\"\n", 900 | "# Otherwise, say, I'm not from there\n", 901 | "\n", 902 | "country = input(\"Enter your country: \")\n", 903 | "\n", 904 | "if country == 'Israel':\n", 905 | " print(\"Me, too!\")\n", 906 | "elif country == 'China' or country == 'India' or country == 'UK':\n", 907 | " print(\"I'm not from there, but I've visited many times!\")\n", 908 | "elif country == 'USA':\n", 909 | " print(\"I'm originally from there, too!\")\n", 910 | "else:\n", 911 | " print(\"I'm not from there.\")" 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 51, 917 | "metadata": {}, 918 | "outputs": [ 919 | { 920 | "name": "stdout", 921 | "output_type": "stream", 922 | "text": [ 923 | "Enter your country: USA\n", 924 | "I'm not from there, but I've visited many times!\n" 925 | ] 926 | } 927 | ], 928 | "source": [ 929 | "country = input(\"Enter your country: \")\n", 930 | "\n", 931 | "if country == 'Israel':\n", 932 | " print(\"Me, too!\")\n", 933 | "elif country == 'China' or 'India' or 'UK':\n", 934 | " print(\"I'm not from there, but I've visited many times!\")\n", 935 | "elif country == 'USA':\n", 936 | " print(\"I'm originally from there, too!\")\n", 937 | "else:\n", 938 | " print(\"I'm not from there.\")" 939 | ] 940 | }, 941 | { 942 | "cell_type": "code", 943 | "execution_count": 52, 944 | "metadata": {}, 945 | "outputs": [], 946 | "source": [ 947 | "# In Python, all strings are considered True\n", 948 | "# when we're evaluating them in if/elif/and/or \n", 949 | "# unless it's the empty string" 950 | ] 951 | }, 952 | { 953 | "cell_type": "code", 954 | "execution_count": 54, 955 | "metadata": {}, 956 | "outputs": [ 957 | { 958 | "name": "stdout", 959 | "output_type": "stream", 960 | "text": [ 961 | "Enter your name: \n", 962 | "Hey! You didn't enter a name!\n" 963 | ] 964 | } 965 | ], 966 | "source": [ 967 | "name = input(\"Enter your name: \")\n", 968 | "\n", 969 | "if name: # if the user's string is not empty\n", 970 | " print(f\"Hello, {name}\")\n", 971 | "else:\n", 972 | " print(\"Hey! You didn't enter a name!\")" 973 | ] 974 | }, 975 | { 976 | "cell_type": "code", 977 | "execution_count": 55, 978 | "metadata": {}, 979 | "outputs": [ 980 | { 981 | "name": "stdout", 982 | "output_type": "stream", 983 | "text": [ 984 | "It is True-ish\n" 985 | ] 986 | } 987 | ], 988 | "source": [ 989 | "x = 'abcd'\n", 990 | "\n", 991 | "if x: # the rule is: all strings are True in an if, except ''\n", 992 | " print(\"It is True-ish\")\n", 993 | " " 994 | ] 995 | }, 996 | { 997 | "cell_type": "code", 998 | "execution_count": 56, 999 | "metadata": {}, 1000 | "outputs": [ 1001 | { 1002 | "name": "stdout", 1003 | "output_type": "stream", 1004 | "text": [ 1005 | "It is False-ish\n" 1006 | ] 1007 | } 1008 | ], 1009 | "source": [ 1010 | "x = ''\n", 1011 | "\n", 1012 | "if x: # x is the empty string, so it's False in an \"if\" statement\n", 1013 | " print(\"It is True-ish\")\n", 1014 | "else:\n", 1015 | " print(\"It is False-ish\")" 1016 | ] 1017 | }, 1018 | { 1019 | "cell_type": "code", 1020 | "execution_count": null, 1021 | "metadata": {}, 1022 | "outputs": [], 1023 | "source": [ 1024 | "x = 'abcd'\n", 1025 | "y = 'efgh'\n", 1026 | "z = 'ijkl'\n", 1027 | "\n", 1028 | "if x == 'hello' or y == 'hello' or z == 'hello': # False or True or True --> True\n", 1029 | " print(\"It is True-ish\")\n", 1030 | "else:\n", 1031 | " print(\"It is False-ish\")" 1032 | ] 1033 | } 1034 | ], 1035 | "metadata": { 1036 | "kernelspec": { 1037 | "display_name": "Python 3", 1038 | "language": "python", 1039 | "name": "python3" 1040 | }, 1041 | "language_info": { 1042 | "codemirror_mode": { 1043 | "name": "ipython", 1044 | "version": 3 1045 | }, 1046 | "file_extension": ".py", 1047 | "mimetype": "text/x-python", 1048 | "name": "python", 1049 | "nbconvert_exporter": "python", 1050 | "pygments_lexer": "ipython3", 1051 | "version": "3.7.7" 1052 | } 1053 | }, 1054 | "nbformat": 4, 1055 | "nbformat_minor": 2 1056 | } 1057 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is a repository for my free "Python for non-programmers" course. It ran for 15 weeks in the spring and summer of 2020; the 15 hours of recorded lectures are still available, for free, at: 2 | 3 | https://PythonForNonProgrammers.com 4 | 5 | You're free to clone and learn from this material, and I hope that you do! 6 | 7 | You can also follow me on Twitter as [@reuvenmlerner](https://twitter.com/reuvenmlerner), or e-mail me at [reuven@lerner.co.il](mailto:reuven@lerner.co.il). 8 | 9 | I hope that you learn from this material! 10 | -------------------------------------------------------------------------------- /Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /dict-counts.txt: -------------------------------------------------------------------------------- 1 | this 1 2 | test 1 3 | also 2 4 | -------------------------------------------------------------------------------- /even-nums.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 3 | 12 4 | 5 | 14 6 | 7 | 100 8 | 9 | -------------------------------------------------------------------------------- /even_numbers.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 12 3 | 200 4 | 308 5 | -------------------------------------------------------------------------------- /ip-address-report.txt: -------------------------------------------------------------------------------- 1 | 67.218.116.165 2 | 66.249.71.65 3 | 65.55.106.183 4 | 65.55.106.183 5 | 66.249.71.65 6 | 66.249.71.65 7 | 66.249.65.12 8 | 66.249.65.12 9 | 66.249.65.12 10 | 66.249.65.12 11 | 66.249.65.12 12 | 65.55.106.131 13 | 65.55.106.131 14 | 66.249.65.12 15 | 66.249.65.12 16 | 66.249.65.12 17 | 66.249.65.12 18 | 66.249.65.12 19 | 66.249.65.12 20 | 66.249.65.12 21 | 65.55.106.186 22 | 65.55.106.186 23 | 66.249.65.12 24 | 66.249.65.12 25 | 66.249.65.12 26 | 74.52.245.146 27 | 74.52.245.146 28 | 66.249.65.43 29 | 66.249.65.43 30 | 66.249.65.43 31 | 66.249.65.12 32 | 66.249.65.12 33 | 66.249.65.12 34 | 66.249.65.12 35 | 66.249.65.12 36 | 66.249.65.12 37 | 65.55.207.25 38 | 65.55.207.25 39 | 66.249.65.12 40 | 66.249.65.12 41 | 66.249.65.12 42 | 66.249.65.12 43 | 66.249.65.12 44 | 66.249.65.12 45 | 66.249.65.12 46 | 65.55.207.94 47 | 65.55.207.94 48 | 66.249.65.12 49 | 65.55.207.71 50 | 66.249.65.12 51 | 66.249.65.12 52 | 66.249.65.12 53 | 98.242.170.241 54 | 66.249.65.38 55 | 66.249.65.38 56 | 66.249.65.38 57 | 66.249.65.38 58 | 66.249.65.38 59 | 66.249.65.38 60 | 66.249.65.38 61 | 66.249.65.38 62 | 66.249.65.38 63 | 66.249.65.38 64 | 66.249.65.38 65 | 66.249.65.38 66 | 66.249.65.38 67 | 66.249.65.38 68 | 66.249.65.38 69 | 66.249.65.38 70 | 66.249.65.38 71 | 66.249.65.38 72 | 66.249.65.38 73 | 66.249.65.38 74 | 66.249.65.38 75 | 66.249.65.38 76 | 66.249.65.38 77 | 66.249.65.38 78 | 66.249.65.38 79 | 66.249.65.38 80 | 65.55.207.126 81 | 65.55.207.126 82 | 66.249.65.38 83 | 66.249.65.38 84 | 66.249.65.38 85 | 66.249.65.38 86 | 82.34.9.20 87 | 82.34.9.20 88 | 66.249.65.38 89 | 66.249.65.38 90 | 66.249.65.38 91 | 66.249.65.38 92 | 66.249.65.38 93 | 66.249.65.38 94 | 66.249.65.38 95 | 66.249.65.38 96 | 66.249.65.38 97 | 65.55.106.155 98 | 65.55.106.155 99 | 66.249.65.38 100 | 66.249.65.38 101 | 66.249.65.38 102 | 66.249.65.38 103 | 66.249.65.38 104 | 65.55.207.77 105 | 65.55.207.77 106 | 66.249.65.38 107 | 67.218.116.165 108 | 66.249.65.38 109 | 208.80.193.28 110 | 66.249.65.38 111 | 66.249.65.38 112 | 66.249.65.38 113 | 66.249.65.38 114 | 66.249.65.38 115 | 66.249.65.38 116 | 66.249.65.38 117 | 66.249.65.38 118 | 66.249.65.38 119 | 89.248.172.58 120 | 89.248.172.58 121 | 89.248.172.58 122 | 89.248.172.58 123 | 89.248.172.58 124 | 89.248.172.58 125 | 89.248.172.58 126 | 89.248.172.58 127 | 89.248.172.58 128 | 89.248.172.58 129 | 89.248.172.58 130 | 89.248.172.58 131 | 89.248.172.58 132 | 89.248.172.58 133 | 89.248.172.58 134 | 89.248.172.58 135 | 89.248.172.58 136 | 89.248.172.58 137 | 89.248.172.58 138 | 89.248.172.58 139 | 89.248.172.58 140 | 89.248.172.58 141 | 66.249.65.38 142 | 66.249.65.38 143 | 66.249.65.38 144 | 66.249.65.38 145 | 66.249.65.38 146 | 66.249.65.38 147 | 66.249.65.38 148 | 66.249.65.38 149 | 66.249.65.38 150 | 66.249.65.38 151 | 66.249.65.38 152 | 66.249.65.38 153 | 66.249.65.38 154 | 66.249.65.38 155 | 66.249.65.38 156 | 66.249.65.38 157 | 66.249.65.38 158 | 66.249.65.38 159 | 66.249.65.38 160 | 66.249.65.38 161 | 66.249.65.38 162 | 66.249.65.38 163 | 66.249.65.38 164 | 66.249.65.38 165 | 66.249.65.38 166 | 66.249.65.38 167 | 66.249.65.38 168 | 66.249.65.38 169 | 66.249.65.38 170 | 67.195.112.35 171 | 67.195.112.35 172 | 67.195.112.35 173 | 67.195.112.35 174 | 67.195.112.35 175 | 67.195.112.35 176 | 67.195.112.35 177 | 67.195.112.35 178 | 67.195.112.35 179 | 67.195.112.35 180 | 67.195.112.35 181 | 67.195.112.35 182 | 67.195.112.35 183 | 67.195.112.35 184 | 67.195.112.35 185 | 67.195.112.35 186 | 66.249.65.38 187 | 65.55.207.50 188 | 65.55.207.50 189 | 65.55.207.50 190 | 66.249.65.38 191 | 66.249.65.38 192 | 66.249.65.38 193 | 66.249.65.38 194 | 66.249.65.38 195 | 66.249.65.38 196 | 65.55.215.75 197 | 65.55.215.75 198 | 66.249.65.38 199 | 66.249.65.38 200 | 66.249.65.38 201 | 66.249.65.38 202 | 66.249.65.38 203 | 66.249.65.38 204 | 66.249.65.38 205 | 66.249.65.38 206 | 66.249.65.38 207 | -------------------------------------------------------------------------------- /linux-etc-passwd.txt: -------------------------------------------------------------------------------- 1 | # This is a comment 2 | # You should ignore me 3 | root:x:0:0:root:/root:/bin/bash 4 | daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin 5 | bin:x:2:2:bin:/bin:/usr/sbin/nologin 6 | sys:x:3:3:sys:/dev:/usr/sbin/nologin 7 | sync:x:4:65534:sync:/bin:/bin/sync 8 | games:x:5:60:games:/usr/games:/usr/sbin/nologin 9 | man:x:6:12:man:/var/cache/man:/usr/sbin/nologin 10 | lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin 11 | mail:x:8:8:mail:/var/mail:/usr/sbin/nologin 12 | 13 | 14 | 15 | news:x:9:9:news:/var/spool/news:/usr/sbin/nologin 16 | uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin 17 | proxy:x:13:13:proxy:/bin:/usr/sbin/nologin 18 | www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin 19 | backup:x:34:34:backup:/var/backups:/usr/sbin/nologin 20 | list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin 21 | irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin 22 | gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin 23 | 24 | nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin 25 | syslog:x:101:104::/home/syslog:/bin/false 26 | messagebus:x:102:106::/var/run/dbus:/bin/false 27 | landscape:x:103:109::/var/lib/landscape:/bin/false 28 | jci:x:955:955::/home/jci:/bin/bash 29 | sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin 30 | user:x:1000:1000:user,,,:/home/user:/bin/bash 31 | reuven:x:1001:1001:Reuven M. Lerner,,,:/home/reuven:/bin/bash 32 | postfix:x:105:113::/var/spool/postfix:/bin/false 33 | colord:x:106:116:colord colour management daemon,,,:/var/lib/colord:/bin/false 34 | postgres:x:107:117:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash 35 | dovecot:x:108:119:Dovecot mail server,,,:/usr/lib/dovecot:/bin/false 36 | dovenull:x:109:120:Dovecot login user,,,:/nonexistent:/bin/false 37 | postgrey:x:110:121::/var/lib/postgrey:/bin/false 38 | debian-spamd:x:111:122::/var/lib/spamassassin:/bin/sh 39 | memcache:x:113:124:Memcached,,,:/nonexistent:/bin/false 40 | genadi:x:1002:1003:Genadi Reznichenko,,,:/home/genadi:/bin/bash 41 | shira:x:1003:1004:Shira Friedman,,,:/home/shira:/bin/bash 42 | atara:x:1004:1005:Atara Lerner-Friedman,,,:/home/atara:/bin/bash 43 | shikma:x:1005:1006:Shikma Lerner-Friedman,,,:/home/shikma:/bin/bash 44 | amotz:x:1006:1007:Amotz Lerner-Friedman,,,:/home/amotz:/bin/bash 45 | mysql:x:114:125:MySQL Server,,,:/nonexistent:/bin/false 46 | clamav:x:115:126::/var/lib/clamav:/bin/false 47 | amavis:x:116:127:AMaViS system user,,,:/var/lib/amavis:/bin/sh 48 | opendkim:x:117:128::/var/run/opendkim:/bin/false 49 | gitlab-redis:x:999:1009::/var/opt/gitlab/redis:/bin/nologin 50 | gitlab-psql:x:998:1010::/var/opt/gitlab/postgresql:/bin/sh 51 | git:x:1007:1008:GitLab,,,:/home/git:/bin/bash 52 | opendmarc:x:118:129::/var/run/opendmarc:/bin/false 53 | dkim-milter-python:x:119:130::/var/run/dkim-milter-python:/bin/false 54 | deploy:x:1008:1011:Deploy,,,:/home/deploy:/bin/bash 55 | redis:x:112:123:redis server,,,:/var/lib/redis:/bin/false 56 | -------------------------------------------------------------------------------- /movies.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/reuven/python-for-non-programmers/f090c30465431d7aba477426a1ad638ab591fdba/movies.dat -------------------------------------------------------------------------------- /mydict.csv: -------------------------------------------------------------------------------- 1 | a,1 2 | b,2 3 | c,3 4 | d,4 5 | -------------------------------------------------------------------------------- /myfile.txt: -------------------------------------------------------------------------------- 1 | aaabbb 2 | cccdddddd 3 | eeefffff 4 | even more! 5 | more more more 6 | -------------------------------------------------------------------------------- /myoutput.txt: -------------------------------------------------------------------------------- 1 | hello 2 | -------------------------------------------------------------------------------- /nums.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 10 3 | 20 4 | 3 5 | 20 6 | 7 | 25 8 | -------------------------------------------------------------------------------- /odd-nums.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 3 | 15 4 | 5 | 101 6 | 7 | -------------------------------------------------------------------------------- /odd_number.txt: -------------------------------------------------------------------------------- 1 | 11 2 | 13 3 | 15 4 | 203 5 | 301 6 | -------------------------------------------------------------------------------- /sales-report.csv: -------------------------------------------------------------------------------- 1 | books,10 2 | ice cream,20 3 | -------------------------------------------------------------------------------- /selected-nums.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 20 3 | 20 4 | 25 5 | -------------------------------------------------------------------------------- /shoe-data.txt: -------------------------------------------------------------------------------- 1 | Adidas orange 43 2 | Nike black 41 3 | Adidas black 39 4 | New Balance pink 41 5 | Nike white 44 6 | New Balance orange 38 7 | Nike pink 44 8 | Adidas pink 44 9 | New Balance orange 39 10 | New Balance black 43 11 | New Balance orange 44 12 | Nike black 41 13 | Adidas orange 37 14 | Adidas black 38 15 | Adidas pink 41 16 | Adidas white 36 17 | Adidas orange 36 18 | Nike pink 41 19 | Adidas pink 35 20 | New Balance orange 37 21 | Nike pink 43 22 | Nike black 43 23 | Nike black 42 24 | Nike black 35 25 | Adidas black 41 26 | New Balance pink 40 27 | Adidas white 35 28 | New Balance pink 41 29 | New Balance orange 41 30 | Adidas orange 40 31 | New Balance orange 40 32 | New Balance white 44 33 | New Balance pink 40 34 | Nike black 43 35 | Nike pink 36 36 | New Balance white 39 37 | Nike black 42 38 | Adidas black 41 39 | New Balance orange 40 40 | New Balance black 40 41 | Nike white 37 42 | Adidas black 39 43 | Adidas black 40 44 | Adidas orange 38 45 | New Balance orange 39 46 | Nike black 35 47 | Adidas white 39 48 | Nike white 37 49 | Adidas orange 37 50 | Adidas pink 35 51 | New Balance orange 41 52 | Nike pink 44 53 | Nike pink 38 54 | Adidas black 39 55 | New Balance white 35 56 | Nike pink 40 57 | Nike white 44 58 | Nike orange 38 59 | Adidas orange 42 60 | New Balance orange 43 61 | Adidas pink 39 62 | Adidas pink 41 63 | Adidas pink 39 64 | Nike white 37 65 | Nike orange 38 66 | Adidas orange 39 67 | Nike pink 40 68 | Adidas white 36 69 | Nike orange 40 70 | New Balance pink 40 71 | New Balance black 40 72 | New Balance pink 40 73 | Adidas pink 41 74 | Nike pink 40 75 | Nike black 41 76 | Nike black 39 77 | New Balance white 38 78 | Adidas black 41 79 | Nike orange 36 80 | Nike black 38 81 | New Balance black 40 82 | New Balance pink 40 83 | Adidas black 42 84 | Adidas white 40 85 | New Balance orange 38 86 | Nike pink 41 87 | Adidas orange 37 88 | Nike black 44 89 | Adidas pink 36 90 | Adidas white 35 91 | Nike black 38 92 | Nike pink 42 93 | New Balance black 43 94 | Nike white 38 95 | New Balance pink 39 96 | Nike orange 39 97 | New Balance orange 40 98 | New Balance white 44 99 | Adidas black 42 100 | Nike black 35 101 | -------------------------------------------------------------------------------- /testprog.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | def add_one(a_list=[]): 4 | 5 | 6 | < """Demonstrate terrible, mutable defaults""" 7 | 8 | a_list.append(1) 9 | return a_list 10 | 11 | 12 | print(add_one()) 13 | print(add_one()) 14 | print(add_one()) 15 | -------------------------------------------------------------------------------- /vowel_counts.txt: -------------------------------------------------------------------------------- 1 | this 1 2 | is 1 3 | a 1 4 | test 1 5 | this 1 6 | is 1 7 | a 1 8 | very 1 9 | important 3 10 | test 1 11 | a 1 12 | test 1 13 | should 2 14 | have 2 15 | no 1 16 | encyclopedias 5 17 | attached 3 18 | -------------------------------------------------------------------------------- /wcfile.txt: -------------------------------------------------------------------------------- 1 | This is a test file. 2 | 3 | It contains 28 words and 20 different words. 4 | 5 | It also contains 165 characters. 6 | 7 | It also contains 11 lines. 8 | 9 | It is also self-referential. 10 | 11 | Wow! 12 | --------------------------------------------------------------------------------