{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "python_warmup.ipynb", "provenance": [], "collapsed_sections": [], "authorship_tag": "ABX9TyOEi8XHqenlASf9vONzCFp+", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "sfPTTBWm7-ck", "colab_type": "text" }, "source": [ "## Python Warm Up\n", "\n", "In this exercise, we will take a look at some basic Python Concepts needed for\n", "day-to-day coding. " ] }, { "cell_type": "markdown", "metadata": { "id": "oNRBa6IP8MSq", "colab_type": "text" }, "source": [ "Check the installed Python version." ] }, { "cell_type": "code", "metadata": { "id": "M4IpqoZB74T-", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "513abea9-a23e-4d6f-94a6-cf15657e304c" }, "source": [ "! python --version" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "Python 3.6.9\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "rPARvah08TyU", "colab_type": "text" }, "source": [ "## Simple For Loop" ] }, { "cell_type": "code", "metadata": { "id": "5GvPz6lq8K02", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 199 }, "outputId": "d0b2a3cb-82ec-4a2b-8248-01f4797d4291" }, "source": [ "for i in range(10):\n", " print(i)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "F6n81SAs8fCe", "colab_type": "text" }, "source": [ "## List" ] }, { "cell_type": "code", "metadata": { "id": "EAGPmo_p8Rnb", "colab_type": "code", "colab": {} }, "source": [ "list_items = ['a', 'b', 'c', 'd', 'e']" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "kYEIV6XD8lM4", "colab_type": "text" }, "source": [ "### Retrieving an Element" ] }, { "cell_type": "code", "metadata": { "id": "AelxfaSb8eMa", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "600ded1b-0dc2-4212-dcc6-8c8342c61776" }, "source": [ "list_items[2]" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" }, "text/plain": [ "'c'" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "markdown", "metadata": { "id": "Diq7GJ2D8xRz", "colab_type": "text" }, "source": [ "### Append New Values" ] }, { "cell_type": "code", "metadata": { "id": "HiLbXKfI8kbd", "colab_type": "code", "colab": {} }, "source": [ "list_items.append('f')" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "73QwwXz_8sIS", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "22b7f8bd-e795-45bc-f2b9-acad4171b01e" }, "source": [ "list_items" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['a', 'b', 'c', 'd', 'e', 'f']" ] }, "metadata": { "tags": [] }, "execution_count": 6 } ] }, { "cell_type": "markdown", "metadata": { "id": "BDgDdUpt882f", "colab_type": "text" }, "source": [ "### Remove an Element" ] }, { "cell_type": "code", "metadata": { "id": "2GKIL0r68t7P", "colab_type": "code", "colab": {} }, "source": [ "list_items.remove('a')" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "pQuXKIdK83B8", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "4bc57378-bbcb-453d-9560-93d72943cd01" }, "source": [ "list_items" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['b', 'c', 'd', 'e', 'f']" ] }, "metadata": { "tags": [] }, "execution_count": 9 } ] }, { "cell_type": "markdown", "metadata": { "id": "g4Vx6Xp_9A3w", "colab_type": "text" }, "source": [ "## Dictionary" ] }, { "cell_type": "code", "metadata": { "id": "uUy92xNf874L", "colab_type": "code", "colab": {} }, "source": [ "dictionary_items = {'a':1, 'b': 2, 'c': 3}" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "6ScumadJ9L1B", "colab_type": "text" }, "source": [ "### Retrieving an Item by Key" ] }, { "cell_type": "code", "metadata": { "id": "0wfx1NPd9HW-", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "6733f5f5-fb16-4100-be1e-9644c8e05646" }, "source": [ "dictionary_items['b']" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "2" ] }, "metadata": { "tags": [] }, "execution_count": 14 } ] }, { "cell_type": "markdown", "metadata": { "id": "SLZaxNGF9Ti6", "colab_type": "text" }, "source": [ "### Append New Item with Key" ] }, { "cell_type": "code", "metadata": { "id": "Yg5l5JWd9KSd", "colab_type": "code", "colab": {} }, "source": [ "dictionary_items['d'] = 5" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "CogFEHMC9RR6", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "8e4ece35-4ff4-43d0-d987-9ab92fb7a357" }, "source": [ "dictionary_items" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'a': 1, 'b': 2, 'c': 4, 'd': 5}" ] }, "metadata": { "tags": [] }, "execution_count": 18 } ] }, { "cell_type": "markdown", "metadata": { "id": "8T72elv79s4h", "colab_type": "text" }, "source": [ "### Delete an Item with Key" ] }, { "cell_type": "code", "metadata": { "id": "ulL6FICs9ShT", "colab_type": "code", "colab": {} }, "source": [ "del dictionary_items['a'] " ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "L7LBwW_g9nqX", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "bd253ebf-5234-48e6-e123-b8924edcbfcc" }, "source": [ "dictionary_items" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'b': 2, 'c': 4, 'd': 5}" ] }, "metadata": { "tags": [] }, "execution_count": 20 } ] }, { "cell_type": "code", "metadata": { "id": "rfZxTSqY9q8h", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "N7tJWW7x92gr", "colab_type": "text" }, "source": [ "## Comparators" ] }, { "cell_type": "code", "metadata": { "id": "j6XSFo4t98N2", "colab_type": "code", "colab": {} }, "source": [ "x = 10\n", "y = 20 \n", "z = 30" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "CQYmzcXp9-EQ", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "eb294f35-ec00-42bb-e8fc-7d395ba5d4cf" }, "source": [ "x > y " ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 23 } ] }, { "cell_type": "code", "metadata": { "id": "nkUknFCR-EDi", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "944a8680-f510-4da9-8a5a-aa496d4984f7" }, "source": [ "x < z" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": { "tags": [] }, "execution_count": 24 } ] }, { "cell_type": "code", "metadata": { "id": "hpfneYGG-J6X", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "1591572c-89ac-452b-9c2d-b7f9a4327e44" }, "source": [ "z == x" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": { "tags": [] }, "execution_count": 25 } ] }, { "cell_type": "code", "metadata": { "id": "fgiWJsa0-Lch", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "4a6d1e73-3d07-4bf9-8d61-e58cc8ee6b99" }, "source": [ "if x < z:\n", " print(\"This is True\")" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "This is True\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "jf16U36c-QYM", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "b4dc6050-172d-43e6-ff57-e8b3e1e20f65" }, "source": [ "if x > z:\n", " print(\"This is True\")\n", "else:\n", " print(\"This is False\") " ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "This is False\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "Fjvmki23-Yee", "colab_type": "text" }, "source": [ "## Arithmetic" ] }, { "cell_type": "code", "metadata": { "id": "oQxoWP72-VV4", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "62a62e52-8cb1-46d0-938e-f80410d84890" }, "source": [ "k = x * y * z\n", "k" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "6000" ] }, "metadata": { "tags": [] }, "execution_count": 28 } ] }, { "cell_type": "code", "metadata": { "id": "FexgTnGE-dA3", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "1189e480-f18f-4354-e5fb-887c52ccdc00" }, "source": [ "j = x + y + z\n", "j" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "60" ] }, "metadata": { "tags": [] }, "execution_count": 29 } ] }, { "cell_type": "code", "metadata": { "id": "-sT6kKkq-hwJ", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "86e769b9-372e-4e2f-e438-de6289b9285c" }, "source": [ "m = x -y \n", "m" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "-10" ] }, "metadata": { "tags": [] }, "execution_count": 30 } ] }, { "cell_type": "markdown", "metadata": { "id": "aNNhnougOEVV", "colab_type": "text" }, "source": [ "" ] }, { "cell_type": "code", "metadata": { "id": "4AR0E_Dr-7k-", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "182662ee-5d88-4912-d8a6-ff43371deb70" }, "source": [ "n = x / z\n", "n" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0.3333333333333333" ] }, "metadata": { "tags": [] }, "execution_count": 31 } ] }, { "cell_type": "markdown", "metadata": { "id": "fuIznlr-OTpI", "colab_type": "text" }, "source": [ "## Numpy" ] }, { "cell_type": "markdown", "metadata": { "id": "hy4h3Dr5OFPR", "colab_type": "text" }, "source": [ "### Create a Random Numpy Array " ] }, { "cell_type": "code", "metadata": { "id": "GeK34E0zON_b", "colab_type": "code", "colab": {} }, "source": [ "import numpy as np" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "Pv4zprn9OHFd", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "d9962fc2-8ae6-418f-8a22-f50ee85ba03f" }, "source": [ "a = np.random.rand(100)\n", "a.shape" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(100,)" ] }, "metadata": { "tags": [] }, "execution_count": 34 } ] }, { "cell_type": "markdown", "metadata": { "id": "TsGHk2AdOdzA", "colab_type": "text" }, "source": [ "### Reshape Numpy Array" ] }, { "cell_type": "code", "metadata": { "id": "soPCH8VHOKwy", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "73d3668f-215c-44ca-b60a-a24af9ea8e4d" }, "source": [ "b = a.reshape(10,10)\n", "b.shape" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(10, 10)" ] }, "metadata": { "tags": [] }, "execution_count": 35 } ] }, { "cell_type": "markdown", "metadata": { "id": "Qs53WmYPOjfm", "colab_type": "text" }, "source": [ "### Manipulate Array Elements" ] }, { "cell_type": "code", "metadata": { "id": "chodumrwOepy", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "7ff86837-2068-4794-d701-32f2b8f714bc" }, "source": [ "c = b * 10\n", "c[1][0]" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "7.916261386148415" ] }, "metadata": { "tags": [] }, "execution_count": 40 } ] }, { "cell_type": "code", "metadata": { "id": "UgYDDDrgOtgk", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "117b98d6-b23e-4842-f191-b29aa2ad0ed2" }, "source": [ "c = np.mean(b,axis=1)\n", "c.shape" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(10,)" ] }, "metadata": { "tags": [] }, "execution_count": 41 } ] }, { "cell_type": "code", "metadata": { "id": "dskGK1HJUG4F", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "26eca454-58a2-453e-97de-5c2b511b155c" }, "source": [ "print(c)" ], "execution_count": null, "outputs": [ { "output_type": "stream", "text": [ "[0.58068464 0.75920489 0.55326323 0.42282685 0.37987765 0.44600994\n", " 0.42849223 0.48782651 0.58758143 0.5704763 ]\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "ZPxzhqDpUHmY", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": null, "outputs": [] } ] }