├── .gitignore ├── README.md ├── Functional Programming.ipynb ├── python_basics.ipynb └── jupyter-notebooks ├── object_oriented_programming.ipynb └── python_basics.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Complete-Python-Developer-Manual 2 | 3 | Resources and notes for the Zero to Mastery [Complete Python Developer course](https://zerotomastery.io/courses/learn-python/) 4 | 5 | - Introduction 6 | - [Python Introduction](https://github.com/zero-to-mastery/Complete-Python-Developer-Manuel/blob/main/python_basics.ipynb) 7 | - Python Basics 8 | - Python Basics 2 9 | - Developer Environment 10 | - Advanced Python: Object-Oriented Programming 11 | - [Advanced Python: Functional Programming](https://github.com/zero-to-mastery/Complete-Python-Developer-Manual/blob/main/Functional%20Programming.ipynb) 12 | - Advanced Python: Decorators 13 | - Advanced Python: Error Handling 14 | - Advanced Python: Generators 15 | - Modules in Python 16 | - Debugging in Python 17 | - File I/O 18 | - Regular Expressions 19 | - Testing in Python 20 | - Career of a Python Developer 21 | - Scripting with Python 22 | - Python Network Programming 23 | - Scraping Data with Python 24 | - Web Development with Python 25 | - Automation/Testing 26 | - Machine Learning + Data Science 27 | - Where to Go from Here 28 | - Bonus: Extra Bits 29 | - Bonus: HTML Forms 30 | -------------------------------------------------------------------------------- /Functional Programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "3c102256", 6 | "metadata": {}, 7 | "source": [ 8 | "# 7. Advanced Python: Functional Programming\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "ea7548fe", 14 | "metadata": {}, 15 | "source": [ 16 | " - Functional programming is separation of concerns\n", 17 | " - packaging our code in chunks so that everything is well organized based on functionality\n", 18 | " - one thing its good at\n", 19 | " - separate data and behaviour/functionality, unlike OOP\n", 20 | " -Makes our code:\n", 21 | " \n", 22 | " 1. clean + understandable\n", 23 | " 2. easy to extend -> grow\n", 24 | " 3. easy to maintain -> not storing\n", 25 | " 4. memory efficient\n", 26 | " 5. dry -> non-redundant" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "id": "ecb6220b", 32 | "metadata": {}, 33 | "source": [ 34 | "## 7.a Pure Functions\n" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "id": "63bc0c69", 40 | "metadata": {}, 41 | "source": [ 42 | "They are used to :\n", 43 | " - one input, always only one and same output\n", 44 | " - should not produce side effects. It shouldn't create changes in outside world of the function. i.e. printing. it displays something on the screen, which is the out side world\n", 45 | " - They help because it creates less-bug code. It is better to understand and modify without many errors.\n", 46 | " - Things not touching each other and being discrete" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "f5278152", 52 | "metadata": {}, 53 | "source": [ 54 | "### Map Fucntion\n", 55 | "\n", 56 | "- map(fun, iterable) -> returns the obj" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "id": "9459d2a5", 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "def mul2(num):\n", 75 | " return num * 2\n", 76 | "print(list(map(mul2,list(range(10)))))" 77 | ] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 3", 83 | "language": "python", 84 | "name": "python3" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 3 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython3", 96 | "version": "3.8.8" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 5 101 | } 102 | -------------------------------------------------------------------------------- /python_basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "64318629", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "f957de12", 14 | "metadata": {}, 15 | "source": [ 16 | "# Variables\n", 17 | "All programming languages have variables so that we can store information. Assigning a value is also known as binding. The number below is stored as a binary representation (zeros and ones).\n", 18 | "\n", 19 | "The following are best practices to use when writing Python variables:\n", 20 | "* They should be snake_case\n", 21 | "* Start with lowercase or underscore\n", 22 | "* Use letters, numbers, or underscores\n", 23 | "* They are case-sensitive\n", 24 | "* Don't overwrite keywords\n", 25 | "* Make variable names descriptive\n", 26 | "\n", 27 | "Other things to be aware of:\n", 28 | "* Constants should be in capitals, i.e., PI = 3.14\n", 29 | "* There are dunder variables that start with two underscores that should not be assigned\n", 30 | "* Underscore in Python singnifies a private variable (_user_iq)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 3, 36 | "id": "86e4a263", 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "47.5\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "user_iq = 190\n", 49 | "user_age = user_iq / 4\n", 50 | "print(user_age)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "id": "f86b84e0", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "2\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "a, b, c = 1, 2, 3\n", 69 | "print(b)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "id": "02dfa7a3", 75 | "metadata": {}, 76 | "source": [ 77 | "# Expressions vs. Statements\n", 78 | "An expression is the right side of a statement. A statement is an entire line of code that performs some sort of action.\n", 79 | "\n", 80 | "As in the previous example, user_iq / 4 is an expression. user_age = user_iq / 4 and user_iq = 190 are statements." 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "id": "06d8700f", 86 | "metadata": {}, 87 | "source": [ 88 | "# Augmented Assignment Operator\n", 89 | "In the example below, some_value += 2 makes use of the augmented assignment operator." 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 2, 95 | "id": "579c4e4a", 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "some_value = 5\n", 100 | "some_value += 2" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "id": "f5023e0e", 106 | "metadata": {}, 107 | "source": [ 108 | "# Strings (str)\n", 109 | "A string is a piece of text and can be encapsulated in single or double quotes.\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 3, 115 | "id": "ad9535fb", 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "print(type('hellooooo'))" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 8, 133 | "id": "abf5ab83", 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "username = 'supercoder'\n", 138 | "password = 'supersecret'\n", 139 | "long_string = '''\n", 140 | "WOW\n", 141 | " 00\n", 142 | "---\n", 143 | "'''" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 9, 149 | "id": "65e6a13e", 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "name": "stdout", 154 | "output_type": "stream", 155 | "text": [ 156 | "\n", 157 | "WOW\n", 158 | " 00\n", 159 | "---\n", 160 | "\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "print(long_string)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "id": "f11ce78a", 171 | "metadata": {}, 172 | "source": [ 173 | "# String Concatenation" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 10, 179 | "id": "360bec82", 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "hellooooo Andrei\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "print('hellooooo' + ' Andrei')" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "id": "0367f71a", 197 | "metadata": {}, 198 | "source": [ 199 | "# Type Conversion" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 11, 205 | "id": "f0ad0c1a", 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "name": "stdout", 210 | "output_type": "stream", 211 | "text": [ 212 | "\n" 213 | ] 214 | } 215 | ], 216 | "source": [ 217 | "print(type(str(100)))" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 12, 223 | "id": "7c20a0b0", 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "name": "stdout", 228 | "output_type": "stream", 229 | "text": [ 230 | "\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "print(type(int(str(100))))" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "id": "cff587dd", 241 | "metadata": {}, 242 | "source": [ 243 | "# Escape Sequence" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 14, 249 | "id": "ad66d0fa", 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "name": "stdout", 254 | "output_type": "stream", 255 | "text": [ 256 | "It's kind of sunny\n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "weather = 'It\\'s kind of sunny'\n", 262 | "print(weather)" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 15, 268 | "id": "e235b9df", 269 | "metadata": {}, 270 | "outputs": [ 271 | { 272 | "name": "stdout", 273 | "output_type": "stream", 274 | "text": [ 275 | "\t weather\n" 276 | ] 277 | } 278 | ], 279 | "source": [ 280 | "print('\\t weather')" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 17, 286 | "id": "0b7c9d83", 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | "--\n", 294 | "Hope you have a good day\n" 295 | ] 296 | } 297 | ], 298 | "source": [ 299 | "print('--\\nHope you have a good day')" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "id": "166d1d2e", 305 | "metadata": {}, 306 | "source": [ 307 | "# Formatted Strings\n", 308 | "In general, the formatted string is recommended now (f-string) versus .format." 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 8, 314 | "id": "784d1e22", 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "name": "stdout", 319 | "output_type": "stream", 320 | "text": [ 321 | "Hi Johnny. You are 55 years old.\n", 322 | "Hi Johnny. You are 55 years old.\n", 323 | "Hi Johnny. You are 55 years old.\n" 324 | ] 325 | } 326 | ], 327 | "source": [ 328 | "name = 'Johnny'\n", 329 | "age = 55\n", 330 | "print(f\"Hi {name}. You are {age} years old.\") # preferred\n", 331 | "print(\"Hi {}. You are {} years old.\".format(name, age)) # python2 syntax\n", 332 | "print(\"Hi {0}. You are {1} years old.\".format(name, age))" 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "id": "192149dc", 338 | "metadata": {}, 339 | "source": [ 340 | "# String Instances\n", 341 | "str is an ordered sequence of characters that are stored in memory in same order and can be accessed by indeces." 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 20, 347 | "id": "a0a399cd", 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "name": "stdout", 352 | "output_type": "stream", 353 | "text": [ 354 | "L\n", 355 | "La\n", 356 | "Lu\n", 357 | "aura\n", 358 | "Lau\n", 359 | "Lua\n", 360 | "Laura\n", 361 | "r\n", 362 | "aruaL\n" 363 | ] 364 | } 365 | ], 366 | "source": [ 367 | "name = \"Laura\"\n", 368 | "print(name[0])\n", 369 | "print(name[0:2])\n", 370 | "print(name[0:4:2])\n", 371 | "print(name[1:])\n", 372 | "print(name[:3])\n", 373 | "print(name[::2])\n", 374 | "print(name[::1])\n", 375 | "print(name[-2])\n", 376 | "print(name[::-1]) # reverse a string" 377 | ] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "id": "ceeff2b6", 382 | "metadata": {}, 383 | "source": [ 384 | "# Immutability\n", 385 | "Strings are immutable in Python. If we reassign a string in Python, it will overwrite the original string.\n", 386 | "[start:stop:step] is string slicing." 387 | ] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "id": "aacac8f9", 392 | "metadata": {}, 393 | "source": [ 394 | "# Built-In Functions and Methods\n", 395 | "We have been learning about built-in functions that Python can take on data, such as str(), int(), type(), print(), and float(). Some resources for built-in functions are [Python String Methods](https://www.w3schools.com/python/python_ref_string.asp) [Built-in Functions](https://docs.python.org/3/library/functions.html)\n" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 2, 401 | "id": "04d68a5e", 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "name": "stdout", 406 | "output_type": "stream", 407 | "text": [ 408 | "9\n" 409 | ] 410 | } 411 | ], 412 | "source": [ 413 | "print(len(\"hellooooo\"))" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 5, 419 | "id": "165b7ad5", 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "name": "stdout", 424 | "output_type": "stream", 425 | "text": [ 426 | "hellooooo\n", 427 | "hellooooo\n" 428 | ] 429 | } 430 | ], 431 | "source": [ 432 | "greet = \"hellooooo\"\n", 433 | "print(greet[:])\n", 434 | "print(greet[0:len(greet)])" 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "id": "1a8d9daf", 440 | "metadata": {}, 441 | "source": [ 442 | "A good code editor will show available methods after we add dot notation." 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 13, 448 | "id": "8010d6cf", 449 | "metadata": {}, 450 | "outputs": [ 451 | { 452 | "name": "stdout", 453 | "output_type": "stream", 454 | "text": [ 455 | "TO BE OR NOT TO BE\n", 456 | "To be or not to be\n", 457 | "3\n", 458 | "to me or not to me\n", 459 | "to be or not to be\n" 460 | ] 461 | } 462 | ], 463 | "source": [ 464 | "quote = \"to be or not to be\"\n", 465 | "print(quote.upper())\n", 466 | "print(quote.capitalize())\n", 467 | "print(quote.find('be')) # index of the word 'be'\n", 468 | "print(quote.replace('be', 'me')) # replace all occurances of 'be' with 'me'\n", 469 | "print(quote) # after the code above prints, it is removed from memory. The original string is not modified" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "id": "d595c60c", 475 | "metadata": {}, 476 | "source": [ 477 | "# Booleans\n", 478 | "A boolean can either be set to true or false. We can use them to control the flow of our program." 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 15, 484 | "id": "b7da5b70", 485 | "metadata": {}, 486 | "outputs": [ 487 | { 488 | "name": "stdout", 489 | "output_type": "stream", 490 | "text": [ 491 | "True\n" 492 | ] 493 | } 494 | ], 495 | "source": [ 496 | "name = \"Andrei\"\n", 497 | "isCool = True\n", 498 | "print(isCool)" 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 17, 504 | "id": "9ccd30b0", 505 | "metadata": {}, 506 | "outputs": [ 507 | { 508 | "name": "stdout", 509 | "output_type": "stream", 510 | "text": [ 511 | "True\n", 512 | "False\n" 513 | ] 514 | } 515 | ], 516 | "source": [ 517 | "# convert integers into boolean values\n", 518 | "print(bool(1))\n", 519 | "print(bool(0))" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "id": "9f433135", 525 | "metadata": {}, 526 | "source": [ 527 | "# DEVELOPER FUNDAMENTALS II\n", 528 | "As soon as Python sees a #, it adds a comment.\n", 529 | "\n", 530 | "Guidelines:\n", 531 | "* When you comment your code, you're adding valuable content to help another programmer or yourself when you review it later.\n", 532 | "* Add comments to explain complexity rather than for everything." 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": null, 538 | "id": "dc7e9119", 539 | "metadata": {}, 540 | "outputs": [], 541 | "source": [] 542 | } 543 | ], 544 | "metadata": { 545 | "kernelspec": { 546 | "display_name": "Python 3 (ipykernel)", 547 | "language": "python", 548 | "name": "python3" 549 | }, 550 | "language_info": { 551 | "codemirror_mode": { 552 | "name": "ipython", 553 | "version": 3 554 | }, 555 | "file_extension": ".py", 556 | "mimetype": "text/x-python", 557 | "name": "python", 558 | "nbconvert_exporter": "python", 559 | "pygments_lexer": "ipython3", 560 | "version": "3.8.11" 561 | } 562 | }, 563 | "nbformat": 4, 564 | "nbformat_minor": 5 565 | } 566 | -------------------------------------------------------------------------------- /jupyter-notebooks/object_oriented_programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "fc4ce5e7", 6 | "metadata": {}, 7 | "source": [ 8 | "In the following sections, feel free to use either Repl.it or your own development enviornment as you code along with Andrei." 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "73134672", 14 | "metadata": {}, 15 | "source": [ 16 | "# Advanced Python: Object-Oriented Programming\n", 17 | "- What is OOP?\n", 18 | "- What is OOP? Part 2\n", 19 | "- Creating Our Own Objects\n", 20 | "- Attributes and Methods\n", 21 | "- __ init __\n", 22 | "- @classmethod and @staticmethod\n", 23 | "- Reviewing What We Know So Far\n", 24 | "- DEVELOPER FUNDAMENTALS V\n", 25 | "- Encapsulation\n", 26 | "- Abstraction\n", 27 | "- Private vs. Public Variables\n", 28 | "- Inheritance\n", 29 | "- Polymorphism\n", 30 | "- super()\n", 31 | "- Object Introspection\n", 32 | "- Dunder Methods\n", 33 | "- Multiple Inheritance\n", 34 | "- MRO - Method Resolution Order" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "id": "f2b6d8c1", 40 | "metadata": {}, 41 | "source": [ 42 | "## What is OOP?\n", 43 | "In this section, we'll discuss object-oriented programming: what it is and why it's such an important topic for us to become great developers.\n", 44 | "\n", 45 | "Everything in Python in an object. Run the code below in your Python environment and examine the output. Notice that everything is built with the \"class\" keyword. We're able to use different methods on our objects to perform some actions on them. Objects have methods and attributes that we can access with the dot method.\n", 46 | "\n", 47 | "Object-oriented programming is exciting because it allows us to go beyond the data types that Python gives us. Python becomes even more powerful when we create our own objects through the class keyword. We can create our own data types with our own attributes and methods.\n", 48 | "\n", 49 | "As we write larger programs and our code gets more complicated, OOP is a programming paradigm that allows us to structure our code in a way that's easier to extend and maintain." 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 2, 55 | "id": "6a9c828a", 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "\n", 63 | "\n", 64 | "\n", 65 | "\n", 66 | "\n", 67 | "\n", 68 | "\n", 69 | "\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "print(type(None))\n", 75 | "print(type(True))\n", 76 | "print(type(5))\n", 77 | "print(type(5.5))\n", 78 | "print(type('hi'))\n", 79 | "print(type([]))\n", 80 | "print(type(()))\n", 81 | "print(type({}))" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "id": "ee8b9f30", 87 | "metadata": {}, 88 | "source": [ 89 | "## What is OOP? Part 2\n", 90 | "Programming started out as highly procedural. We can think of it as an explicit set of instructions. As programming has evolved, objects were introduced, as were methods and actions, so we can think in terms of models and blueprints. This gives us a better way to organize and run our programs. For more on how programming has evolved, check out [History of Programming Languages](https://en.wikipedia.org/wiki/History_of_programming_languages) on Wikipedia.\n", 91 | "\n", 92 | "Python can support OOP ideas, and we have seen that everything is an object because we use the \"class\" keyword. We can use this keyword to create our own objects. Convention is that objects in Python are capitalized and use camelCase, meaning that every new word starts with a capital letter." 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "id": "8ff45ba7", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "# Let's create our own object!\n", 111 | "class BigObject:\n", 112 | " pass\n", 113 | "print(type(BigObject)) # " 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "id": "56294f50", 119 | "metadata": {}, 120 | "source": [ 121 | "A class is a blueprint of what we want to create. From this blueprint, we can create different objects, using our class as a building block. The class can be instantiated, that is, creating different instances of objects. We create a class or a blueprint. When we create obj1 below, we instantiate the class. We can save a lot of time this way, instead of coding everything from scratch.\n", 122 | "\n", 123 | "The blueprint BigObject() is stored in memory, and when we create new objects, we go to memory where BigObject() is and then run that code.\n", 124 | "\n", 125 | "The language of object-oriented programming can be difficult to understand at first, but as we work with these concepts, the language becomes more familiar." 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 7, 131 | "id": "8cdfe130", 132 | "metadata": { 133 | "scrolled": true 134 | }, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "obj1 = BigObject() # instantiate\n", 146 | "print(type(obj1)) # " 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "id": "bbaf8f49", 152 | "metadata": {}, 153 | "source": [ 154 | "## Creating Our Own Objects\n", 155 | "Let's code our own class by mimicing conditions of building a game. \n", 156 | "\n", 157 | "The __ init __ method is a special method called a Dunder method, or a constructor method. It is automatically called every time we instantiate an object.\n", 158 | "\n", 159 | "Self refers to the PlayerCharacter. self.name will equal whatever the parameter is. In this case, we're going to give it the name parameter of \"Cindy\". \"self\" refers to whatever is left of the dot. We can create different objects with different attributes, such as name, age, etc.\n", 160 | "\n", 161 | "Don't worry if the syntax looks confusing at first." 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 44, 167 | "id": "2fbfe66e", 168 | "metadata": {}, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "<__main__.PlayerCharacter object at 0x7fb5ac7d1f10>\n", 175 | "44\n", 176 | "My name is Cindy\n", 177 | "done\n" 178 | ] 179 | } 180 | ], 181 | "source": [ 182 | "# Note that player1 and player2 are at different locations in memory.\n", 183 | "class PlayerCharacter:\n", 184 | " # Class Object Attribute\n", 185 | " membership = True # static attribute - all players have this attribute set to True\n", 186 | " def __init__(self, name, age):\n", 187 | " # only assign name and age if the PlayerCharacter instance is a member\n", 188 | " if(self.membership):\n", 189 | " self.name = name # attribute\n", 190 | " self.age = age # attribute\n", 191 | " def shout(self):\n", 192 | " print(f'My name is {self.name}')\n", 193 | " return('done')\n", 194 | " \n", 195 | " @classmethod # see lecture on @classmethod and @staticmethod below\n", 196 | " def adding_things(cls, num1, num2):\n", 197 | " return cls(\"Teddy\", num1 + num2)\n", 198 | " \n", 199 | " @staticmethod\n", 200 | " def adding_things2(num1, num2):\n", 201 | " return num1 + num2\n", 202 | " \n", 203 | "player1 = PlayerCharacter('Cindy', 44)\n", 204 | "print(player1)\n", 205 | "print(player1.age) # 44\n", 206 | "print(player1.shout()) # My name is Cindy done" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 37, 212 | "id": "59c11b84", 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "<__main__.PlayerCharacter object at 0x7fb5ac7cc9d0>\n", 220 | "Tom\n", 221 | "True\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "player2 = PlayerCharacter('Tom', 21)\n", 227 | "print(player2)\n", 228 | "print(player2.name) # Tom\n", 229 | "print(player2.membership)" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "id": "2f3be17d", 235 | "metadata": {}, 236 | "source": [ 237 | "## Attributes and Methods\n", 238 | "Object-oriented programming allows us to create our own objects, attributes, and methods. It allows us to write repeatable, well-organized, memory-efficient code. We group data (attributes) with methods (actions) to mimic a real-world situation.\n", 239 | "\n", 240 | "The membership attribute in the example above doesn't change -- it is the same across instances of the class.\n", 241 | "\n", 242 | "Because the name and age attributes change across the instances, we cannot run PlayerCharacter.name or PlayerCharacter.age. We can only run them on specific instances. When we use those instances, we also need to reference self." 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 38, 248 | "id": "8ed422d6", 249 | "metadata": { 250 | "scrolled": true 251 | }, 252 | "outputs": [ 253 | { 254 | "name": "stdout", 255 | "output_type": "stream", 256 | "text": [ 257 | "Help on PlayerCharacter in module __main__ object:\n", 258 | "\n", 259 | "class PlayerCharacter(builtins.object)\n", 260 | " | PlayerCharacter(name, age)\n", 261 | " | \n", 262 | " | Methods defined here:\n", 263 | " | \n", 264 | " | __init__(self, name, age)\n", 265 | " | Initialize self. See help(type(self)) for accurate signature.\n", 266 | " | \n", 267 | " | shout(self)\n", 268 | " | \n", 269 | " | ----------------------------------------------------------------------\n", 270 | " | Class methods defined here:\n", 271 | " | \n", 272 | " | adding_things(num1, num2) from builtins.type\n", 273 | " | \n", 274 | " | ----------------------------------------------------------------------\n", 275 | " | Data descriptors defined here:\n", 276 | " | \n", 277 | " | __dict__\n", 278 | " | dictionary for instance variables (if defined)\n", 279 | " | \n", 280 | " | __weakref__\n", 281 | " | list of weak references to the object (if defined)\n", 282 | " | \n", 283 | " | ----------------------------------------------------------------------\n", 284 | " | Data and other attributes defined here:\n", 285 | " | \n", 286 | " | membership = True\n", 287 | "\n" 288 | ] 289 | } 290 | ], 291 | "source": [ 292 | "# Running a function like help on an object will give us the entire blueprint.\n", 293 | "help(player1)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "markdown", 298 | "id": "e0332952", 299 | "metadata": {}, 300 | "source": [ 301 | "## __ init __\n", 302 | "The __ init __ gives us a lot of control. It initializes the object and assign values to new members of the class when the new objects are created.\n", 303 | "\n", 304 | "We can also give default parameters, such as def __ init __(self, name=\"anonymous\", age=0)." 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "id": "3275ac88", 310 | "metadata": {}, 311 | "source": [ 312 | "## @classmethod and @staticmethod\n", 313 | "We can use a decorator to write functions. Instead of using self, we use cls. We can use this without instantiating a class. It's a method on the actual class. Most of our classes won't use a @classmethod, but we can use cls to instantiate an object.\n", 314 | "\n", 315 | "@staticmethod works the same way, but we don't have access to the cls. We can use this when we don't care about the attributes or the class state and don't need to modify them." 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": 45, 321 | "id": "da79d1ee", 322 | "metadata": {}, 323 | "outputs": [], 324 | "source": [ 325 | "@classmethod\n", 326 | "def adding_things(cls, num1, num2):\n", 327 | " return cls(\"Teddy\", num1 + num2)\n", 328 | "\n", 329 | "@staticmethod\n", 330 | "def adding_things2(num1, num2):\n", 331 | " return num1 + num2" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 46, 337 | "id": "497d7353", 338 | "metadata": {}, 339 | "outputs": [ 340 | { 341 | "name": "stdout", 342 | "output_type": "stream", 343 | "text": [ 344 | "5\n" 345 | ] 346 | } 347 | ], 348 | "source": [ 349 | "# Let's create a new player character using cls\n", 350 | "player3 = PlayerCharacter.adding_things(2,3)\n", 351 | "print(player3.age)" 352 | ] 353 | }, 354 | { 355 | "cell_type": "markdown", 356 | "id": "6d884d97", 357 | "metadata": {}, 358 | "source": [ 359 | "## Reviewing What We Know So Far\n", 360 | "We use classes to incorporate object-oriented programming paradigms. We learned about class object attributes and methods and how __ init __ runs on every object to customize them. We also learned how to call methods on a class without instantiating it into an object.\n", 361 | "\n", 362 | "The idea of object-oriented programming requires some time to grasp. We'll continue to practice so we can start using this paradigm in our programs." 363 | ] 364 | }, 365 | { 366 | "cell_type": "markdown", 367 | "id": "09525baa", 368 | "metadata": {}, 369 | "source": [ 370 | "## DEVELOPER FUNDAMENTALS V\n", 371 | "With programming, it's important to test our assumptions with code. For instance, when we learned about \"self\", we can try to return self from the run function above and see what happens. As you try things out in your Python environment, think about what you think will happen. Change a line of code, see what happens, and record your observations. The more you test things, the more you will learn." 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "id": "08ab7862", 377 | "metadata": {}, 378 | "source": [ 379 | "The four pillars of object-oriented programming, which we will explore over the next several lectures, are:\n", 380 | "1. Inheritance\n", 381 | "2. Abstraction\n", 382 | "3. Encapsulation\n", 383 | "4. Polymorphism" 384 | ] 385 | }, 386 | { 387 | "attachments": {}, 388 | "cell_type": "markdown", 389 | "id": "02861e48", 390 | "metadata": {}, 391 | "source": [ 392 | "## Encapsulation\n", 393 | "When learning about object-oriented programming, there are four pillars -- four things that OOP does really well.\n", 394 | "\n", 395 | "Encapsulation is the binding of functions and data that we \"encapsulate\" into one big object that users, code, or other machines can interact with. Our PlayerCharacter class is one such example, in which we have grouped attributes and methods into a class. We have packaged these up as a blueprint and have functionality available to us.\n", 396 | "\n", 397 | "If we didn't need functionality, we could create a dictionary instead, so with the idea of OOP, we combine and package attributes and methods that mimic our world that is full of data and actions." 398 | ] 399 | }, 400 | { 401 | "cell_type": "markdown", 402 | "id": "ed09abce", 403 | "metadata": {}, 404 | "source": [ 405 | "## Abstraction\n", 406 | "Abstration means hiding information and giving the programmer or user access only to what is necessary. In the example below, we are using abstraction when we use the len method. We receive access to the method, but we don't necessarily know the details around the inner workings of it. We just know that it works in a certain way and that we can use it." 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 51, 412 | "id": "d09d428b", 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "name": "stdout", 417 | "output_type": "stream", 418 | "text": [ 419 | "3\n" 420 | ] 421 | } 422 | ], 423 | "source": [ 424 | "tup = (1,2,3)\n", 425 | "print(len(tup))" 426 | ] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "id": "0a18287a", 431 | "metadata": {}, 432 | "source": [ 433 | "## Private vs. Public Variables\n", 434 | "With each data type, we have a number of dunder methods. They're there to let us know not to overwrite them. A private field is important in Python. Even though we can overwrite things, it's bad practice. By using private attributes, we can abstract things away and ensure that the user isn't going to break our code." 435 | ] 436 | }, 437 | { 438 | "cell_type": "markdown", 439 | "id": "01c484f4", 440 | "metadata": {}, 441 | "source": [ 442 | "## Inheritance\n", 443 | "Inheritance allows new objects to take on the properties of existing objects. They can inherit classes.\n", 444 | "\n", 445 | "If we don't have attributes we want to assign to a user, we don't need the __ init __ method.\n", 446 | "\n", 447 | "In the example below, we are going to give all of our users access to the sign_in method since they all need to be signed in to play the game, so we'll pass the User class to them.\n", 448 | "\n", 449 | "The idea is that we have a parent class (User) and child classes (Wizard and Archer). Sometimes child classes are called subclasses or derived classes." 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 71, 455 | "id": "96d28ccd", 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "Attacking with the power of fire.\n", 463 | "None\n", 464 | "Attacking with arrows: arrows left - 100\n", 465 | "None\n" 466 | ] 467 | } 468 | ], 469 | "source": [ 470 | "# Let's create a new game with different types of users that can be wizzards, archers, ogres, etc.\n", 471 | "# We want to give all of our users \n", 472 | "class User:\n", 473 | " def sign_in(self):\n", 474 | " print('logged in')\n", 475 | "\n", 476 | "class Wizzard(User):\n", 477 | " def __init__(self, name, power):\n", 478 | " self.name = name\n", 479 | " self.power = power\n", 480 | " \n", 481 | " def attack(self):\n", 482 | " print(f'Attacking with the power of {self.power}.')\n", 483 | "\n", 484 | "class Archer(User):\n", 485 | " def __init__(self, name, num_arrows):\n", 486 | " self.name = name\n", 487 | " self.num_arrows = num_arrows\n", 488 | " \n", 489 | " def attack(self):\n", 490 | " print(f'Attacking with arrows: arrows left - {self.num_arrows}')\n", 491 | "\n", 492 | "wizard1 = Wizzard('Merlin', 'fire')\n", 493 | "archer1 = Archer('Robin', 100)\n", 494 | "print(wizard1.attack())\n", 495 | "print(archer1.attack())" 496 | ] 497 | }, 498 | { 499 | "cell_type": "markdown", 500 | "id": "f95ba6c0", 501 | "metadata": {}, 502 | "source": [ 503 | "## Inheritance 2" 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "id": "c3bfef72", 509 | "metadata": {}, 510 | "source": [ 511 | "## Polymorphism" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "id": "c4a367be", 517 | "metadata": {}, 518 | "source": [ 519 | "## super()" 520 | ] 521 | }, 522 | { 523 | "cell_type": "markdown", 524 | "id": "29477e98", 525 | "metadata": {}, 526 | "source": [ 527 | "## Object Introspection" 528 | ] 529 | }, 530 | { 531 | "cell_type": "markdown", 532 | "id": "f1b7c071", 533 | "metadata": {}, 534 | "source": [ 535 | "## Dunder Methods" 536 | ] 537 | }, 538 | { 539 | "cell_type": "markdown", 540 | "id": "0cfd2faf", 541 | "metadata": {}, 542 | "source": [ 543 | "## Multiple Inheritance" 544 | ] 545 | }, 546 | { 547 | "cell_type": "markdown", 548 | "id": "185261bd", 549 | "metadata": {}, 550 | "source": [ 551 | "## MRO - Method Resolution Order" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": null, 557 | "id": "b13c57ce", 558 | "metadata": {}, 559 | "outputs": [], 560 | "source": [] 561 | } 562 | ], 563 | "metadata": { 564 | "kernelspec": { 565 | "display_name": "Python 3 (ipykernel)", 566 | "language": "python", 567 | "name": "python3" 568 | }, 569 | "language_info": { 570 | "codemirror_mode": { 571 | "name": "ipython", 572 | "version": 3 573 | }, 574 | "file_extension": ".py", 575 | "mimetype": "text/x-python", 576 | "name": "python", 577 | "nbconvert_exporter": "python", 578 | "pygments_lexer": "ipython3", 579 | "version": "3.8.11" 580 | } 581 | }, 582 | "nbformat": 4, 583 | "nbformat_minor": 5 584 | } 585 | -------------------------------------------------------------------------------- /jupyter-notebooks/python_basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "541944b6", 6 | "metadata": {}, 7 | "source": [ 8 | "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/zero-to-mastery/Complete-Python-Developer-Manual/blob/main/jupyter-notebooks/python_basics.ipynb)" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "a223de83", 14 | "metadata": {}, 15 | "source": [ 16 | "To get the most out of this course, follow along with Andrei and code along with the videos. The course will go over how to install Python on your computer later. To start, you can use Python in [Repl.it](https://replit.com/)." 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "dbdd5340", 22 | "metadata": {}, 23 | "source": [ 24 | "## Learning Python\n", 25 | "\n", 26 | "There are four key things you need to master for learning a programming language, including Python:\n", 27 | "1. Terms: things like statements, variables, etc. so you can talk to other programmers\n", 28 | "2. Data types: values a program can hold to store information\n", 29 | "3. Actions: programming is a way to tell our machines how to store and retrieve data and perform actions on that data\n", 30 | "4. Best practices: we need to learn the good ways to write programs with a solid structure" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "id": "f8e6be39", 36 | "metadata": {}, 37 | "source": [ 38 | "## Python Data Types\n", 39 | "We will use [Repl.it](https://replit.com/) to write our first Python code so we don't have to worry about installation at first.\n", 40 | "\n", 41 | "Python has several data types. We'll list them out and then discuss them individually. Notice that the REPL will highlight these data types, which clues us in that there is something special about them.\n", 42 | "\n", 43 | "A data type represents a value in Python, and writing a program allows us to take actions on these data types. Examples of actions include creating, changing/manipulating, storing, and removing data types.\n", 44 | "\n", 45 | "Python Data Types:\n", 46 | "- `int`: integer (a whole number)\n", 47 | "- `float`: floating point number\n", 48 | "- `bool`: boolean (True or False)\n", 49 | "- `str`: string\n", 50 | "- `list`\n", 51 | "- `tuple`\n", 52 | "- `set`\n", 53 | "- `dict`\n", 54 | "- `None`: the absence of value\n", 55 | "\n", 56 | "We can also create our own custom data types using something called \"classes\". We'll explore how to do this later in the course.\n", 57 | "\n", 58 | "We also have specialized data types. They are special packages and modules we can use from libraries. They're a little something extra, an extension, beyond a standard Python data type." 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "id": "c1ec26da", 64 | "metadata": {}, 65 | "source": [ 66 | "## How to Succeed\n", 67 | "\n", 68 | "In order to get the most out of this course, please code along with Andrei throughout the next sections. Hands-on practice is the best way to develop your skills." 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "63ab0f8a", 74 | "metadata": {}, 75 | "source": [ 76 | "## Numbers\n", 77 | "\n", 78 | "This lecture will cover two data types: `int` and `float`. An integer is a number, such as 3, 4, or 5. We can use integers to perform mathematical operations in a program. The following code prints new numbers based on the mathematical operations we're performing on two whole numbers.\n", 79 | "\n", 80 | "A `float` is a floating point number, or a number with a decimal point, such as 0.5. We need to make this distinction because a float takes up more space in memory compared to an integer on our machines. Adding an integer and a float will result in a float as will adding two floats together, even if the result is a whole number. For more information on floating point numbers, check out this [Floating Point Numbers video](https://www.youtube.com/watch?v=PZRI1IfStY0).\n", 81 | "\n", 82 | "If the syntax is intimidating, don't worry! As we work with these concepts more, it will become more familiar. And, if you're ever confused about a Python data type or anything else that has to do with the language, you can learn more about it in the [Python documentation](https://docs.python.org/3/contents.html)." 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 2, 88 | "id": "5c7188ad", 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "6\n", 96 | "-2\n", 97 | "8\n", 98 | "0.5\n", 99 | "8\n", 100 | "0\n", 101 | "1\n", 102 | "2\n", 103 | "\n", 104 | "\n", 105 | "\n", 106 | "\n", 107 | "\n", 108 | "\n" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | "print(2 + 4) # addition - result is 6\n", 114 | "print(2 - 4) # subtraction - result is -2\n", 115 | "print(2 * 4) # multiplication - result is 8\n", 116 | "print(2 / 4) # division - result is 0.5\n", 117 | "# print(2 / 0) # division by zero! - this will give us an error!\n", 118 | "print(2 ** 3) # exponent - 2 to the 3rd power is 8\n", 119 | "print(3 // 4) # double division returns an integer, rounded down to an integer - result is 0\n", 120 | "print(5 // 4) # result is 1\n", 121 | "# The modulo operator represents the remainder of division. 6 divided by 4 is 1 with a remainder of 2.\n", 122 | "print(6 % 4)\n", 123 | "# type is another action we can perform to check a data type. Content in inner brackets is evaluated first\n", 124 | "print(type(2 + 4)) # integer\n", 125 | "print(type(2 / 4)) # float\n", 126 | "print(type(5.001)) # float\n", 127 | "print(type(10 + 1.1)) # float\n", 128 | "print(type(9.9 + 1.1)) # float\n", 129 | "print(type(0)) # integer" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "id": "e5bf3c30", 135 | "metadata": {}, 136 | "source": [ 137 | "## Math Functions" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "id": "c7c7f051", 143 | "metadata": {}, 144 | "source": [ 145 | "There are specific math functions built into Python that we can use on integers and floating point numbers. Functions are actions we can take on data, such as `print()` and `type()`. Try the following out in your REPL.\n", 146 | "\n", 147 | "See this article on [Python Mathematical Functions](https://www.programiz.com/python-programming/modules/math) for more and try Googling for Python math functions as well. Also, keep in mind that you'll never need all of the math functions in Python. We'll go over some of the important once in this course." 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 1, 153 | "id": "5a569317", 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "3\n", 161 | "4\n", 162 | "20\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "print(round(3.1)) # rounding down the number - result is 3\n", 168 | "print(round(3.9)) # result is 4\n", 169 | "print(abs(-20)) # returns absolute value of the argument (no negative numbers)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "id": "80f5cdd7", 175 | "metadata": {}, 176 | "source": [ 177 | "## DEVELOPER FUNDAMENTALS I\n", 178 | "Developer fundamentals will be sprinkled throughout the course and will go over common mistakes in the hopes that we can avoid them as well as tips on how to be a great Python developer.\n", 179 | "\n", 180 | "The first developer fundamental is: Don't read the dictionary. This means, don't worry about every little thing. Look up what we need to in the documentation, but don't try to read or memorize everything. Make sure you understand what exists, what you can use, and learn how to write good Google queries to find the information we need. This ensures we're focusing on the things that actually matter." 181 | ] 182 | }, 183 | { 184 | "cell_type": "markdown", 185 | "id": "bf33aee2", 186 | "metadata": {}, 187 | "source": [ 188 | "## Operator Precedence\n", 189 | "Different operators have precedence over others, like multiplication gets evaluated before addition. \n", 190 | "\n", 191 | "Whatever is wrapped in parentheses gets evaluated first. Exponents get evaluated next, then multiplication and division, and finally, addition and subtraction.\n", 192 | "\n", 193 | "Try these out in your REPL along with the exercises in the following lecture." 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 24, 199 | "id": "39abbccb", 200 | "metadata": { 201 | "scrolled": true 202 | }, 203 | "outputs": [ 204 | { 205 | "name": "stdout", 206 | "output_type": "stream", 207 | "text": [ 208 | "8\n", 209 | "21\n" 210 | ] 211 | } 212 | ], 213 | "source": [ 214 | "print(20 - 3 * 4) # 3 * 4 gets evaluated before 20 - 3 - result is 8\n", 215 | "print((20 - 3) + 2 ** 2) # result is 21" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "id": "7dbdee3f", 221 | "metadata": {}, 222 | "source": [ 223 | "## Bin and Complex\n", 224 | "\n", 225 | "There is an extra data type we didn't discuss. It's called `complex` and is a third type of number. We won't focus on it because it's only significant if we're doing complicated math.\n", 226 | "\n", 227 | "Integers and floats get stored as binary numbers. There is an action called bin that we can use to return a binary representation of an integer." 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 32, 233 | "id": "1875edd5", 234 | "metadata": { 235 | "scrolled": true 236 | }, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "0b101\n", 243 | "0b1000\n", 244 | "5\n" 245 | ] 246 | } 247 | ], 248 | "source": [ 249 | "print(bin(5)) # binary number for 5 is 101\n", 250 | "print(bin(8))\n", 251 | "print(int('0b101', 2)) # convert this base 2 binary number to an integer" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "id": "f957de12", 257 | "metadata": {}, 258 | "source": [ 259 | "## Variables\n", 260 | "All programming languages have variables so that we can store information. Assigning a value is also known as binding. The number below is stored as a binary representation (zeros and ones).\n", 261 | "\n", 262 | "The following are best practices to use when writing Python variables:\n", 263 | "* They should be snake_case\n", 264 | "* Start with lowercase or underscore\n", 265 | "* Use letters, numbers, or underscores\n", 266 | "* They are case-sensitive\n", 267 | "* Don't overwrite keywords\n", 268 | "* Make variable names descriptive\n", 269 | "\n", 270 | "Other things to be aware of:\n", 271 | "* Constants should be in capitals, i.e., `PI = 3.14`\n", 272 | "* There are dunder variables that start with two underscores that should not be assigned\n", 273 | "* Underscore in Python singnifies a private variable (`_user_iq`)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 2, 279 | "id": "86e4a263", 280 | "metadata": {}, 281 | "outputs": [ 282 | { 283 | "name": "stdout", 284 | "output_type": "stream", 285 | "text": [ 286 | "47.5\n" 287 | ] 288 | } 289 | ], 290 | "source": [ 291 | "user_iq = 190\n", 292 | "user_age = user_iq / 4\n", 293 | "print(user_age)" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 3, 299 | "id": "f86b84e0", 300 | "metadata": { 301 | "scrolled": true 302 | }, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "2\n" 309 | ] 310 | } 311 | ], 312 | "source": [ 313 | "a, b, c = 1, 2, 3\n", 314 | "print(b)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "id": "02dfa7a3", 320 | "metadata": {}, 321 | "source": [ 322 | "## Expressions vs. Statements\n", 323 | "\n", 324 | "An expression is the right side of a statement. A statement is an entire line of code that performs some sort of action.\n", 325 | "\n", 326 | "As in the previous example, `user_iq / 4` is an expression. `user_age = user_iq / 4` and `user_iq = 190` are statements." 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "id": "06d8700f", 332 | "metadata": {}, 333 | "source": [ 334 | "## Augmented Assignment Operator\n", 335 | "\n", 336 | "In the example below, `some_value += 2` makes use of the augmented assignment operator." 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 33, 342 | "id": "579c4e4a", 343 | "metadata": {}, 344 | "outputs": [], 345 | "source": [ 346 | "some_value = 5\n", 347 | "some_value += 2" 348 | ] 349 | }, 350 | { 351 | "cell_type": "markdown", 352 | "id": "f5023e0e", 353 | "metadata": {}, 354 | "source": [ 355 | "## Strings (`str`)\n", 356 | "A string is a piece of text and can be encapsulated in single or double quotes." 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 3, 362 | "id": "ad9535fb", 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "\n" 370 | ] 371 | } 372 | ], 373 | "source": [ 374 | "print(type('hellooooo'))" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 8, 380 | "id": "abf5ab83", 381 | "metadata": {}, 382 | "outputs": [], 383 | "source": [ 384 | "username = 'supercoder'\n", 385 | "password = 'supersecret'\n", 386 | "long_string = '''\n", 387 | "WOW\n", 388 | " 00\n", 389 | "---\n", 390 | "'''" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 9, 396 | "id": "65e6a13e", 397 | "metadata": {}, 398 | "outputs": [ 399 | { 400 | "name": "stdout", 401 | "output_type": "stream", 402 | "text": [ 403 | "\n", 404 | "WOW\n", 405 | " 00\n", 406 | "---\n", 407 | "\n" 408 | ] 409 | } 410 | ], 411 | "source": [ 412 | "print(long_string)" 413 | ] 414 | }, 415 | { 416 | "cell_type": "markdown", 417 | "id": "f11ce78a", 418 | "metadata": {}, 419 | "source": [ 420 | "## String Concatenation\n", 421 | "\n", 422 | "String concatenation is simply adding strings together. This only works with strings in Python. If we added the number `5 + \"hello\"`, we would get a TypeError." 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 37, 428 | "id": "360bec82", 429 | "metadata": {}, 430 | "outputs": [ 431 | { 432 | "name": "stdout", 433 | "output_type": "stream", 434 | "text": [ 435 | "hellooooo Andrei\n" 436 | ] 437 | } 438 | ], 439 | "source": [ 440 | "print('hellooooo' + ' Andrei')" 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "id": "0367f71a", 446 | "metadata": {}, 447 | "source": [ 448 | "## Type Conversion\n", 449 | "With Python, we can convert one data type to another. For instance, we can convert a string to an integer." 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 11, 455 | "id": "f0ad0c1a", 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "\n" 463 | ] 464 | } 465 | ], 466 | "source": [ 467 | "print(type(str(100)))" 468 | ] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "execution_count": 12, 473 | "id": "7c20a0b0", 474 | "metadata": {}, 475 | "outputs": [ 476 | { 477 | "name": "stdout", 478 | "output_type": "stream", 479 | "text": [ 480 | "\n" 481 | ] 482 | } 483 | ], 484 | "source": [ 485 | "print(type(int(str(100))))" 486 | ] 487 | }, 488 | { 489 | "cell_type": "markdown", 490 | "id": "cff587dd", 491 | "metadata": {}, 492 | "source": [ 493 | "## Escape Sequence\n", 494 | "The following are escape sequences -- a backslash -- that allows us to interpret whatever comes after it as a string." 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 14, 500 | "id": "ad66d0fa", 501 | "metadata": {}, 502 | "outputs": [ 503 | { 504 | "name": "stdout", 505 | "output_type": "stream", 506 | "text": [ 507 | "It's kind of sunny\n" 508 | ] 509 | } 510 | ], 511 | "source": [ 512 | "weather = 'It\\'s kind of sunny'\n", 513 | "print(weather)" 514 | ] 515 | }, 516 | { 517 | "cell_type": "code", 518 | "execution_count": 15, 519 | "id": "e235b9df", 520 | "metadata": {}, 521 | "outputs": [ 522 | { 523 | "name": "stdout", 524 | "output_type": "stream", 525 | "text": [ 526 | "\t weather\n" 527 | ] 528 | } 529 | ], 530 | "source": [ 531 | "print('\\t weather')" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": 17, 537 | "id": "0b7c9d83", 538 | "metadata": {}, 539 | "outputs": [ 540 | { 541 | "name": "stdout", 542 | "output_type": "stream", 543 | "text": [ 544 | "--\n", 545 | "Hope you have a good day\n" 546 | ] 547 | } 548 | ], 549 | "source": [ 550 | "print('--\\nHope you have a good day')" 551 | ] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "id": "166d1d2e", 556 | "metadata": {}, 557 | "source": [ 558 | "## Formatted Strings\n", 559 | "Formatted strings allow us to use strings in a dynamic way instead of hard-coding everything. This is important when we are writing programs to work with users and with databases.\n", 560 | "\n", 561 | "In general, the formatted string is recommended now (f-string) versus `.format`. Try the exercises below in your own REPL." 562 | ] 563 | }, 564 | { 565 | "cell_type": "code", 566 | "execution_count": 41, 567 | "id": "784d1e22", 568 | "metadata": {}, 569 | "outputs": [ 570 | { 571 | "name": "stdout", 572 | "output_type": "stream", 573 | "text": [ 574 | "Hi, Johnny\n", 575 | "Hi Johnny. You are 55 years old.\n", 576 | "Hi Johnny. You are 55 years old.\n", 577 | "Hi Johnny. You are 55 years old.\n" 578 | ] 579 | } 580 | ], 581 | "source": [ 582 | "name = 'Johnny'\n", 583 | "age = 55\n", 584 | "print(\"Hi, \" + name)\n", 585 | "print(f\"Hi {name}. You are {age} years old.\") # preferred - available from Python 3.6\n", 586 | "print(\"Hi {}. You are {} years old.\".format(name, age)) # Python 3.5 and below syntax\n", 587 | "print(\"Hi {0}. You are {1} years old.\".format(name, age))" 588 | ] 589 | }, 590 | { 591 | "cell_type": "markdown", 592 | "id": "192149dc", 593 | "metadata": {}, 594 | "source": [ 595 | "## String Indexes\n", 596 | "\n", 597 | "`str` is an ordered sequence of characters that are stored in memory in same order and can be accessed by indeces." 598 | ] 599 | }, 600 | { 601 | "cell_type": "code", 602 | "execution_count": 44, 603 | "id": "a0a399cd", 604 | "metadata": {}, 605 | "outputs": [ 606 | { 607 | "name": "stdout", 608 | "output_type": "stream", 609 | "text": [ 610 | "A\n", 611 | "An\n", 612 | "Ad\n", 613 | "ndrei\n", 614 | "And\n", 615 | "Ade\n", 616 | "Andrei\n", 617 | "e\n", 618 | "ierdnA\n" 619 | ] 620 | } 621 | ], 622 | "source": [ 623 | "name = \"Andrei\"\n", 624 | "print(name[0])\n", 625 | "print(name[0:2])\n", 626 | "print(name[0:4:2])\n", 627 | "print(name[1:])\n", 628 | "print(name[:3])\n", 629 | "print(name[::2])\n", 630 | "print(name[::1])\n", 631 | "print(name[-2])\n", 632 | "print(name[::-1]) # reverse a string" 633 | ] 634 | }, 635 | { 636 | "cell_type": "markdown", 637 | "id": "ceeff2b6", 638 | "metadata": {}, 639 | "source": [ 640 | "## Immutability\n", 641 | "Strings are immutable in Python. If we reassign a string in Python, it will overwrite the original string.\n", 642 | "`[start:stop:step]` is string slicing." 643 | ] 644 | }, 645 | { 646 | "cell_type": "markdown", 647 | "id": "aacac8f9", 648 | "metadata": {}, 649 | "source": [ 650 | "## Built-In Functions and Methods\n", 651 | "\n", 652 | "We have been learning about built-in functions that Python can take on data, such as `str()`, `int()`, `type()`, `print()`, and `float()`. Some resources for built-in functions are [Python String Methods](https://www.w3schools.com/python/python_ref_string.asp) [Built-in Functions](https://docs.python.org/3/library/functions.html)\n" 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": 2, 658 | "id": "04d68a5e", 659 | "metadata": {}, 660 | "outputs": [ 661 | { 662 | "name": "stdout", 663 | "output_type": "stream", 664 | "text": [ 665 | "9\n" 666 | ] 667 | } 668 | ], 669 | "source": [ 670 | "print(len(\"hellooooo\"))" 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": 5, 676 | "id": "165b7ad5", 677 | "metadata": {}, 678 | "outputs": [ 679 | { 680 | "name": "stdout", 681 | "output_type": "stream", 682 | "text": [ 683 | "hellooooo\n", 684 | "hellooooo\n" 685 | ] 686 | } 687 | ], 688 | "source": [ 689 | "greet = \"hellooooo\"\n", 690 | "print(greet[:])\n", 691 | "print(greet[0:len(greet)])" 692 | ] 693 | }, 694 | { 695 | "cell_type": "markdown", 696 | "id": "1a8d9daf", 697 | "metadata": {}, 698 | "source": [ 699 | "A good code editor will show available methods after we add dot notation." 700 | ] 701 | }, 702 | { 703 | "cell_type": "code", 704 | "execution_count": 13, 705 | "id": "8010d6cf", 706 | "metadata": {}, 707 | "outputs": [ 708 | { 709 | "name": "stdout", 710 | "output_type": "stream", 711 | "text": [ 712 | "TO BE OR NOT TO BE\n", 713 | "To be or not to be\n", 714 | "3\n", 715 | "to me or not to me\n", 716 | "to be or not to be\n" 717 | ] 718 | } 719 | ], 720 | "source": [ 721 | "quote = \"to be or not to be\"\n", 722 | "print(quote.upper())\n", 723 | "print(quote.capitalize())\n", 724 | "print(quote.find('be')) # index of the word 'be'\n", 725 | "print(quote.replace('be', 'me')) # replace all occurances of 'be' with 'me'\n", 726 | "print(quote) # after the code above prints, it is removed from memory. The original string is not modified" 727 | ] 728 | }, 729 | { 730 | "cell_type": "markdown", 731 | "id": "d595c60c", 732 | "metadata": {}, 733 | "source": [ 734 | "## Booleans\n", 735 | "A boolean can either be set to true or false. We can use them to control the flow of our program." 736 | ] 737 | }, 738 | { 739 | "cell_type": "code", 740 | "execution_count": 15, 741 | "id": "b7da5b70", 742 | "metadata": {}, 743 | "outputs": [ 744 | { 745 | "name": "stdout", 746 | "output_type": "stream", 747 | "text": [ 748 | "True\n" 749 | ] 750 | } 751 | ], 752 | "source": [ 753 | "name = \"Andrei\"\n", 754 | "is_cool = True\n", 755 | "print(is_cool)" 756 | ] 757 | }, 758 | { 759 | "cell_type": "code", 760 | "execution_count": 17, 761 | "id": "9ccd30b0", 762 | "metadata": {}, 763 | "outputs": [ 764 | { 765 | "name": "stdout", 766 | "output_type": "stream", 767 | "text": [ 768 | "True\n", 769 | "False\n" 770 | ] 771 | } 772 | ], 773 | "source": [ 774 | "# convert integers into boolean values\n", 775 | "print(bool(1))\n", 776 | "print(bool(0))" 777 | ] 778 | }, 779 | { 780 | "cell_type": "markdown", 781 | "id": "9f433135", 782 | "metadata": {}, 783 | "source": [ 784 | "## DEVELOPER FUNDAMENTALS II\n", 785 | "As soon as Python sees a #, it adds a comment.\n", 786 | "\n", 787 | "Guidelines for comments:\n", 788 | "* When you comment your code, you're adding valuable content to help another programmer or yourself when you review it later.\n", 789 | "* Add comments to explain complexity rather than for everything." 790 | ] 791 | }, 792 | { 793 | "cell_type": "markdown", 794 | "id": "842f460c", 795 | "metadata": {}, 796 | "source": [ 797 | "## Lists\n", 798 | "A list is an ordered sequence of objects that can be of any type. We denote lists with square brackets. We can have different objects inside of these square brackets.\n", 799 | "\n", 800 | "If you're familiar with other programming languages, you might have heard of arrays. Lists are similar, but we'll talk about the differences later in the course.\n", 801 | "\n", 802 | "In Python, we can have lists with mixed data types, as with `li3` below.\n", 803 | "\n", 804 | "The list is the first data structure we're learning. Data structures are ways for us to organize information in a program. Think of them as a container around our data with different pros and cons." 805 | ] 806 | }, 807 | { 808 | "cell_type": "code", 809 | "execution_count": 1, 810 | "id": "4f8c66f4", 811 | "metadata": {}, 812 | "outputs": [], 813 | "source": [ 814 | "li = [1, 2, 3, 4, 5]\n", 815 | "li2 = ['a', 'b', 'c']\n", 816 | "li3 = [1, 2, 'a', True, 1.67]" 817 | ] 818 | }, 819 | { 820 | "cell_type": "markdown", 821 | "id": "68c892d9", 822 | "metadata": {}, 823 | "source": [ 824 | "We can access items in a list similar to how we accessed characters in a string. If we try to access a third item in the list below, we'll get a \"list index out of range\" error since a third item doesn't exist." 825 | ] 826 | }, 827 | { 828 | "cell_type": "code", 829 | "execution_count": 3, 830 | "id": "5130a1b8", 831 | "metadata": {}, 832 | "outputs": [ 833 | { 834 | "name": "stdout", 835 | "output_type": "stream", 836 | "text": [ 837 | "notebooks\n", 838 | "sunglasses\n" 839 | ] 840 | } 841 | ], 842 | "source": [ 843 | "amazon_cart = ['notebooks', 'sunglasses']\n", 844 | "print(amazon_cart[0])\n", 845 | "print(amazon_cart[1])" 846 | ] 847 | }, 848 | { 849 | "cell_type": "markdown", 850 | "id": "9f9dd49c", 851 | "metadata": {}, 852 | "source": [ 853 | "## List Slicing\n", 854 | "We saw square brackets earlier, when we were working with strings. We can use list slicing as we did with string slicing earlier in the course.\n", 855 | "\n", 856 | "Unlike strings, lists are mutable. We can change our `amazon_cart`." 857 | ] 858 | }, 859 | { 860 | "cell_type": "code", 861 | "execution_count": 3, 862 | "id": "bd240859", 863 | "metadata": {}, 864 | "outputs": [ 865 | { 866 | "name": "stdout", 867 | "output_type": "stream", 868 | "text": [ 869 | "['notebooks', 'sunglasses', 'toys', 'grapes']\n", 870 | "['notebooks', 'sunglasses']\n", 871 | "['notebooks', 'toys']\n", 872 | "4\n" 873 | ] 874 | } 875 | ], 876 | "source": [ 877 | "amazon_cart2 = ['notebooks', 'sunglasses', 'toys', 'grapes']\n", 878 | "print(amazon_cart2)\n", 879 | "# Remember: with slicing, the second number is NOT inclusive\n", 880 | "print(amazon_cart2[0:2]) # get first two items in the cart\n", 881 | "print(amazon_cart2[0::2])\n", 882 | "print(len(amazon_cart2)) # We can also get the length of the list easily" 883 | ] 884 | }, 885 | { 886 | "cell_type": "code", 887 | "execution_count": 17, 888 | "id": "5eaac74e", 889 | "metadata": {}, 890 | "outputs": [ 891 | { 892 | "name": "stdout", 893 | "output_type": "stream", 894 | "text": [ 895 | "['laptop', 'sunglasses', 'toys', 'grapes']\n", 896 | "['laptop', 'sunglasses', 'toys']\n" 897 | ] 898 | } 899 | ], 900 | "source": [ 901 | "# Let's change our list!\n", 902 | "amazon_cart2[0] = \"laptop\"\n", 903 | "print(amazon_cart2)\n", 904 | "print(amazon_cart2[0:3]) # get item 1 - 3, not including 3" 905 | ] 906 | }, 907 | { 908 | "cell_type": "markdown", 909 | "id": "f556684a", 910 | "metadata": {}, 911 | "source": [ 912 | "Every time we slice a list, we create a new copy of our list. But, if we set our list equal to another list, if we modify the new list, we'll modify our old list as well.\n", 913 | "\n", 914 | "This happens because we're setting the lists equal to one another, and they're pointing to the same place in memory. This is different than when we slice a list because slicing makes a copy whereas when we set two lists equal to one another, we're creating conditions under which both lists will be modified." 915 | ] 916 | }, 917 | { 918 | "cell_type": "code", 919 | "execution_count": 20, 920 | "id": "a759e18f", 921 | "metadata": {}, 922 | "outputs": [ 923 | { 924 | "name": "stdout", 925 | "output_type": "stream", 926 | "text": [ 927 | "['gum', 'sunglasses', 'toys', 'grapes']\n", 928 | "['gum', 'sunglasses', 'toys', 'grapes']\n" 929 | ] 930 | } 931 | ], 932 | "source": [ 933 | "new_cart = amazon_cart2\n", 934 | "new_cart[0] = 'gum'\n", 935 | "print(amazon_cart2)\n", 936 | "print(new_cart)" 937 | ] 938 | }, 939 | { 940 | "cell_type": "markdown", 941 | "id": "387a868a", 942 | "metadata": {}, 943 | "source": [ 944 | "## Matrix\n", 945 | "A matrix is a way to describe multi-dimensional lists. It's a list with another list inside of it. We have a list with sub lists. These topics come up a lot in machine learning and image processing. Matrices allow us to do some heavy calculations under the hood." 946 | ] 947 | }, 948 | { 949 | "cell_type": "code", 950 | "execution_count": 26, 951 | "id": "6819a831", 952 | "metadata": {}, 953 | "outputs": [ 954 | { 955 | "name": "stdout", 956 | "output_type": "stream", 957 | "text": [ 958 | "2\n", 959 | "8\n", 960 | "6\n" 961 | ] 962 | } 963 | ], 964 | "source": [ 965 | "matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", 966 | "print(matrix[0][1]) # print second item in the first list\n", 967 | "print(matrix[2][1]) # print the second item in the last list\n", 968 | "print(matrix[1][2]) # print the last item in the second list" 969 | ] 970 | }, 971 | { 972 | "cell_type": "markdown", 973 | "id": "dd2c2cd0", 974 | "metadata": {}, 975 | "source": [ 976 | "## List Methods\n", 977 | "We can perform a number of actions on lists with list methods. See the [W3Schools Resource](https://www.w3schools.com/python/python_ref_list.asp) for more Python list methods.\n", 978 | "\n", 979 | "Try out the examples below. And remember, if you're using [replit.com](https://replit.com/), as soon as you write the variable plus the dot, the conding environment will tell you which methods you can use." 980 | ] 981 | }, 982 | { 983 | "cell_type": "code", 984 | "execution_count": 58, 985 | "id": "b95debee", 986 | "metadata": { 987 | "scrolled": true 988 | }, 989 | "outputs": [ 990 | { 991 | "name": "stdout", 992 | "output_type": "stream", 993 | "text": [ 994 | "5\n" 995 | ] 996 | } 997 | ], 998 | "source": [ 999 | "basket = [1, 2, 3, 4, 5]\n", 1000 | "# Remember, the length starts counting from one, not zero\n", 1001 | "print(len(basket)) # calculate the length of the basket" 1002 | ] 1003 | }, 1004 | { 1005 | "cell_type": "markdown", 1006 | "id": "272101a4", 1007 | "metadata": {}, 1008 | "source": [ 1009 | "The `.append`, `.insert`, and `.extends` methods modify an existing list in place (in memory). They don't create a copy of the list that they modify.\n", 1010 | "\n", 1011 | "The `.extends` method takes an item we can iterate over, like a list, and modifies it." 1012 | ] 1013 | }, 1014 | { 1015 | "cell_type": "code", 1016 | "execution_count": 36, 1017 | "id": "f19a7cae", 1018 | "metadata": {}, 1019 | "outputs": [ 1020 | { 1021 | "name": "stdout", 1022 | "output_type": "stream", 1023 | "text": [ 1024 | "[1, 2, 3, 4, 5]\n", 1025 | "[1, 2, 3, 4, 5, 100]\n", 1026 | "[1, 2, 3, 4, 5, 100]\n" 1027 | ] 1028 | } 1029 | ], 1030 | "source": [ 1031 | "# adding items to a list\n", 1032 | "print(basket)\n", 1033 | "basket.append(100)\n", 1034 | "new_list = basket\n", 1035 | "print(basket)\n", 1036 | "print(new_list)" 1037 | ] 1038 | }, 1039 | { 1040 | "cell_type": "code", 1041 | "execution_count": 37, 1042 | "id": "44beeacf", 1043 | "metadata": {}, 1044 | "outputs": [ 1045 | { 1046 | "name": "stdout", 1047 | "output_type": "stream", 1048 | "text": [ 1049 | "[1, 2, 3, 4, 50, 5, 100]\n" 1050 | ] 1051 | } 1052 | ], 1053 | "source": [ 1054 | "# insert an item into a list\n", 1055 | "basket.insert(4, 50)\n", 1056 | "print(basket)" 1057 | ] 1058 | }, 1059 | { 1060 | "cell_type": "code", 1061 | "execution_count": 40, 1062 | "id": "d5eb071d", 1063 | "metadata": {}, 1064 | "outputs": [ 1065 | { 1066 | "name": "stdout", 1067 | "output_type": "stream", 1068 | "text": [ 1069 | "[1, 2, 3, 4, 50, 5, 100, 101, 101]\n" 1070 | ] 1071 | } 1072 | ], 1073 | "source": [ 1074 | "# extending our list\n", 1075 | "# 101 is added twice because this cell was printed more than once\n", 1076 | "new_list = basket.extend([101])\n", 1077 | "print(basket)" 1078 | ] 1079 | }, 1080 | { 1081 | "cell_type": "code", 1082 | "execution_count": 41, 1083 | "id": "804e798a", 1084 | "metadata": {}, 1085 | "outputs": [ 1086 | { 1087 | "name": "stdout", 1088 | "output_type": "stream", 1089 | "text": [ 1090 | "[1, 2, 3, 4, 50, 5, 100, 101]\n" 1091 | ] 1092 | } 1093 | ], 1094 | "source": [ 1095 | "# removing items from our list\n", 1096 | "basket.pop() # removes last item from the list\n", 1097 | "print(basket)" 1098 | ] 1099 | }, 1100 | { 1101 | "cell_type": "markdown", 1102 | "id": "e1559fea", 1103 | "metadata": {}, 1104 | "source": [ 1105 | "With `.pop`, we give it the index and with `.remove`, we give it the value. `.pop` also returns the value that was removed. The `.clear` method removes everything from the list." 1106 | ] 1107 | }, 1108 | { 1109 | "cell_type": "code", 1110 | "execution_count": 42, 1111 | "id": "5dc77777", 1112 | "metadata": {}, 1113 | "outputs": [], 1114 | "source": [ 1115 | "basket.pop()\n", 1116 | "basket.pop(0) # remove item at index 0\n", 1117 | "basket.remove(4) # remove 4 from the list" 1118 | ] 1119 | }, 1120 | { 1121 | "cell_type": "code", 1122 | "execution_count": 43, 1123 | "id": "564ce88d", 1124 | "metadata": {}, 1125 | "outputs": [ 1126 | { 1127 | "name": "stdout", 1128 | "output_type": "stream", 1129 | "text": [ 1130 | "[2, 3, 50, 5, 100]\n" 1131 | ] 1132 | } 1133 | ], 1134 | "source": [ 1135 | "print(basket)" 1136 | ] 1137 | }, 1138 | { 1139 | "cell_type": "code", 1140 | "execution_count": 44, 1141 | "id": "cfecff2c", 1142 | "metadata": {}, 1143 | "outputs": [ 1144 | { 1145 | "name": "stdout", 1146 | "output_type": "stream", 1147 | "text": [ 1148 | "[]\n" 1149 | ] 1150 | } 1151 | ], 1152 | "source": [ 1153 | "# remove all items from the list\n", 1154 | "basket.clear()\n", 1155 | "print(basket)" 1156 | ] 1157 | }, 1158 | { 1159 | "cell_type": "markdown", 1160 | "id": "c5268a48", 1161 | "metadata": {}, 1162 | "source": [ 1163 | "## List Methods 2\n", 1164 | "\n", 1165 | "`.index` is a list method we can use. We give it a value, a start, and a stop.\n", 1166 | "\n", 1167 | "A Python keyword is a word that means something to the language that we cannot use to name a variable. Examples are `True` (a boolean) or `in`. We can use `in` to see if something is in a list.\n", 1168 | "\n", 1169 | "We can use `.count` to count how many times an item occurs." 1170 | ] 1171 | }, 1172 | { 1173 | "cell_type": "code", 1174 | "execution_count": 3, 1175 | "id": "95b42394", 1176 | "metadata": {}, 1177 | "outputs": [ 1178 | { 1179 | "name": "stdout", 1180 | "output_type": "stream", 1181 | "text": [ 1182 | "3\n", 1183 | "3\n" 1184 | ] 1185 | } 1186 | ], 1187 | "source": [ 1188 | "basket = ['a', 'b', 'c', 'd', 'e']\n", 1189 | "print(basket.index('d'))\n", 1190 | "print(basket.index('d', 0, 4)) # item to search for, index to start search, index to stop search" 1191 | ] 1192 | }, 1193 | { 1194 | "cell_type": "code", 1195 | "execution_count": 4, 1196 | "id": "4fcb6cd0", 1197 | "metadata": {}, 1198 | "outputs": [ 1199 | { 1200 | "name": "stdout", 1201 | "output_type": "stream", 1202 | "text": [ 1203 | "True\n", 1204 | "False\n", 1205 | "True\n", 1206 | "1\n" 1207 | ] 1208 | } 1209 | ], 1210 | "source": [ 1211 | "print('d' in basket)\n", 1212 | "print('x' in basket)\n", 1213 | "print('i' in 'hi my name is Ian')\n", 1214 | "print(basket.count('d'))" 1215 | ] 1216 | }, 1217 | { 1218 | "cell_type": "markdown", 1219 | "id": "2c14dea1", 1220 | "metadata": {}, 1221 | "source": [ 1222 | "## List Methods 3\n", 1223 | "A continuation of list methods, in which we'll look at sorting and reversing the order of the items in a list." 1224 | ] 1225 | }, 1226 | { 1227 | "cell_type": "code", 1228 | "execution_count": 5, 1229 | "id": "05a8d266", 1230 | "metadata": {}, 1231 | "outputs": [ 1232 | { 1233 | "name": "stdout", 1234 | "output_type": "stream", 1235 | "text": [ 1236 | "['a', 'b', 'x', 'c', 'd', 'e', 'd']\n" 1237 | ] 1238 | } 1239 | ], 1240 | "source": [ 1241 | "basket.append('d')\n", 1242 | "basket.insert(2, 'x')\n", 1243 | "print(basket)" 1244 | ] 1245 | }, 1246 | { 1247 | "cell_type": "code", 1248 | "execution_count": 6, 1249 | "id": "806f9334", 1250 | "metadata": {}, 1251 | "outputs": [ 1252 | { 1253 | "name": "stdout", 1254 | "output_type": "stream", 1255 | "text": [ 1256 | "['a', 'b', 'c', 'd', 'd', 'e', 'x']\n" 1257 | ] 1258 | } 1259 | ], 1260 | "source": [ 1261 | "basket.sort()\n", 1262 | "print(basket)" 1263 | ] 1264 | }, 1265 | { 1266 | "cell_type": "markdown", 1267 | "id": "ecf8612f", 1268 | "metadata": {}, 1269 | "source": [ 1270 | "We can also use `sorted(basket)` if we want to produce a new list without modifying the existing one. It creates a new copy of the list as with list slicing.\n", 1271 | "\n", 1272 | "If we want to do the opposite of `.sort()`, we can use the `.reverse()` method. It's a good idea to use `.sort()` first, however, otherwise the `.reverse()` method may not work as expected." 1273 | ] 1274 | }, 1275 | { 1276 | "cell_type": "code", 1277 | "execution_count": 7, 1278 | "id": "fcb09c53", 1279 | "metadata": {}, 1280 | "outputs": [ 1281 | { 1282 | "name": "stdout", 1283 | "output_type": "stream", 1284 | "text": [ 1285 | "['x', 'e', 'd', 'd', 'c', 'b', 'a']\n" 1286 | ] 1287 | } 1288 | ], 1289 | "source": [ 1290 | "basket.sort()\n", 1291 | "basket.reverse()\n", 1292 | "print(basket)" 1293 | ] 1294 | }, 1295 | { 1296 | "cell_type": "markdown", 1297 | "id": "1d8b24bf", 1298 | "metadata": {}, 1299 | "source": [ 1300 | "## Common List Patterns\n", 1301 | "\n", 1302 | "We'll now learn some useful tricks with lists. One that we've seen is `len()`, to figure out the length of a string or a list.\n", 1303 | "\n", 1304 | "Another is how to reverse a list, which we can do with the `.reverse()` method or with list slicing." 1305 | ] 1306 | }, 1307 | { 1308 | "cell_type": "code", 1309 | "execution_count": 10, 1310 | "id": "1b8f9af7", 1311 | "metadata": {}, 1312 | "outputs": [ 1313 | { 1314 | "name": "stdout", 1315 | "output_type": "stream", 1316 | "text": [ 1317 | "['x', 'e', 'd', 'd', 'c', 'b', 'a']\n", 1318 | "['a', 'b', 'c', 'd', 'd', 'e', 'x']\n" 1319 | ] 1320 | } 1321 | ], 1322 | "source": [ 1323 | "print(basket)\n", 1324 | "basket.sort() # sort the list\n", 1325 | "basket.reverse() # reverse the sorted list\n", 1326 | "print(basket[::-1]) # reverse the last order of the sorted list with list slicing - creates new list" 1327 | ] 1328 | }, 1329 | { 1330 | "cell_type": "markdown", 1331 | "id": "cd2e95b0", 1332 | "metadata": {}, 1333 | "source": [ 1334 | "If we want to generate a numbered list quickly, we can use range." 1335 | ] 1336 | }, 1337 | { 1338 | "cell_type": "code", 1339 | "execution_count": 13, 1340 | "id": "e0765348", 1341 | "metadata": {}, 1342 | "outputs": [ 1343 | { 1344 | "name": "stdout", 1345 | "output_type": "stream", 1346 | "text": [ 1347 | "range(1, 10)\n", 1348 | "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" 1349 | ] 1350 | } 1351 | ], 1352 | "source": [ 1353 | "print(range(1, 10))\n", 1354 | "print(list(range(1, 10))) # start, stop (not inclusive)" 1355 | ] 1356 | }, 1357 | { 1358 | "cell_type": "markdown", 1359 | "id": "ec79474f", 1360 | "metadata": {}, 1361 | "source": [ 1362 | "`.join()` takes an iterable data structure like a list and joins it together." 1363 | ] 1364 | }, 1365 | { 1366 | "cell_type": "code", 1367 | "execution_count": 17, 1368 | "id": "0d091ba3", 1369 | "metadata": {}, 1370 | "outputs": [ 1371 | { 1372 | "name": "stdout", 1373 | "output_type": "stream", 1374 | "text": [ 1375 | "hi my name is Andrei\n" 1376 | ] 1377 | } 1378 | ], 1379 | "source": [ 1380 | "sentence = \" \"\n", 1381 | "new_sentence = sentence.join([\"hi\", \"my\", \"name\", \"is\", \"Andrei\"])\n", 1382 | "print(new_sentence)" 1383 | ] 1384 | }, 1385 | { 1386 | "cell_type": "markdown", 1387 | "id": "9ae59879", 1388 | "metadata": {}, 1389 | "source": [ 1390 | "## List Unpacking\n", 1391 | "In Python, we can use list unpacking to assign elements of a list to multiple variables. Try out the examples below in your Python environment." 1392 | ] 1393 | }, 1394 | { 1395 | "cell_type": "code", 1396 | "execution_count": 21, 1397 | "id": "08399771", 1398 | "metadata": {}, 1399 | "outputs": [ 1400 | { 1401 | "name": "stdout", 1402 | "output_type": "stream", 1403 | "text": [ 1404 | "1\n", 1405 | "2\n", 1406 | "3\n", 1407 | "[4, 5, 6, 7, 8]\n", 1408 | "9\n" 1409 | ] 1410 | } 1411 | ], 1412 | "source": [ 1413 | "a,b,c, *rest, d = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", 1414 | "print(a)\n", 1415 | "print(b)\n", 1416 | "print(c)\n", 1417 | "print(rest)\n", 1418 | "print(d)" 1419 | ] 1420 | }, 1421 | { 1422 | "cell_type": "markdown", 1423 | "id": "3c0abf2b", 1424 | "metadata": {}, 1425 | "source": [ 1426 | "We can also access elements using a for-loop." 1427 | ] 1428 | }, 1429 | { 1430 | "cell_type": "code", 1431 | "execution_count": 12, 1432 | "id": "69e16577", 1433 | "metadata": {}, 1434 | "outputs": [ 1435 | { 1436 | "name": "stdout", 1437 | "output_type": "stream", 1438 | "text": [ 1439 | "Using indexing:\n", 1440 | "1\n", 1441 | "2\n", 1442 | "3\n", 1443 | "4\n", 1444 | "5\n", 1445 | "6\n", 1446 | "7\n", 1447 | "Printing the first half of the list:\n", 1448 | "1\n", 1449 | "2\n", 1450 | "3\n", 1451 | "Using iterator:\n", 1452 | "1\n", 1453 | "2\n", 1454 | "3\n", 1455 | "4\n", 1456 | "5\n", 1457 | "6\n", 1458 | "7\n" 1459 | ] 1460 | } 1461 | ], 1462 | "source": [ 1463 | "list = [1,2,3,4,5,6,7]\n", 1464 | "\n", 1465 | "# There are different way to approach a for-loop\n", 1466 | "# We will use the simple indexing way first!\n", 1467 | "\n", 1468 | "# this is for 0 (inclusive) up to the length of the list (exclusive)\n", 1469 | "print(\"Using indexing:\")\n", 1470 | "for i in range(0,len(list)):\n", 1471 | " print(list[i])\n", 1472 | "\n", 1473 | "# Suppose we want the first half of the list, we would change the parameters in range()\n", 1474 | "print(\"Printing the first half of the list:\")\n", 1475 | "for i in range(0, len(list)//2): # remember we can only use int for indexing, therefore we need double dividing to get an int!\n", 1476 | " print(list[i])\n", 1477 | "\n", 1478 | "# We can also use the iterator!\n", 1479 | "print(\"Using iterator:\")\n", 1480 | "for number in list:\n", 1481 | " print(number)" 1482 | ] 1483 | }, 1484 | { 1485 | "cell_type": "markdown", 1486 | "id": "6db20452", 1487 | "metadata": {}, 1488 | "source": [ 1489 | "## `None`\n", 1490 | "\n", 1491 | "With `None`, we saw that some methods worked on lists but didn't output anything. `None` is a special data type that exists in Python. Most programming languages have a data type like this to represent an absence of value. We'll see this being used a lot more as we get deeper into the course.\n", 1492 | "\n", 1493 | "In a video game for example, when a user is first created, it won't have any weapons at the beginning of the game, so we can use `None` to represent this." 1494 | ] 1495 | }, 1496 | { 1497 | "cell_type": "code", 1498 | "execution_count": 22, 1499 | "id": "58e5d1e6", 1500 | "metadata": {}, 1501 | "outputs": [ 1502 | { 1503 | "name": "stdout", 1504 | "output_type": "stream", 1505 | "text": [ 1506 | "None\n" 1507 | ] 1508 | } 1509 | ], 1510 | "source": [ 1511 | "weapons = None\n", 1512 | "print(weapons)" 1513 | ] 1514 | }, 1515 | { 1516 | "cell_type": "markdown", 1517 | "id": "ed0762ac", 1518 | "metadata": {}, 1519 | "source": [ 1520 | "## Dictionaries\n", 1521 | "In other languages, this data structure may be called an object or a hash map. Lists were the first data structure we learned in Python. With lists, they are easily ordered, and we can access them through indexes.\n", 1522 | "\n", 1523 | "Dictionaries are unordered (unlike lists) and have key-value pairs. A key is a string that allows us to access a value. We access a value by giving the dictionary a key. Python will then retrieve the value from memory based on the key we provide.\n", 1524 | "\n", 1525 | "Unlike lists, we cannot access anything in a dictionary through an index because they are unordered -- we don't know where in memory the keys and values are stored.\n", 1526 | "\n", 1527 | "In the example below, the dictionary is printed in order, but as a dictionary grows, the order is likely to change." 1528 | ] 1529 | }, 1530 | { 1531 | "cell_type": "code", 1532 | "execution_count": 6, 1533 | "id": "a9dba2b4", 1534 | "metadata": {}, 1535 | "outputs": [ 1536 | { 1537 | "name": "stdout", 1538 | "output_type": "stream", 1539 | "text": [ 1540 | "hello\n", 1541 | "{'a': [1, 2, 3], 'b': 'hello', 'x': True}\n", 1542 | "2\n" 1543 | ] 1544 | } 1545 | ], 1546 | "source": [ 1547 | "dictionary = {\n", 1548 | " 'a': [1,2,3],\n", 1549 | " 'b': 'hello',\n", 1550 | " 'x': True\n", 1551 | "}\n", 1552 | "print(dictionary['b']) # hello\n", 1553 | "print(dictionary) # {'a': [1, 2, 3], 'b': 'hello', 'x': True}\n", 1554 | "print(dictionary['a'][1]) # 2" 1555 | ] 1556 | }, 1557 | { 1558 | "cell_type": "markdown", 1559 | "id": "de5a15c4", 1560 | "metadata": {}, 1561 | "source": [ 1562 | "We can also store dictionary items within a list." 1563 | ] 1564 | }, 1565 | { 1566 | "cell_type": "code", 1567 | "execution_count": 5, 1568 | "id": "2d7e9d69", 1569 | "metadata": {}, 1570 | "outputs": [ 1571 | { 1572 | "name": "stdout", 1573 | "output_type": "stream", 1574 | "text": [ 1575 | "3\n" 1576 | ] 1577 | } 1578 | ], 1579 | "source": [ 1580 | "my_list = [\n", 1581 | "{\n", 1582 | " 'a': [1,2,3],\n", 1583 | " 'b': 'hello',\n", 1584 | " 'x': True\n", 1585 | "},\n", 1586 | "{\n", 1587 | " 'a': [4,5,6],\n", 1588 | " 'b': 'hello',\n", 1589 | " 'x': True\n", 1590 | "}\n", 1591 | "]\n", 1592 | "print(my_list[0]['a'][2])" 1593 | ] 1594 | }, 1595 | { 1596 | "cell_type": "markdown", 1597 | "id": "34b1beac", 1598 | "metadata": {}, 1599 | "source": [ 1600 | "## DEVELOPER FUNDAMENTALS III\n", 1601 | "Our next fundamental is understanding data structures. A good programmer knows when to use which data structure because there are different trade-offs. This takes practice and experience, so throughout the course, we'll explore the pros and cons.\n", 1602 | "\n", 1603 | "A benefit to a list is that they are ordered whereas dictionaries are not, but dictionaries can hold more information through key-value pairs. " 1604 | ] 1605 | }, 1606 | { 1607 | "cell_type": "markdown", 1608 | "id": "c93a6ed0", 1609 | "metadata": {}, 1610 | "source": [ 1611 | "## Dictionary Keys\n", 1612 | "Dictionary keys need to have a special property. The keys need to be immutable, meaning they cannot change. A dictionary key, for example, cannot be a list.\n", 1613 | "\n", 1614 | "A key must also be unique; otherwise it might lose its value. If we use the same key, a value will be overwritten.\n", 1615 | "\n", 1616 | "Typically, a dictionary key is something descriptive, like a string." 1617 | ] 1618 | }, 1619 | { 1620 | "cell_type": "code", 1621 | "execution_count": 26, 1622 | "id": "21319ef5", 1623 | "metadata": {}, 1624 | "outputs": [ 1625 | { 1626 | "name": "stdout", 1627 | "output_type": "stream", 1628 | "text": [ 1629 | "{123: [1, 2, 3], True: 'hello', '100': True}\n" 1630 | ] 1631 | } 1632 | ], 1633 | "source": [ 1634 | "dictionary1 = {\n", 1635 | " 123: [1,2,3],\n", 1636 | " True: 'hello',\n", 1637 | " '100': True\n", 1638 | "}\n", 1639 | "print(dictionary1) # {123: [1, 2, 3], True: 'hello', '100': True}" 1640 | ] 1641 | }, 1642 | { 1643 | "cell_type": "markdown", 1644 | "id": "76458850", 1645 | "metadata": {}, 1646 | "source": [ 1647 | "## Dictionary Methods\n", 1648 | "\n", 1649 | "A way to access a key or to see if it exists is to use the `.get()` method." 1650 | ] 1651 | }, 1652 | { 1653 | "cell_type": "code", 1654 | "execution_count": 43, 1655 | "id": "0403365e", 1656 | "metadata": {}, 1657 | "outputs": [ 1658 | { 1659 | "name": "stdout", 1660 | "output_type": "stream", 1661 | "text": [ 1662 | "None\n", 1663 | "55\n", 1664 | "{'basket': [1, 2, 3], 'greet': 'hello'}\n" 1665 | ] 1666 | } 1667 | ], 1668 | "source": [ 1669 | "user = {\n", 1670 | " 'basket': [1,2,3],\n", 1671 | " 'greet': 'hello'\n", 1672 | "}\n", 1673 | "print(user.get('age')) # None\n", 1674 | "print(user.get('age', 55)) # Add a default value of age 55 to the user\n", 1675 | "print(user) # {'basket': [1, 2, 3], 'greet': 'hello'}" 1676 | ] 1677 | }, 1678 | { 1679 | "cell_type": "markdown", 1680 | "id": "a96a7ca0", 1681 | "metadata": {}, 1682 | "source": [ 1683 | "We can add a key-value pair to a dictionary using the syntax below." 1684 | ] 1685 | }, 1686 | { 1687 | "cell_type": "code", 1688 | "execution_count": 44, 1689 | "id": "93879183", 1690 | "metadata": {}, 1691 | "outputs": [ 1692 | { 1693 | "name": "stdout", 1694 | "output_type": "stream", 1695 | "text": [ 1696 | "{'name': 'John'}\n" 1697 | ] 1698 | } 1699 | ], 1700 | "source": [ 1701 | "user2 = dict(name='John')\n", 1702 | "print(user2) # {'name': 'John'}" 1703 | ] 1704 | }, 1705 | { 1706 | "cell_type": "markdown", 1707 | "id": "cb8e4e4a", 1708 | "metadata": {}, 1709 | "source": [ 1710 | "## Dictionary Methods 2\n", 1711 | "We can use the in keyword with dictionaries just like we can with lists. \n", 1712 | "\n", 1713 | "We will go over the main methods dictionaries have, but there aren't too many.\n", 1714 | "\n", 1715 | " - `.keys()` - Returns the keys in the dictionary\n", 1716 | " - `.values()` - Returns the values in the dictionary\n", 1717 | " - `.clear()` - Remove all key-value pairs from the dictionary\n", 1718 | " - `.copy()` - Returns a copy of the dictionary\n", 1719 | " - `.pop(key)` - Returns the value corresponding to the given `key` and removes the key-value pair from the dictionary\n", 1720 | " - `.popitem()` - Returns an arbitrary `(key, value)` pair from the dictionary and removes it\n", 1721 | " - `.update(other_dict)` - Updates a key with a new value. It will add a new key item if the key doesn't exist" 1722 | ] 1723 | }, 1724 | { 1725 | "cell_type": "code", 1726 | "execution_count": 45, 1727 | "id": "e05735b6", 1728 | "metadata": { 1729 | "scrolled": true 1730 | }, 1731 | "outputs": [ 1732 | { 1733 | "name": "stdout", 1734 | "output_type": "stream", 1735 | "text": [ 1736 | "True\n", 1737 | "False\n", 1738 | "False\n", 1739 | "True\n", 1740 | "dict_items([('basket', [1, 2, 3]), ('greet', 'hello')])\n", 1741 | "None\n", 1742 | "{'basket': [1, 2, 3], 'greet': 'hello'}\n" 1743 | ] 1744 | } 1745 | ], 1746 | "source": [ 1747 | "print('basket' in user) # True\n", 1748 | "print('size' in user) # False\n", 1749 | "print('size' in user.keys()) # False\n", 1750 | "print('hello' in user.values()) # True\n", 1751 | "print(user.items()) # Includes tuples, which we'll discuss next\n", 1752 | "user2 = user.copy()\n", 1753 | "print(user.clear()) # None\n", 1754 | "print(user2) # {'basket': [1, 2, 3], 'greet': 'hello'}" 1755 | ] 1756 | }, 1757 | { 1758 | "cell_type": "code", 1759 | "execution_count": 46, 1760 | "id": "23928db0", 1761 | "metadata": { 1762 | "scrolled": true 1763 | }, 1764 | "outputs": [ 1765 | { 1766 | "name": "stdout", 1767 | "output_type": "stream", 1768 | "text": [ 1769 | "hello\n", 1770 | "None\n", 1771 | "{'age': 55}\n" 1772 | ] 1773 | } 1774 | ], 1775 | "source": [ 1776 | "print(user2.pop('greet')) # hello\n", 1777 | "print(user.update({'age': 55})) # None\n", 1778 | "print(user) # {'age': 55}" 1779 | ] 1780 | }, 1781 | { 1782 | "cell_type": "markdown", 1783 | "id": "f6b9e2f6", 1784 | "metadata": {}, 1785 | "source": [ 1786 | "## Tuples\n", 1787 | "A tuple is similar to a list, but unlike lists, we cannot modify them. We cannot add to them, reverse them, or sort them. Like a list, we can access an item in a tuple with an index and see if an item is in a tuple. If we want to create a list that we know will not be modified, a tuple is desirable because they are more performant than lists.\n", 1788 | "\n", 1789 | "We use parentheses to denote a tuple. A good example of a tuple would be a specific location with latitude and longitude that will not change. We can also use tuples as dictionary keys since they are immutable." 1790 | ] 1791 | }, 1792 | { 1793 | "cell_type": "code", 1794 | "execution_count": 48, 1795 | "id": "43c05daa", 1796 | "metadata": {}, 1797 | "outputs": [ 1798 | { 1799 | "name": "stdout", 1800 | "output_type": "stream", 1801 | "text": [ 1802 | "True\n", 1803 | "dict_items([('age', 55)])\n" 1804 | ] 1805 | } 1806 | ], 1807 | "source": [ 1808 | "my_tuple = (1, 2, 3, 4, 5)\n", 1809 | "print(5 in my_tuple) # True\n", 1810 | "print(user.items()) # returns a tuple" 1811 | ] 1812 | }, 1813 | { 1814 | "cell_type": "markdown", 1815 | "id": "d4517840", 1816 | "metadata": {}, 1817 | "source": [ 1818 | "## Tuples 2\n", 1819 | "\n", 1820 | "We can slice a tuple just like we can slice a list. Methods we can use on tuples include `.count()` and `.index()`.\n", 1821 | "\n", 1822 | "- `.count(value)` - Returns how many times `value` appears in the tuple\n", 1823 | "- `.index(value)` - Returns the index of the first appearance of `value` in the tuple" 1824 | ] 1825 | }, 1826 | { 1827 | "cell_type": "code", 1828 | "execution_count": 53, 1829 | "id": "dac496df", 1830 | "metadata": { 1831 | "scrolled": true 1832 | }, 1833 | "outputs": [ 1834 | { 1835 | "name": "stdout", 1836 | "output_type": "stream", 1837 | "text": [ 1838 | "(2,)\n", 1839 | "1\n", 1840 | "[4, 5]\n", 1841 | "1\n", 1842 | "4\n", 1843 | "5\n" 1844 | ] 1845 | } 1846 | ], 1847 | "source": [ 1848 | "new_tuple = my_tuple[1:2]\n", 1849 | "x, y, z, *other = my_tuple\n", 1850 | "print(new_tuple) # (2,)\n", 1851 | "print(x) # 1\n", 1852 | "print(other) # [4, 5]\n", 1853 | "print(my_tuple.count(5)) # 1\n", 1854 | "print(my_tuple.index(5)) # 4\n", 1855 | "print(len(my_tuple)) # 5" 1856 | ] 1857 | }, 1858 | { 1859 | "cell_type": "markdown", 1860 | "id": "6d066fa5", 1861 | "metadata": {}, 1862 | "source": [ 1863 | "## Sets\n", 1864 | "If you're struggling with the material, it's okay. Hang in there! We are learning the concepts that will build our foundation for building projects.\n", 1865 | "\n", 1866 | "Sets are collections of unique objects. They look similar to a dictionary, but they do not have key-value pairs. Unlike lists, they are unordered and all values must be unique. There are no duplicates in set. \n", 1867 | "\n", 1868 | "Because sets are not ordered, they do not support indexing. To see if something exists in a set, we can use the in operator.\n", 1869 | "\n", 1870 | "We can add an item to a set with the `.add(value)` method. We can create a copy of a set with the `.copy()` method." 1871 | ] 1872 | }, 1873 | { 1874 | "cell_type": "code", 1875 | "execution_count": 58, 1876 | "id": "eea1b538", 1877 | "metadata": {}, 1878 | "outputs": [ 1879 | { 1880 | "name": "stdout", 1881 | "output_type": "stream", 1882 | "text": [ 1883 | "{1, 2, 3, 4, 5, 100}\n", 1884 | "True\n", 1885 | "6\n" 1886 | ] 1887 | } 1888 | ], 1889 | "source": [ 1890 | "my_set = {1, 2, 3, 4, 5}\n", 1891 | "my_set.add(100) # adds 100 to the set\n", 1892 | "my_set.add(2) # doesn't get added\n", 1893 | "print(my_set)\n", 1894 | "print(1 in my_set) # True\n", 1895 | "print(len(my_set)) # 6" 1896 | ] 1897 | }, 1898 | { 1899 | "cell_type": "markdown", 1900 | "id": "09d67be0", 1901 | "metadata": {}, 1902 | "source": [ 1903 | "Create a set from a list with no duplicates." 1904 | ] 1905 | }, 1906 | { 1907 | "cell_type": "code", 1908 | "execution_count": 55, 1909 | "id": "bff90f51", 1910 | "metadata": {}, 1911 | "outputs": [ 1912 | { 1913 | "name": "stdout", 1914 | "output_type": "stream", 1915 | "text": [ 1916 | "{1, 2, 3, 4, 5}\n" 1917 | ] 1918 | } 1919 | ], 1920 | "source": [ 1921 | "my_list = [1, 2, 3, 4, 5, 5]\n", 1922 | "print(set(my_list))" 1923 | ] 1924 | }, 1925 | { 1926 | "cell_type": "markdown", 1927 | "id": "2aa5cdfd", 1928 | "metadata": {}, 1929 | "source": [ 1930 | "## Sets 2\n", 1931 | "\n", 1932 | "There are several methods we can use on sets. Code along and try these out in your python environment.\n", 1933 | "\n", 1934 | "- `.difference(other_set)` - Find the difference between two sets\n", 1935 | "- `.discard(element)` - Removes an element from the set\n", 1936 | "- `.difference_update(other_set)` - Updates the set so that the differences from another set are removed.\n", 1937 | "- `.intersection(other_set)` - Finds the common elements between two sets\n", 1938 | "- `.isdisjoint(other_set)` - Returns a boolean to test if the two sets have any elements in common. If so, `True` will be returned. Otherwise, `False` will be returned\n", 1939 | "- `.issubset(other_set)` - Checks to see if a set is contained within another set and returns a boolean\n", 1940 | "- `.issuperset(other_set)` - Check to see if a set is a superset of another set. It is similar to the `.issubset` method but asks the opposite question\n", 1941 | "- `.union(other_set)` - Joins two sets together and removes duplicates. Returns a new set. Can also use the syntax `my_set | your_set`" 1942 | ] 1943 | }, 1944 | { 1945 | "cell_type": "code", 1946 | "execution_count": 74, 1947 | "id": "6ee1ec83", 1948 | "metadata": {}, 1949 | "outputs": [ 1950 | { 1951 | "name": "stdout", 1952 | "output_type": "stream", 1953 | "text": [ 1954 | "{1, 2, 3}\n", 1955 | "{6, 7, 8, 9, 10}\n", 1956 | "{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\n" 1957 | ] 1958 | } 1959 | ], 1960 | "source": [ 1961 | "my_set = {1, 2, 3, 4, 5}\n", 1962 | "your_set = {4, 5, 6, 7, 8, 9, 10}\n", 1963 | "print(my_set.difference(your_set))\n", 1964 | "print(your_set.difference(my_set))\n", 1965 | "print(my_set.union(your_set))" 1966 | ] 1967 | }, 1968 | { 1969 | "cell_type": "code", 1970 | "execution_count": 63, 1971 | "id": "44e3d49f", 1972 | "metadata": {}, 1973 | "outputs": [ 1974 | { 1975 | "name": "stdout", 1976 | "output_type": "stream", 1977 | "text": [ 1978 | "{1, 2, 3, 4}\n" 1979 | ] 1980 | } 1981 | ], 1982 | "source": [ 1983 | "my_set.discard(5) # removes 4\n", 1984 | "print(my_set)" 1985 | ] 1986 | }, 1987 | { 1988 | "cell_type": "code", 1989 | "execution_count": 64, 1990 | "id": "6f820716", 1991 | "metadata": {}, 1992 | "outputs": [ 1993 | { 1994 | "name": "stdout", 1995 | "output_type": "stream", 1996 | "text": [ 1997 | "{1, 2, 3}\n" 1998 | ] 1999 | } 2000 | ], 2001 | "source": [ 2002 | "my_set.difference_update(your_set)\n", 2003 | "print(my_set)" 2004 | ] 2005 | }, 2006 | { 2007 | "cell_type": "code", 2008 | "execution_count": 66, 2009 | "id": "ea33acd9", 2010 | "metadata": {}, 2011 | "outputs": [ 2012 | { 2013 | "name": "stdout", 2014 | "output_type": "stream", 2015 | "text": [ 2016 | "{4, 5}\n" 2017 | ] 2018 | } 2019 | ], 2020 | "source": [ 2021 | "print(my_set.intersection(your_set)) # The original sets have two elements in common: {4, 5}" 2022 | ] 2023 | }, 2024 | { 2025 | "cell_type": "code", 2026 | "execution_count": 67, 2027 | "id": "d71840f5", 2028 | "metadata": {}, 2029 | "outputs": [ 2030 | { 2031 | "name": "stdout", 2032 | "output_type": "stream", 2033 | "text": [ 2034 | "False\n" 2035 | ] 2036 | } 2037 | ], 2038 | "source": [ 2039 | "print(my_set.isdisjoint(your_set)) # Returns False because the two sets share values." 2040 | ] 2041 | }, 2042 | { 2043 | "cell_type": "code", 2044 | "execution_count": 72, 2045 | "id": "439fe177", 2046 | "metadata": {}, 2047 | "outputs": [ 2048 | { 2049 | "name": "stdout", 2050 | "output_type": "stream", 2051 | "text": [ 2052 | "True\n", 2053 | "True\n", 2054 | "False\n" 2055 | ] 2056 | } 2057 | ], 2058 | "source": [ 2059 | "my_set = {4, 5}\n", 2060 | "your_set = {4, 5, 6, 7, 8, 9}\n", 2061 | "print(my_set.issubset(your_set)) # True - the entirety of my_set is in your_set \n", 2062 | "print(your_set.issuperset(my_set)) # True - the entirety of my_set is within your_set, so your_set is the superset of my_set\n", 2063 | "print(my_set.issuperset(your_set)) # False" 2064 | ] 2065 | } 2066 | ], 2067 | "metadata": { 2068 | "kernelspec": { 2069 | "display_name": "Python 3 (ipykernel)", 2070 | "language": "python", 2071 | "name": "python3" 2072 | }, 2073 | "language_info": { 2074 | "codemirror_mode": { 2075 | "name": "ipython", 2076 | "version": 3 2077 | }, 2078 | "file_extension": ".py", 2079 | "mimetype": "text/x-python", 2080 | "name": "python", 2081 | "nbconvert_exporter": "python", 2082 | "pygments_lexer": "ipython3", 2083 | "version": "3.7.2" 2084 | } 2085 | }, 2086 | "nbformat": 4, 2087 | "nbformat_minor": 5 2088 | } 2089 | --------------------------------------------------------------------------------