{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Lesson3-Python For Data Science-Basics.ipynb", "version": "0.3.2", "provenance": [], "collapsed_sections": [ "qqAIvhP5N4iu", "yHdWIarsN850", "I2e-GRTObNek", "p8t4I8eiM9kO", "jDqDV20zM9kP", "dVGDHgzScHyB", "tBSUPJEmcJhf", "z2eOUv5AHSZL", "O_mIki5Virkk", "X6TKBogIisNT", "loWnoGY8OPLA" ], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "metadata": { "id": "spdivf2TMnGC", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# Lesson 3 Basics" ] }, { "metadata": { "id": "c_Id55m6Jsbu", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## Pragmatic AI Labs\n", "\n" ] }, { "metadata": { "id": "e5p96AqpSDZa", "colab_type": "text" }, "cell_type": "markdown", "source": [ "![alt text](https://paiml.com/images/logo_with_slogan_white_background.png)\n", "\n", "This notebook was produced by [Pragmatic AI Labs](https://paiml.com/). You can continue learning about these topics by:\n", "\n", "* Buying a copy of [Pragmatic AI: An Introduction to Cloud-Based Machine Learning](http://www.informit.com/store/pragmatic-ai-an-introduction-to-cloud-based-machine-9780134863917)\n", "* Reading an online copy of [Pragmatic AI:Pragmatic AI: An Introduction to Cloud-Based Machine Learning](https://www.safaribooksonline.com/library/view/pragmatic-ai-an/9780134863924/)\n", "* Watching video [Essential Machine Learning and AI with Python and Jupyter Notebook-Video-SafariOnline](https://www.safaribooksonline.com/videos/essential-machine-learning/9780135261118) on Safari Books Online.\n", "* Watching video [AWS Certified Machine Learning-Speciality](https://learning.oreilly.com/videos/aws-certified-machine/9780135556597)\n", "* Purchasing video [Essential Machine Learning and AI with Python and Jupyter Notebook- Purchase Video](http://www.informit.com/store/essential-machine-learning-and-ai-with-python-and-jupyter-9780135261095)\n", "* Viewing more content at [noahgift.com](https://noahgift.com/)\n" ] }, { "metadata": { "id": "pBTeTbnRKG_k", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "qqAIvhP5N4iu", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 3.1 Write procedural code" ] }, { "metadata": { "id": "BwmnQEziVQ9k", "colab_type": "text" }, "cell_type": "markdown", "source": [ " ### Procedural Statements\n", " Procedural statements are literally statements that can be issued one line at a time. Below are types of procedural statements. These statements can be run in:\n", " * Jupyter Notebook\n", " * IPython shell\n", " * Python interpreter\n", " * Python scripts" ] }, { "metadata": { "id": "_jaxjKtPTeBx", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "three_type_of_energy = [\"protein\", \"carbohydrates\", \"fat\"]" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "H004n8R-WDGF", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### **Multiple procedural statements**" ] }, { "metadata": { "id": "xrZWZEFmV_bN", "colab_type": "code", "outputId": "4e179bd6-cad9-4165-f582-2c5c3a4c6351", "colab": { "base_uri": "https://localhost:8080/", "height": 51 } }, "cell_type": "code", "source": [ "protein, carbohydrate, fat = three_type_of_energy\n", "print(f\"{carbohydrate} sure taste good\")\n", "print(f\"{fat} isn't bad for you anymore?\")\n" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "carbohydrates sure taste good\n", "fat isn't bad for you anymore?\n" ], "name": "stdout" } ] }, { "metadata": { "id": "6IiZ6RnLWKsq", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Adding Numbers" ] }, { "metadata": { "id": "h049FhlhWPRm", "colab_type": "code", "outputId": "85c2b05e-9649-435e-bbb4-61f666a55ca5", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "protein = 4\n", "fat = 9\n", "carbohydrate = 4\n", "carbohydrate + protein" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "8" ] }, "metadata": { "tags": [] }, "execution_count": 61 } ] }, { "metadata": { "id": "mlszsN87WmcO", "colab_type": "text" }, "cell_type": "markdown", "source": [ "**Adding Phrases**" ] }, { "metadata": { "id": "FaE5GnCaWgbI", "colab_type": "code", "outputId": "40730303-1b6b-4c6a-bdd0-45f3c5d16852", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "\"a carbohydrate \" + \"has \" + str(carbohydrate) + \" calories\" " ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'a carbohydrate has 4 calories'" ] }, "metadata": { "tags": [] }, "execution_count": 63 } ] }, { "metadata": { "id": "WSrfrIO1XYts", "colab_type": "text" }, "cell_type": "markdown", "source": [ "**Complex statements**\n", "\n", "More complex statements can be created that use data structures like the belts variable, which is a list." ] }, { "metadata": { "id": "0nf0W0e3XSQS", "colab_type": "code", "outputId": "7250d000-5442-4e43-ea51-9516e9f6d0ed", "colab": { "base_uri": "https://localhost:8080/", "height": 68 } }, "cell_type": "code", "source": [ "for energy_type in three_type_of_energy:\n", " print(energy_type)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "protein\n", "carbohydrates\n", "fat\n" ], "name": "stdout" } ] }, { "metadata": { "id": "yHdWIarsN850", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 3.2 Use simple expressions and variables" ] }, { "metadata": { "id": "jYjzO6a4N88q", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### assert" ] }, { "metadata": { "id": "xXnbhsNjWTiu", "colab_type": "code", "outputId": "fd781d57-5d1b-4a56-9f25-7b88530c3491", "colab": { "base_uri": "https://localhost:8080/", "height": 181 } }, "cell_type": "code", "source": [ "assert carbohydrate == 9\n", " " ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "AssertionError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mcarbohydrate\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m9\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: " ] } ] }, { "metadata": { "id": "N0jWM3RIWdEC", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "assert carbohydrate == 4" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "dJhCYS9gWhE7", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### pass" ] }, { "metadata": { "id": "5JTGSrVLXC56", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "class Calorie: pass\n", "kcal = Calorie()\n" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "IQ6SXQ1hJo31", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "kcal.value = \"9\"" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "-ZupZ1esXMXN", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### del\n" ] }, { "metadata": { "id": "YSl9e1O6XQVo", "colab_type": "code", "outputId": "1bb7a93a-5d44-4127-b150-01496cdb3094", "colab": { "base_uri": "https://localhost:8080/", "height": 357 } }, "cell_type": "code", "source": [ "class Calorie: pass\n", "kcal = Calorie()\n", "%who_ls" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Calorie',\n", " 'breakfast',\n", " 'calories',\n", " 'carb',\n", " 'carbohydrate',\n", " 'carbs',\n", " 'egg_set',\n", " 'energy',\n", " 'energy_type',\n", " 'fat',\n", " 'food',\n", " 'ingredients',\n", " 'kcal',\n", " 'omelette',\n", " 'protein',\n", " 'snacks',\n", " 'this',\n", " 'three_type_of_energy',\n", " 'too_much_food',\n", " 'variable']" ] }, "metadata": { "tags": [] }, "execution_count": 70 } ] }, { "metadata": { "id": "sa1YcirqJ0J7", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "kcal.value = 9" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "q1Kfx2kQXbES", "colab_type": "code", "outputId": "2b14803e-1867-4d3d-b1a6-6218c40f5e4b", "colab": { "base_uri": "https://localhost:8080/", "height": 340 } }, "cell_type": "code", "source": [ "del kcal\n", "%who_ls" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Calorie',\n", " 'breakfast',\n", " 'calories',\n", " 'carb',\n", " 'carbohydrate',\n", " 'carbs',\n", " 'egg_set',\n", " 'energy',\n", " 'energy_type',\n", " 'fat',\n", " 'food',\n", " 'ingredients',\n", " 'omelette',\n", " 'protein',\n", " 'snacks',\n", " 'this',\n", " 'three_type_of_energy',\n", " 'too_much_food',\n", " 'variable']" ] }, "metadata": { "tags": [] }, "execution_count": 72 } ] }, { "metadata": { "id": "7sHqDgRiXlYS", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### return" ] }, { "metadata": { "id": "cS3zelFyXoXX", "colab_type": "code", "outputId": "4c08a16f-fe44-4c1b-b6a1-64428c5222d6", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "def food():\n", " return \"whey\"\n", "\n", "print(f\"Make {food()} while the sun shines\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Make whey while the sun shines\n" ], "name": "stdout" } ] }, { "metadata": { "id": "qFVa9v6FYOi0", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### yield" ] }, { "metadata": { "id": "AHJMMYqfYR47", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "def too_much_food():\n", " meal = [\"orange\", \"apple\", \"turkey\", \"ham\"]\n", " for snack in meal:\n", " yield snack\n", " " ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "Uiu8dH3IYoaX", "colab_type": "code", "outputId": "d54843d4-0b5f-437c-e4c8-0c81dddec746", "colab": { "base_uri": "https://localhost:8080/", "height": 51 } }, "cell_type": "code", "source": [ "snacks = too_much_food()\n", "print(next(snacks))\n", "print(next(snacks))" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "orange\n", "apple\n" ], "name": "stdout" } ] }, { "metadata": { "id": "6xXJnKUnKNqp", "colab_type": "code", "outputId": "6854502c-6548-46ae-98fe-b72368bb51b9", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "next(snacks)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'turkey'" ] }, "metadata": { "tags": [] }, "execution_count": 76 } ] }, { "metadata": { "id": "3vvXvpmwKQ08", "colab_type": "code", "outputId": "1a0fc4af-0bb1-4dea-8947-0a26e7b329e9", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "next(snacks)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'ham'" ] }, "metadata": { "tags": [] }, "execution_count": 77 } ] }, { "metadata": { "id": "PTL1xoBdKT45", "colab_type": "code", "outputId": "c1ae5143-d784-4fc9-c55a-95ccd20867dc", "colab": { "base_uri": "https://localhost:8080/", "height": 164 } }, "cell_type": "code", "source": [ "next(snacks)" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "StopIteration", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msnacks\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mStopIteration\u001b[0m: " ] } ] }, { "metadata": { "id": "4ZDzW5MqZ1RU", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### break\n" ] }, { "metadata": { "id": "0kjVX0pnZ3D6", "colab_type": "code", "outputId": "5894a0be-74a0-4340-eebd-a049682bbcf0", "colab": { "base_uri": "https://localhost:8080/", "height": 85 } }, "cell_type": "code", "source": [ "carbohydrate = 4\n", "calories = 0\n", "while True:\n", " calories += carbohydrate\n", " print(f\"Eating more carbohydrates {calories}\")\n", " if calories > 8:\n", " print(\"This is all I can eat\")\n", " break" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Eating more carbohydrates 4\n", "Eating more carbohydrates 8\n", "Eating more carbohydrates 12\n", "This is all I can eat\n" ], "name": "stdout" } ] }, { "metadata": { "id": "tfcD9G_TKTDp", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "6VpcRvywahUF", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### continue" ] }, { "metadata": { "id": "quS_WfM8alY-", "colab_type": "code", "outputId": "4fae0a60-0ba2-4da8-fa34-8febc0f2b763", "colab": { "base_uri": "https://localhost:8080/", "height": 68 } }, "cell_type": "code", "source": [ "three_type_of_energy = [\"protein\", \"sugar\", \"fat\"]\n", "for energy in three_type_of_energy:\n", " if energy == \"sugar\":\n", " print(f\"skipping {energy} for my health\")\n", " continue\n", " print(f\"eating {energy}\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "eating protein\n", "skipping sugar for my health\n", "eating fat\n" ], "name": "stdout" } ] }, { "metadata": { "id": "I2e-GRTObNek", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### import\n" ] }, { "metadata": { "id": "D1XcQeQ8bQCJ", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "import this" ], "execution_count": 0, "outputs": [] }, { "metadata": { "colab_type": "text", "id": "p8t4I8eiM9kO" }, "cell_type": "markdown", "source": [ "## 3.3 Work with the built-in types" ] }, { "metadata": { "colab_type": "text", "id": "jDqDV20zM9kP" }, "cell_type": "markdown", "source": [ "### dict" ] }, { "metadata": { "id": "4XJ5TAyycHbH", "colab_type": "code", "outputId": "d9e94016-cc6e-4acc-96b2-66c28f0e86c5", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "omelette = {\"egg\": 3, \"ham\": \"yes\"}\n", "type(omelette)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "dict" ] }, "metadata": { "tags": [] }, "execution_count": 87 } ] }, { "metadata": { "id": "dVGDHgzScHyB", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### list" ] }, { "metadata": { "id": "v2kHfbALcJGK", "colab_type": "code", "outputId": "400be13e-62e7-46f2-f5ac-4bbb977d34df", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "ingredients = [\"egg\", \"ham\", \"bacon\"]\n", "type(ingredients)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "list" ] }, "metadata": { "tags": [] }, "execution_count": 88 } ] }, { "metadata": { "id": "tBSUPJEmcJhf", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### set\n" ] }, { "metadata": { "id": "eOdfWOr8cNHH", "colab_type": "code", "outputId": "18c390c3-4c9e-4bcf-bba9-90a3c32edc2e", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "egg_set = set([\"egg\", \"egg\"])\n", "type(egg_set)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "set" ] }, "metadata": { "tags": [] }, "execution_count": 90 } ] }, { "metadata": { "id": "LzKIr5mZLn9S", "colab_type": "code", "outputId": "b7abb843-470c-4ebe-b62f-e10a88265bd7", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "egg_set" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'egg'}" ] }, "metadata": { "tags": [] }, "execution_count": 91 } ] }, { "metadata": { "id": "z2eOUv5AHSZL", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### tuple" ] }, { "metadata": { "id": "xEyAvrjdHUE1", "colab_type": "code", "outputId": "1e983865-ec67-4d7c-91bb-afde45b4bbb8", "colab": { "base_uri": "https://localhost:8080/", "height": 181 } }, "cell_type": "code", "source": [ "breakfast = (\"egg\",\"soup\")\n", "breakfast[0] = \"turkey\"" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mbreakfast\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"egg\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"soup\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mbreakfast\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"turkey\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ] }, { "metadata": { "id": "mB5Mcx3GLy0b", "colab_type": "code", "outputId": "fd89789f-b6ad-4df9-c065-dea941b6d549", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "breakfast[1]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'soup'" ] }, "metadata": { "tags": [] }, "execution_count": 95 } ] }, { "metadata": { "id": "5E7X1a9QLK0H", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 3.4 Printing" ] }, { "metadata": { "colab_type": "text", "id": "G7VMVJY9HNjQ" }, "cell_type": "markdown", "source": [ "### Printing" ] }, { "metadata": { "id": "n1r0n2V3Vs70", "colab_type": "code", "outputId": "3b1554a1-33d6-4c7a-f2df-60e14d50432d", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "print(\"omelets are tasty\")\n" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "omelets are tasty\n" ], "name": "stdout" } ] }, { "metadata": { "id": "oWl-6bpTMJnr", "colab_type": "code", "outputId": "7d44db3e-0733-4aa9-ad50-415167482214", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "variable = \"ham\"\n", "print(f\"I like {variable}\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "I like ham\n" ], "name": "stdout" } ] }, { "metadata": { "id": "8nUjNnApV6cC", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Create Variable and Use Variable" ] }, { "metadata": { "id": "mhjO2LpSVx9V", "colab_type": "code", "outputId": "7802f366-5d2b-440b-81dd-4fde62e71174", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "variable = \"omelets\";print(variable)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "omelets\n" ], "name": "stdout" } ] }, { "metadata": { "id": "YtGLHh1mdTGA", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Use print as a function" ] }, { "metadata": { "id": "id03bYS2dX2u", "colab_type": "code", "outputId": "e56ccfb5-d93e-4312-a5c7-3a35e6d6cf46", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "print(\"kombucha\", \"manuka honey\", sep=\"+\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "kombucha+manuka honey\n" ], "name": "stdout" } ] }, { "metadata": { "colab_type": "text", "id": "O_mIki5Virkk" }, "cell_type": "markdown", "source": [ "## 3.5 Perform basic math operations\n", "\n" ] }, { "metadata": { "id": "eRmipgSocZ9i", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Numbers and Arithmetic Operations\n", "\n", "Python is also a built-in calculator. Without installing any additional libraries it can do many simple and complex arithmetic operations." ] }, { "metadata": { "id": "xd6LxgEXcgP8", "colab_type": "text" }, "cell_type": "markdown", "source": [ "**Adding and Subtracting Numbers**" ] }, { "metadata": { "id": "mKwZGBoMb6_A", "colab_type": "code", "outputId": "012deab5-6e37-4c3e-e938-66b502b14978", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "steps = (1+1)-1\n", "print(f\"Two Steps Forward: One Step Back = {steps}\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Two Steps Forward: One Step Back = 1\n" ], "name": "stdout" } ] }, { "metadata": { "id": "nMKS0ZYkdjvp", "colab_type": "text" }, "cell_type": "markdown", "source": [ "**Multiplication with Decimals**\n", "\n", "Can use float type to solve decimal problems" ] }, { "metadata": { "id": "Xgrw0LDucinh", "colab_type": "code", "outputId": "c01e6e27-1b28-43cf-8fa8-f69bcd362d9a", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "body_fat_percentage = 0.10\n", "weight = 200\n", "fat_total = body_fat_percentage * weight\n", "print(f\"I weight 200lbs, and {fat_total}lbs of that is fat\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "I weight 200lbs, and 20.0lbs of that is fat\n" ], "name": "stdout" } ] }, { "metadata": { "id": "gjMkBnuxiCQW", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Can also use Decimal Library to set precision and deal with repeating decimal\n" ] }, { "metadata": { "id": "GN_PYDuDiBHg", "colab_type": "code", "outputId": "4a32c96d-3d86-4105-bbf8-51ed00273cda", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "from decimal import (Decimal, getcontext)\n", "\n", "getcontext().prec = 2\n", "Decimal(1)/Decimal(3)\n", "\n" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Decimal('0.33')" ] }, "metadata": { "tags": [] }, "execution_count": 107 } ] }, { "metadata": { "id": "f1_B6OUrdxlU", "colab_type": "text" }, "cell_type": "markdown", "source": [ "**Using Exponents**\n", "\n", "Using the Python math library it is straightforward to call 2 to the 3rd power" ] }, { "metadata": { "id": "Q2oC1HVhdmB9", "colab_type": "code", "outputId": "cf76c6ed-e0f0-4207-d07e-5eaf94bb9d0e", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "import math\n", "math.pow(2,3)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "8.0" ] }, "metadata": { "tags": [] }, "execution_count": 108 } ] }, { "metadata": { "id": "nkPcDry7jWt-", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Can also use built in exponent operator to accomplish same thing" ] }, { "metadata": { "id": "9aV-R-DljXEj", "colab_type": "code", "outputId": "45484ec4-1e15-48e7-ea2c-a62e8ff4b57b", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "2**3" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "8" ] }, "metadata": { "tags": [] }, "execution_count": 109 } ] }, { "metadata": { "id": "9cejMs6nNhKQ", "colab_type": "text" }, "cell_type": "markdown", "source": [ "multiply" ] }, { "metadata": { "id": "2w0o6EAjNfNg", "colab_type": "code", "outputId": "5935736e-78e1-4e03-87d8-5e77549127ec", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "2*3" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "6" ] }, "metadata": { "tags": [] }, "execution_count": 110 } ] }, { "metadata": { "id": "aYA_mApaI-Y7", "colab_type": "text" }, "cell_type": "markdown", "source": [ "this is regular multiplication" ] }, { "metadata": { "id": "6A_Vdvl_I8lg", "colab_type": "code", "outputId": "937270a6-1ae6-4d79-915d-4f06ba68ce02", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "2*3" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "6" ] }, "metadata": { "tags": [] }, "execution_count": 56 } ] }, { "metadata": { "id": "P6gOf9qtd6Nt", "colab_type": "text" }, "cell_type": "markdown", "source": [ "**Converting Between different numerical types**\n", "\n", "There are many numerical forms to be aware of in Python.\n", "A couple of the most common are:\n", "\n", "* Integers\n", "* Floats" ] }, { "metadata": { "id": "XCWg7yX-d210", "colab_type": "code", "outputId": "c4a0aed3-7394-4ab7-85f2-430bd4a7cfae", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "number = 100\n", "num_type = type(number).__name__\n", "print(f\"{number} is type [{num_type}]\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "100 is type [int]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "iW2f1Ik7eAzd", "colab_type": "code", "outputId": "2a7317ca-e4f2-4b4e-fe01-4e880c5b439e", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "number = float(100)\n", "num_type = type(number).__name__\n", "print(f\"{number} is type [{num_type}]\")" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "100.0 is type [float]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "5dnSfoXZFQuP", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "class Foo:pass\n", "f = Foo()" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "44v5W-HdFU4J", "colab_type": "code", "outputId": "ee42e20f-d270-435f-e7cc-a5aa320f6257", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "type(f)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "__main__.Foo" ] }, "metadata": { "tags": [] }, "execution_count": 114 } ] }, { "metadata": { "id": "MrrFXb99gQ1Z", "colab_type": "text" }, "cell_type": "markdown", "source": [ "**Numbers can also be rounded**\n", "\n", "Python Built in round " ] }, { "metadata": { "id": "Wjtnfol2iZDL", "colab_type": "code", "outputId": "ea6bed82-1fc6-4825-e97d-eac500f2de07", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "too_many_decimals = 1.912345897\n", "round(too_many_decimals, 3)\n", "#get more info\n", "#round?" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.912" ] }, "metadata": { "tags": [] }, "execution_count": 115 } ] }, { "metadata": { "id": "SuJDUTLFWUJz", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Numpy round" ] }, { "metadata": { "id": "PEabuAGGWXIF", "colab_type": "code", "outputId": "0f75132f-fe28-4e31-8f02-0e4994f552ef", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "import numpy as np\n", "np.round(too_many_decimals, 3)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1.912" ] }, "metadata": { "tags": [] }, "execution_count": 116 } ] }, { "metadata": { "id": "X3aHIe6qW8ab", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Pandas round" ] }, { "metadata": { "id": "J-x_sjiZW-gB", "colab_type": "code", "outputId": "c40516d8-6cd7-4c1d-a82e-c153ee878d78", "colab": { "base_uri": "https://localhost:8080/", "height": 80 } }, "cell_type": "code", "source": [ "import pandas as pd\n", "df = pd.DataFrame([too_many_decimals], columns=[\"A\"], index=[\"first\"])\n", "df.round(2)\n" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A
first1.91
\n", "
" ], "text/plain": [ " A\n", "first 1.91" ] }, "metadata": { "tags": [] }, "execution_count": 117 } ] }, { "metadata": { "id": "elnncfa0XtOt", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Simple benchmark of all three (**Python**, **numpy** and **Pandas** round): using **%timeit**\n", "\n", "*Depending on what is getting rounded (i.e. a very large DataFrame, performance may very, so knowing how to benchmark performance is important with round) *\n" ] }, { "metadata": { "id": "Yr1SXcF5Xw-L", "colab_type": "code", "outputId": "d18d8e94-68fc-4251-aca0-3aa8d99d640c", "colab": { "base_uri": "https://localhost:8080/", "height": 153 } }, "cell_type": "code", "source": [ "print(\"built in Python Round\")\n", "%timeit round(too_many_decimals, 2)\n", "\n", "print(\"numpy round\")\n", "%timeit np.round(too_many_decimals, 2)\n", "\n", "print(\"Pandas DataFrame round\")\n", "%timeit df.round(2)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "built in Python Round\n", "The slowest run took 21.00 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "1000000 loops, best of 3: 486 ns per loop\n", "numpy round\n", "The slowest run took 9.26 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "100000 loops, best of 3: 7.62 µs per loop\n", "Pandas DataFrame round\n", "1000 loops, best of 3: 951 µs per loop\n" ], "name": "stdout" } ] }, { "metadata": { "colab_type": "text", "id": "X6TKBogIisNT" }, "cell_type": "markdown", "source": [ "## 3.6 Use classes and objects with dot notation\n", "\n" ] }, { "metadata": { "id": "srx3voPVKPop", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Interacting with Special Class Methods and Other Class Techniques\n", "\n", "Class special methods have the signature ```__method__```:\n", "\n", "Examples include\n", "```\n", "__len__\n", "__call__\n", "__equal__\n", "\n", "```" ] }, { "metadata": { "id": "Uzzr1EhLxUnP", "colab_type": "code", "outputId": "74f9253c-a3bd-44e8-98de-f6977b284a9c", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "l = [1,2]\n", "len(l)\n", "#class Foo:pass\n", "#f = Foo()\n", "#len(f)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": { "tags": [] }, "execution_count": 121 } ] }, { "metadata": { "id": "IJ48YIJUKoYz", "colab_type": "code", "outputId": "d54236e3-c70e-4b0e-d745-99a8e6d9885d", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "class JonJones:\n", " \"\"\"Jon Jones class with customized length\"\"\"\n", " \n", " def __len__(self):\n", " return 84\n", "\n", "jon_jones = JonJones()\n", "len(jon_jones)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "84" ] }, "metadata": { "tags": [] }, "execution_count": 122 } ] }, { "metadata": { "id": "bqGdWZB3UEos", "colab_type": "code", "outputId": "cdbf49a0-59f5-4202-8f5e-1b35b6cb065b", "colab": { "base_uri": "https://localhost:8080/", "height": 198 } }, "cell_type": "code", "source": [ "class foo():pass\n", "f = foo()\n", "f.red = \"red\"\n", "len(f)" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfoo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mred\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"red\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: object of type 'foo' has no len()" ] } ] }, { "metadata": { "id": "tbTyE8mrKvcj", "colab_type": "text" }, "cell_type": "markdown", "source": [ "@property decorator is a shortcut for creating a read only property" ] }, { "metadata": { "id": "QtQiqN9JKq_X", "colab_type": "code", "outputId": "8a7373da-2b8d-4d4f-e183-465f61553dbb", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "class JonJones:\n", " \"\"\"Jon Jones class with read only property\"\"\"\n", " \n", " @property\n", " def reach(self):\n", " return 84\n", "\n", "jon_jones = JonJones()\n", "jon_jones.reach\n", "#jon_jones.reach = 85 #cannot set\n", "jon_jones.length = 85\n", "jon_jones.length" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "85" ] }, "metadata": { "tags": [] }, "execution_count": 125 } ] }, { "metadata": { "id": "WURULjSfO7sc", "colab_type": "code", "outputId": "e447db71-a08a-4ecb-89d2-5aea3a98e741", "colab": { "base_uri": "https://localhost:8080/", "height": 181 } }, "cell_type": "code", "source": [ "jon_jones.reach\n", "jon_jones.reach = 85" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "AttributeError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mjon_jones\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreach\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mjon_jones\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreach\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m85\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: can't set attribute" ] } ] }, { "metadata": { "id": "grcojpMOKyao", "colab_type": "text" }, "cell_type": "markdown", "source": [ "@staticmethod bolts a function onto a class" ] }, { "metadata": { "id": "vctQpjK-K0AE", "colab_type": "code", "outputId": "c8378034-ede1-46ca-d568-83aa6128c4fd", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "class JonJones:\n", " \"\"\"Jon Jones Class with 'bolt-on' reach method\n", " self isn't needed\n", " \"\"\"\n", " \n", " @staticmethod\n", " def reach():\n", " return 84\n", "\n", "jon_jones =JonJones()\n", "jon_jones.reach()" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "84" ] }, "metadata": { "tags": [] }, "execution_count": 130 } ] }, { "metadata": { "id": "dhabI3o_dWd7", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Immutability concepts with Objects" ] }, { "metadata": { "id": "8S3WDMvVdJaz", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "class Foo:\n", " \n", " @property\n", " def unbreakable(self):\n", " return \"David\"\n", "\n" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "xpR4L8XVdd1Q", "colab_type": "code", "outputId": "6b9ce786-52c1-49ee-e1bb-61bb1cd4a7f7", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "foo = Foo()\n", "foo.unbreakable " ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'David'" ] }, "metadata": { "tags": [] }, "execution_count": 133 } ] }, { "metadata": { "id": "GZwabNvndsCs", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "foo.not_unbreakable = \"Elijah\"" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "VuSZ9Y49egiO", "colab_type": "text" }, "cell_type": "markdown", "source": [ "@property acts like an read only attribute, but it isn't" ] }, { "metadata": { "id": "A4yseU7fecuW", "colab_type": "code", "outputId": "cd344e59-5c5c-401d-e77d-4321c8703375", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "foo.__dict__" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'not_unbreakable': 'Elijah'}" ] }, "metadata": { "tags": [] }, "execution_count": 135 } ] }, { "metadata": { "id": "DmLJWuUtfE2-", "colab_type": "text" }, "cell_type": "markdown", "source": [ "You can change an attribute on the object, but not the read only property" ] }, { "metadata": { "id": "lQzn9BCoeeLu", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "foo.not_unbreakable = \"Mr. Glass\"" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "0iS_9Q0AfWtY", "colab_type": "code", "outputId": "f6518647-88d8-45e0-f626-0df7f66f94e0", "colab": { "base_uri": "https://localhost:8080/", "height": 164 } }, "cell_type": "code", "source": [ "foo.unbreakable = \"Bruce Willis\"" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "AttributeError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munbreakable\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Bruce Willis\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: can't set attribute" ] } ] }, { "metadata": { "id": "loWnoGY8OPLA", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## Notes" ] }, { "metadata": { "id": "OAAlmCIQyAwH", "colab_type": "text" }, "cell_type": "markdown", "source": [ "" ] } ] }