{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "fVFXQKGbLeDL" }, "source": [ "

LU2: Python String, Integer, Float & Boolean

" ] }, { "cell_type": "markdown", "metadata": { "id": "FkHni0zv_kJF" }, "source": [ "**Python String**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UJHZQ6R2r97I", "outputId": "77907f83-2cd8-4453-db5c-403b4581463e" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "Hello World!\n" ] } ], "source": [ "a = 'Hello World!'\n", "print(type(a))\n", "print(a)" ] }, { "cell_type": "markdown", "source": [], "metadata": { "id": "KiLeF25mVvcW" } }, { "cell_type": "code", "source": [ "print(a)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Ae_MERFTVxsg", "outputId": "6af59461-874e-4e86-eed7-5e0923ddf5b5" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hello World!\n" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "Yj6Vo6Aj_xxt" }, "source": [ "**Exercise 2.1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "gGu2DYyd_1tM" }, "outputs": [], "source": [ "firstName = 'aleX'\n", "lastName = 'tan'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kja4zViRCXwn", "outputId": "3e2901f4-8724-4ec6-c6d5-e881909140e5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Alex TAN\n" ] } ], "source": [ "print('{} {}'.format(firstName.capitalize(), lastName.upper()))" ] }, { "cell_type": "markdown", "metadata": { "id": "dVz4_9-2FG55" }, "source": [ "**Exercise 2.2**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "_vGcSmJ5AB91" }, "outputs": [], "source": [ "fruit = \" durian \"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "flJFLfIyCTlX", "outputId": "950bb4c9-0abd-4867-c3ae-ea4f15849f6e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Durian is my favorite fruit.\n" ] } ], "source": [ "fav = fruit.strip()\n", "print('{} is my favorite fruit.'.format(fav.capitalize()))" ] }, { "cell_type": "markdown", "metadata": { "id": "N-YQOK97FsHY" }, "source": [ "**Exercise 2.3**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "17ekgvXzAOT2", "outputId": "08ca0c32-79a7-4559-e480-e9e1271197c6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Today is a lousy day!\n" ] } ], "source": [ "old = 'Today is a lousy day!'\n", "print(old)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "q4yg_5CTCLgJ", "outputId": "c4daba44-12c0-423b-db62-43b08ae9b467" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Today is a great day!\n" ] } ], "source": [ "new = old.replace('lousy','great')\n", "print(new)" ] }, { "cell_type": "markdown", "metadata": { "id": "UkM4HcbzeHsb" }, "source": [ "**Python Number**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "zuW0erZg5Y6Q" }, "outputs": [], "source": [ "x = 2\n", "y = 3.5\n", "z = 4 + 6j" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rCqQbS_YCFo-", "outputId": "850cd4ec-c2ed-4b72-ebed-795d5156a0c2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n" ] } ], "source": [ "print(type(x))\n", "print(type(y))\n", "print(type(z))" ] }, { "cell_type": "markdown", "metadata": { "id": "YeT3vx1_18H2" }, "source": [ "**Examples: Printing Numbers**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ZhUM97DF2CiM" }, "outputs": [], "source": [ "a = 2.3456; b = 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Mcvi7szY2RE0", "outputId": "5e2025d6-7980-4782-be80-f5f3629b34cd" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.3456 + 5 = 7.3456\n" ] } ], "source": [ "print(\"{} + {} = {}\".format (a,b,a+b))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kpClkItv3jvO", "outputId": "7c477883-f5db-4d83-8eee-540eaa689f3b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 + 2.3456 = 7.3456\n" ] } ], "source": [ "print(\"{1} + {0} = {2}\".format (a,b,a+b))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mhZy7S_R3lqM", "outputId": "09f85171-a538-4025-b11f-d29451df4cb4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.35 + 5.00 = 7.35\n" ] } ], "source": [ "print(\"{:0.2f} + {:0.2f} = {:0.2f}\".format (a,b,a+b))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lRsGIC0R3nBI", "outputId": "ca9df5da-d8ef-435d-827a-a228f1270201" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.35 + 5.00 = 7.35\n" ] } ], "source": [ "print(\"{0:0.2f} + {1:0.2f} = {2:0.2f}\".format (a,b,a+b))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4UGfvxas3pAs", "outputId": "2454b2e1-22e3-4af0-976b-268868deabc0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 2.35 + 5.00 = 7.35\n" ] } ], "source": [ "print(\"{0:5.2f} + {1:5.2f} = {2:5.2f}\".format (a,b,a+b))" ] }, { "cell_type": "markdown", "metadata": { "id": "cYpCNDlJfTfz" }, "source": [ "**Exercise 2.4**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Va9QX6Jf-Ad_" }, "outputs": [], "source": [ "x = 2.333\n", "y = 5.666\n", "z = 7.888" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-1GevajkCAdf", "outputId": "f5e8da28-7e5e-4437-d442-09b40cf4448f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The result of \"00002.33 + 5.67 + 7.89\" is 15.887\n", "The result of \" 2.33 + 5.67 + 7.89\" is 15.887\n", "The result of \"2.33 + 5.67 + 7.89\" is 15.887\n" ] } ], "source": [ "print('The result of \"{:08.2f} + {:0.2f} + {:0.2f}\" is {:0.3f}'.format(x,y,z,x+y+z))\n", "print('The result of \"{:8.2f} + {:0.2f} + {:0.2f}\" is {:0.3f}'.format(x,y,z,x+y+z))\n", "print('The result of \"{:0.2f} + {:0.2f} + {:0.2f}\" is {:0.3f}'.format(x,y,z,x+y+z))" ] }, { "cell_type": "markdown", "metadata": { "id": "M6g_w8ZffWuj" }, "source": [ "**Exercise 2.5**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-bvT6wirfXDj", "outputId": "d83a6c1e-f451-4a2a-8525-ca928963b3a8" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Monthly Salary:\t$ 3,250.00\n", "Annual Salary :\t$ 39,000.00\n" ] } ], "source": [ "mthly = 3250\n", "print('Monthly Salary:\\t${:10,.2f}'.format(mthly))\n", "print('Annual Salary :\\t${:10,.2f}'.format(mthly*12))" ] }, { "cell_type": "markdown", "metadata": { "id": "JNkg4kekJnYH" }, "source": [ "**Exercise 2.6**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "JB0YQILul9OD" }, "outputs": [], "source": [ "a = (10 > 7)\n", "b = (2 == 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rHuPuTNJBzHA", "outputId": "8c053d3e-e006-43fb-acf8-1addabfce7d7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "True\n" ] } ], "source": [ "print(type(a))\n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "sww7866GB7zW", "outputId": "bb752710-99cc-4395-d574-eb2a39414208" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(b)" ] }, { "cell_type": "markdown", "metadata": { "id": "ViK_wTAnMFuu" }, "source": [ "

LU3: Python Operators

" ] }, { "cell_type": "markdown", "metadata": { "id": "abN4_HzRLdzG" }, "source": [ "**Exercise 3.1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "FpdvRra1J_gm" }, "outputs": [], "source": [ "candies = 91\n", "people = 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3ilGMi9oExxn", "outputId": "3df9da73-9dbf-4b85-e9c9-9d7be58bef5a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "giveAway = candies%people\n", "print(giveAway)" ] }, { "cell_type": "markdown", "metadata": { "id": "udj61UbbSrzD" }, "source": [ "**Exercise 3.2**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Ntx2DZ89RVE1", "outputId": "41c5105b-a062-490f-ac4b-f88f73532a0b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n", "True\n" ] } ], "source": [ "name = ['Alex', 'Berlinda', 'Catherine', 'David']\n", "print('Alex' in name)\n", "print('Eddy' in name)\n", "print('berlinda' not in name)" ] }, { "cell_type": "markdown", "metadata": { "id": "DmdscMORMPsF" }, "source": [ "

LU4: Python List, Tuple & Dictionary

" ] }, { "cell_type": "markdown", "metadata": { "id": "UAbcflSFJ-Wk" }, "source": [ "**List**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ULrom7rAVCnH" }, "outputs": [], "source": [ "a = [2, 4, 6, 8, 10, 12] \n", "b = ['apple', 'banana', 'coconut', 'durian']" ] }, { "cell_type": "markdown", "metadata": { "id": "hqqQGjkCLQSz" }, "source": [ "**Slicing List**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "RxcSEhgEKZKL" }, "outputs": [], "source": [ "a = [2, 4, 6, 8, 10, 12, 14] " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eX_9-fOfKfAf", "outputId": "269f6d3a-e7d1-422d-99cb-ac25c2fb4f96" }, "outputs": [ { "data": { "text/plain": [ "[4, 6]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1:3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dzqdRxF0KgzS", "outputId": "8a89994e-44a1-49e3-8ae4-b0e53c0136f9" }, "outputs": [ { "data": { "text/plain": [ "[4, 6, 8, 10, 12]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RpHzl1v3KhiD", "outputId": "a860b219-5d98-4ed6-9d16-3c5dce42991a" }, "outputs": [ { "data": { "text/plain": [ "[6, 8, 10, 12]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0x2on1VjKkle", "outputId": "69c504eb-ff25-4a6b-815b-81bf79358ba7" }, "outputs": [ { "data": { "text/plain": [ "[2, 4, 6, 8, 10]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "hcIpTKzGKl_q", "outputId": "897c6533-8197-444b-ea76-04da4a4f7952" }, "outputs": [ { "data": { "text/plain": [ "[2, 4, 6, 8]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:-2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wZz9b9bDT_dw", "outputId": "2a995b74-8923-490e-8f5f-dceeb6ad4e3e" }, "outputs": [ { "data": { "text/plain": [ "[2, 6, 10]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[::2]" ] }, { "cell_type": "markdown", "metadata": { "id": "z6m_UPLoZJ6L" }, "source": [ "**Adding Elements to List**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7ah5hSC4YV1t", "outputId": "2d4c5def-179a-4629-ed0b-e12d1fe1d19b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'coconut', 'durian']\n" ] } ], "source": [ "a = ['apple', 'banana', 'coconut', 'durian']\n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eSCNHbRjZWrC", "outputId": "a975e46c-d0b0-4366-aa02-a5e6f5f44798" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'coconut', 'durian', 'strawberry']\n" ] } ], "source": [ "a.append('strawberry')\n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XRLJ9MYgZZW8", "outputId": "e53142b7-7034-4c47-c97a-99cd65a34b90" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'coconut', 'orange', 'durian', 'strawberry']\n" ] } ], "source": [ "a.insert(3,'orange')\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "id": "5Qa3nAjRa8I7" }, "source": [ "**Removing Elements from List**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aOFSjLyLbGIK", "outputId": "7fb02f67-e1bc-4ab4-92a9-b785b909e534" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'coconut', 'orange', 'durian', 'strawberry']\n" ] } ], "source": [ "a = ['apple', 'banana', 'coconut', 'orange', 'durian', 'strawberry']\n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 188 }, "id": "bID3e-KaagcD", "outputId": "7f7e723e-0736-458b-8ae0-cfa26106ab90" }, "outputs": [ { "ename": "ValueError", "evalue": "ignored", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\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[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'coconut'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ], "source": [ "a.remove('coconut')\n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Fj6aUrUzbQ_7" }, "outputs": [], "source": [ "a.pop()\n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2fKbSIwPbSls", "outputId": "dd62af90-3dc7-44e0-d267-05dacdb2dfc0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'orange', 'durian']\n" ] } ], "source": [ "a.pop(1)\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "id": "AjyqlM4YczEa" }, "source": [ "**Copying a List**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jYTSzE8qczNR", "outputId": "087af502-48a4-443a-ecbb-f8d532ac0a89" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'coconut', 'durian']\n" ] } ], "source": [ "a = ['apple', 'banana', 'coconut', 'durian']\n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "naTDATRVc7gs", "outputId": "93b40d7c-60fd-4f3f-8461-ac1b0f4f637a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'coconut', 'durian']\n", "['apple', 'banana', 'coconut', 'durian']\n" ] } ], "source": [ "b = a.copy()\n", "print(a)\n", "print(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "l499LBk0c-Cz", "outputId": "e438bac1-29d2-4225-e1ab-cea86c888aa1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4, 6, 8]\n", "['apple', 'banana', 'coconut', 'durian']\n", "[2, 4, 6, 8]\n" ] } ], "source": [ "c = a\n", "print(a)\n", "print(b)\n", "print(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "tBxsobw9dA_T", "outputId": "95185f4a-3d98-435f-9d50-8435a81e8fb5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['apple', 'banana', 'coconut']\n", "['apple', 'banana', 'coconut', 'durian']\n", "['apple', 'banana', 'coconut']\n" ] } ], "source": [ "c.pop()\n", "print(a)\n", "print(b)\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": { "id": "9GUnB4RSLuhW" }, "source": [ "**Exercise 4.1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UgmWduPyLyhH", "outputId": "7a60e5aa-76ab-4ddd-addc-c72fd4d0775d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Toyota', 'Mercedes-Benz', 'BMW', 'Honda', 'Hyundai']\n" ] } ], "source": [ "cars = ['Toyota','Mercedes-Benz','BMW','Honda','Hyundai']\n", "print(cars)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cpIbQ9suO-9c", "outputId": "0b9619ee-1fa7-4d81-b991-2e4245f5ca2c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Toyota', 'Mercedes-Benz', 'BMW', 'Honda', 'Hyundai', 'Mazda']\n" ] } ], "source": [ "cars.append('Mazda')\n", "print(cars)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "D-JT93UbPk2R", "outputId": "61521f63-f728-4a89-d2ce-14b9e2ae8410" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Toyota', 'Mercedes-Benz', 'BMW', 'Honda', 'Mazda']\n" ] } ], "source": [ "cars.remove('Hyundai')\n", "print(cars)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YBhAp5hOQFsf", "outputId": "aa6e018d-03ef-4bf1-fffa-e7b4dc777730" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Toyota', 'Mercedes-Benz', 'BMW', 'Honda', 'Hyundai', 'Mazda']\n" ] } ], "source": [ "sgcars = cars.copy()\n", "print(sgcars)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LASgLIZXQVnS", "outputId": "f4aebf9d-a774-472d-fa77-bd6413c3fadd" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['BMW', 'Honda', 'Hyundai', 'Mazda', 'Mercedes-Benz', 'Toyota']\n" ] } ], "source": [ "sgcars.sort()\n", "print(sgcars)" ] }, { "cell_type": "markdown", "metadata": { "id": "qPPs7_ABd3A6" }, "source": [ "**Performing Math on a List**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "bFBZPIZzd3Lt" }, "outputs": [], "source": [ "a = [1, 2, 2, 3, 4, 5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "faXvHdUOi2rn" }, "outputs": [], "source": [ "print(sum(a))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "3db0N8sAi5hi", "outputId": "4935198f-f104-483e-d441-33afb3461306" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(min(a))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dvYL-Ynzi_QX", "outputId": "2e783b6d-fa15-450a-af93-c0393ebb92f3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(max(a))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "OAjArw_pjBxm", "outputId": "97f0d8d4-6cd1-42d3-caf2-15f8c429c711" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] } ], "source": [ "print(len(a))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0rkHlQoojEJ9", "outputId": "caa00ba4-ee4d-486c-c948-27311a32df7c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(a.count(5))" ] }, { "cell_type": "markdown", "metadata": { "id": "smE28H1Vm1fu" }, "source": [ "**Tuple**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "OIwtVkKTm3wO" }, "outputs": [], "source": [ "a = ('apple', 'banana', 'coconut', 'durian')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KLntcd-6nCb2", "outputId": "571cdc0c-1a3b-4f7b-f460-8578492d8e4b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('apple', 'banana', 'coconut', 'durian')\n", "banana\n" ] } ], "source": [ "print(a)\n", "print(a[1])" ] }, { "cell_type": "markdown", "metadata": { "id": "4pFImBkWmyQF" }, "source": [ "**Dictionary**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Kk9rFZirjN17", "outputId": "478defb2-22c6-47ec-d881-4764f406e78e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "{'China': 'Beijing', 'Japan': 'Tokyo'}\n" ] } ], "source": [ "capitals = {'China':'Beijing', 'Japan':'Tokyo'}\n", "print(type(capitals))\n", "print(capitals)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "W2GTfWqVmSWm", "outputId": "0b2a2cd6-4628-4e13-b348-fd87237d924c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Beijing\n" ] } ], "source": [ "print(capitals['China'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "qyUaQW21KFUB", "outputId": "80a99e58-8d6a-4943-ea23-fdd0a6ce0908" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'China': 'Beijing', 'Japan': 'Tokyo', 'Germany': 'Berlin'}\n" ] } ], "source": [ "capitals['Germany'] = 'Berlin'\n", "print(capitals)" ] }, { "cell_type": "markdown", "metadata": { "id": "8K9j34XaKoqq" }, "source": [ "**Exercise 4.2**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SJma6JG1KmMV", "outputId": "76d1f5e4-0c91-4035-d242-19fdba4ce13f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}\n" ] } ], "source": [ "squares = dict()\n", "for i in range(1,11):\n", " squares[i] = i**2\n", "print(squares) " ] }, { "cell_type": "markdown", "metadata": { "id": "LmGon08kMXLK" }, "source": [ "

LU5: Python Control Structures

" ] }, { "cell_type": "markdown", "metadata": { "id": "rPTNOAuvxm0T" }, "source": [ "**Selection Structure: if ... elif ... else**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ZSQ-D5CFmd5T" }, "outputs": [], "source": [ "x = int(input('Enter a number: '))\n", "\n", "if x < 0:\n", " print('This is a negative number')\n", "\n", "elif x > 0:\n", " print('This is a positive number')\n", " \n", "else:\n", " print('The number is a zero')" ] }, { "cell_type": "markdown", "metadata": { "id": "NsstUI6WyEwS" }, "source": [ "**Exercise 5.1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6jpQuMLbyISt", "outputId": "fd88b7b2-919c-4cfb-a5cf-4b9911fdb972" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "How are you feeling today? :happy\n", "happy\n", "Be Happy!\n" ] } ], "source": [ "mood = input('How are you feeling today? :' )\n", "print(mood)\n", "if mood=='Happy':\n", " print(\"Excellent!\")\n", "elif mood=='Sad':\n", " print(\"There there now.\")\n", "elif mood=='Angry':\n", " print(\"Relax Lah!\")\n", "else:\n", " print(\"Be Happy!\")" ] }, { "cell_type": "markdown", "metadata": { "id": "oZYEuyFqNey7" }, "source": [ "**Exercise 5.1 (Enhanced)**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "O3aY2nNJNeNW", "outputId": "6eea5f0e-e6a9-44b0-c112-e51014b83e04" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "How are you feeling today? :HAPPY\n", "happy\n", "Excellent!\n" ] } ], "source": [ "mood = input('How are you feeling today? :' ).lower()\n", "print(mood)\n", "if mood=='happy':\n", " print(\"Excellent!\")\n", "elif mood=='sad':\n", " print(\"There,there now.\")\n", "elif mood=='angry':\n", " print(\"Relax Lah!\")\n", "else:\n", " print(\"Be Happy!\")" ] }, { "cell_type": "markdown", "metadata": { "id": "8rEgFrYw0Tpw" }, "source": [ "**while Loop**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "grtrBU0f0Ydu", "outputId": "37b7236a-1904-41ac-9ac9-735504daca9e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "a = 1\n", "while (a < 10):\n", " print(a)\n", " a = a + 1\n", "\n", "m=0\n", "while (m < 10):\n", " print(m)\n", " m = m + 1" ] }, { "cell_type": "markdown", "metadata": { "id": "wOeU0203PnzC" }, "source": [ "**Range Function**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "nFy9W0PWPsx_", "outputId": "88a1c924-b00b-4fb3-b6d7-65e4ca7f88a2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jM5BNvGGQQrr", "outputId": "45caa271-e1ec-44ae-de60-8d0e3de200be" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "for i in range(1,6):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "RPW9Bg34QYj0" }, "outputs": [], "source": [ "for i in range(2,11,2):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": { "id": "Jyj8ka0s2erW" }, "source": [ "**for Loop**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "NFnw_bE-0bjA" }, "outputs": [], "source": [ "for j in range(1,10,2):\n", " print('Hello {}'.format(j))" ] }, { "cell_type": "markdown", "metadata": { "id": "MX1ikZcF3sXt" }, "source": [ "**break**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KwDQnkaL2ovl", "outputId": "f1314ef0-34a1-410c-9b6c-c5a575284749" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(10):\n", "\tif i==5:\n", "\t\tbreak\n", "\tprint(i)" ] }, { "cell_type": "markdown", "metadata": { "id": "Y0lnHZ3X34pi" }, "source": [ "**continue**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "EVg8DnH736nV", "outputId": "b796742e-684f-489b-8642-322387629787" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(10):\n", " if i==5:\n", " continue\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": { "id": "Dtg0WV-aSa1N" }, "source": [ "**Exercise 5.2a**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Ai-cZ6JaSfo6", "outputId": "361f06d5-1d4f-4a48-b9ea-63b35002cf5a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter a number: 3\n", "Factorial of 3 (3!) is 6\n" ] } ], "source": [ "num = int(input(\"Please enter a number: \"))\n", " \n", "fac = 1\n", "for i in range(1, num + 1):\n", " fac = fac * i\n", " \n", "print('Factorial of {0} ({0}!) is {1}'.format(num,fac))" ] }, { "cell_type": "markdown", "metadata": { "id": "6tLcF1WLVoR4" }, "source": [ "**Exercise 5.2b**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "__9gyMQVUKyK", "outputId": "9f35c565-eff1-4411-c069-e2aeff820de3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter a number: 2\n", "Factorial of 2 (2!) is 2\n" ] } ], "source": [ "num = int(input(\"Please enter a number: \"))\n", "\n", "i = num \n", "fac = 1\n", "\n", "while (i>0):\n", " fac = fac * i\n", " i = i - 1\n", " \n", "print('Factorial of {0} ({0}!) is {1}'.format(num,fac))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "1zBaMWJVVXQg" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "a4porREPYV8K" }, "source": [ "

LU6: Python Files Handling

" ] }, { "cell_type": "markdown", "metadata": { "id": "iYrM3L0gawWN" }, "source": [ "**Writing to a File**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rM_GXxaoaBwi", "outputId": "75da84d7-52da-4c12-96b9-842296122457" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\t1Hello World!\t2Hello World!\t3Hello World!\t4Hello World!\t5\n" ] } ], "source": [ "f = open('file.txt','w')\n", "for i in range(5):\n", " f.write('Hello World!\\t{}\\n'.format(i+1))\n", "\n", "f.close()\n", "#Reopen for reading to list contents of file\n", "f = open(\"file.txt\",\"r\")\n", "for line in f:\n", " print(line.strip())\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": { "id": "Pk7AFco3ivlI" }, "source": [ "**Appending to a File**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "wrlkjEuuippr" }, "outputs": [], "source": [ "f = open('file.txt','a')\n", "for i in range(5):\n", " f.write('Hello Singapore!\\t{}\\n'.format(i+1))\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": { "id": "TYtViQSsi4AH" }, "source": [ "**Reading from a File**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_JqlMEyhi0jx", "outputId": "1df75c9f-13dc-49b0-d262-c6b3d9893b5c" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\t1Hello World!\t2Hello World!\t3Hello World!\t4Hello World!\t5Hello Singapore!\t1\n", "Hello Singapore!\t2\n", "Hello Singapore!\t3\n", "Hello Singapore!\t4\n", "Hello Singapore!\t5\n" ] } ], "source": [ "f = open(\"file.txt\",\"r\")\n", "for line in f:\n", " print(line.strip())\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": { "id": "2d2y853274PJ" }, "source": [ "**Exercise 6.1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NEB28CHNlKRM", "outputId": "c8aaedc6-e0bf-460a-a330-f4abca014f1f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tell me and I forget. Teach me and I remember. Involve me and I learn. - Benjamin Franklin\n" ] } ], "source": [ "quote1 = \"Tell me and I forget. Teach me and I remember. Involve me and I learn. - Benjamin Franklin\\n\"\n", "f = open(\"quotes.txt\",\"w\")\n", "f.write(quote1)\n", "f.close()\n", "#added by DP\n", "f = open(\"quotes.txt\",\"r\")\n", "for line in f:\n", " #print(line)\n", " print(line.strip())\n", "f.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "iEOunthWISA8" }, "outputs": [], "source": [ "quote2 = \"In the end, it's not the years in your life that count. It's the life in your years. - Abraham Lincoln\\n\"\n", "f = open(\"quotes.txt\",\"a\")\n", "f.write(quote2)\n", "f.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_hC-Vu0YJ34f", "outputId": "d7735979-77c9-41c9-f31b-5308cba6a475" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tell me and I forget. Teach me and I remember. Involve me and I learn. - Benjamin Franklin\n", "In the end, it's not the years in your life that count. It's the life in your years. - Abraham Lincoln\n" ] } ], "source": [ "f = open(\"quotes.txt\",\"r\")\n", "for line in f:\n", " #print(line)\n", " print(line.strip())\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": { "id": "P7yxJ0UHKB2l" }, "source": [ "

LU7: Python Functions

" ] }, { "cell_type": "markdown", "metadata": { "id": "ALQRyc11NcYU" }, "source": [ "**A Simple Function**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "xpCkOV5LKHQ1" }, "outputs": [], "source": [ "def my_function1( x ):\n", " y = x ** 2\n", " return y" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "L8Vf3OSRNSi0", "outputId": "a27420b9-3dcd-4a97-b92a-2605f897c10a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "a = my_function1(2)\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "id": "pmU2y14fNfon" }, "source": [ "**Function Returning Multiple Values**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "xCNX8U-LNsBP" }, "outputs": [], "source": [ "def my_function2( x ):\n", " y = x ** 2\n", " z = x ** 0.5\n", " return y, z" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kSExsTiYNyOH", "outputId": "8f9a23bf-e87e-457a-97e8-37be98054764" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "1.4142135623730951\n" ] } ], "source": [ "a, b = my_function2(2)\n", "print(a)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": { "id": "yyDoRCOOO3e-" }, "source": [ "**Functions with Multiple Input Parameters**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "guYuu7OrO47l" }, "outputs": [], "source": [ "def my_function3( x, y ):\n", " z = x + y\n", " return z" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rUNM8I_XO8GC", "outputId": "04c9b06d-68e7-4c63-f6ce-20aa12add1e3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "a = my_function3(2, 3)\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "id": "O0BWBl1uPqeZ" }, "source": [ "**Exercise 7.1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4enzRC9dPsYI", "outputId": "034eb03e-d6a8-403c-da2b-c839ce151863" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello1\n" ] } ], "source": [ "def hello1():\n", " print('hello1')\n", "\n", "hello1()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 223 }, "id": "wvoSdL-0P-dy", "outputId": "95088719-095b-4007-e661-5d78d0845330" }, "outputs": [ { "ename": "NameError", "evalue": "ignored", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\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[0mhello2\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[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mhello2\u001b[0m\u001b[0;34m(\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 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'hello2'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'hello2' is not defined" ] } ], "source": [ "hello2()\n", "\n", "def hello2():\n", " print('hello2')" ] }, { "cell_type": "markdown", "metadata": { "id": "aBmF6ByAsT1-" }, "source": [ "**Exercise 7.2**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "xqXjximJsWGe" }, "outputs": [], "source": [ "def ticket(age, price):\n", " disc = 0\n", " if age>60 or age<7:\n", " disc = price * 0.1\n", " tax = (price-disc)*0.07\n", " return disc,tax" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YDlCIhsps7di", "outputId": "573d9017-d399-4b34-9563-3804a092ff90" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter age: 70\n", "Please enter ticket price: 100\n", "Discounted amount: $10.00\n", "Tax amount: $6.30\n", "Net price: $96.30\n" ] } ], "source": [ "age = int(input('Please enter age: '))\n", "price = float(input('Please enter ticket price: '))\n", "disc,tax = ticket(age,price)\n", "\n", "print('Discounted amount: ${:0.2f}'.format(disc))\n", "print('Tax amount: ${:0.2f}'.format(tax))\n", "print('Net price: ${:0.2f}'.format(price-disc+tax))" ] }, { "cell_type": "markdown", "metadata": { "id": "wxK01UdQWpjc" }, "source": [ "

LU8: Python Errors and Exceptions

" ] }, { "cell_type": "markdown", "metadata": { "id": "vi17sVws3P5H" }, "source": [ "**Syntax Error Example**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 134 }, "id": "tV15D23h3J_m", "outputId": "6ec7d145-458f-440b-ba41-cd71cf964ff0" }, "outputs": [ { "ename": "SyntaxError", "evalue": "ignored", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print('Hello Worls!'))\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unmatched ')'\n" ] } ], "source": [ "print('Hello Worls!'))" ] }, { "cell_type": "markdown", "metadata": { "id": "L63nYYQL3Ymo" }, "source": [ "**Exception Example**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 170 }, "id": "qvg_OXxr3T_I", "outputId": "7b48275a-b905-4158-c9e2-31812cb8498e" }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "ignored", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\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\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "print(0/0)" ] }, { "cell_type": "markdown", "metadata": { "id": "jPr2Stfcmk7B" }, "source": [ "**(No) Exceptions Handling Example**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "tARSISKomp0O" }, "outputs": [], "source": [ "num = int(input('Enter an integer number: '))\n", "print(\"You have entered \", num)" ] }, { "cell_type": "markdown", "metadata": { "id": "RIJcT-VP47Vz" }, "source": [ "**Exceptions Handling Example**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true, "base_uri": "https://localhost:8080/" }, "id": "n0ACS9Xo4umm", "outputId": "e8b0e50c-ea04-4ca2-86cd-224b5347debf" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter an integer number\n", "Thank you\n" ] } ], "source": [ "try:\n", "\tnum = int(input('Enter an integer number: '))\n", "except:\n", "\tprint(\"Please enter an integer number\")\n", "else:\n", " \tprint(\"You have entered \", num)\n", "finally:\n", " print(\"Thank you\")" ] }, { "cell_type": "markdown", "metadata": { "id": "HWpy06LX5AJf" }, "source": [ "**Exercise 8.1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9yjQ4R8G3iqY", "outputId": "a58e2d12-8a61-45dc-a796-71016ac1a1e2" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Please enter an integer number\n", "Please enter an integer number\n", "Enter an integer number: 4\n", "You have entered 4\n" ] } ], "source": [ "while True:\n", " try:\n", "\t num = int(input('Enter an integer number: '))\n", " except:\n", "\t print(\"Please enter an integer number\")\n", " else:\n", " print(\"You have entered \", num)\n", " break" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "qlH8hwrz6Mg4", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "ca5ed380-82fa-48eb-b817-6ceca8d20f00" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter an integer number or 0 to quit: 0\n", "You have entered 0\n", "Have a nice day \n" ] } ], "source": [ " #enhanced with loop that quits when enter 0\n", " while True:\n", " try:\n", "\t num = int(input('Enter an integer number or 0 to quit: '))\n", " except:\n", "\t print(\"Please enter an integer number\")\n", " else:\n", " print(\"You have entered \", num)\n", " # enter 0 to quit\n", " if num == 0:\n", " print(\"Have a nice day \")\n", " break" ] }, { "cell_type": "markdown", "metadata": { "id": "wYZVxK7MWxpV" }, "source": [ "

LU9: Python Modules

" ] }, { "cell_type": "markdown", "metadata": { "id": "f2hqtistCW8Q" }, "source": [ "**Exercise 9.1**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "QpFB0aJWzEs6" }, "outputs": [], "source": [ "a = [1, 2, 2, 3, 4, 5, 5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "PUqMHieHy28h" }, "outputs": [], "source": [ "import statistics\n", "print('Mean: {:0.3f}'.format(statistics.mean(a)))\n", "print('Median: {:0.3f}'.format(statistics.median(a)))\n", "print('StDev: {:0.3f}'.format(statistics.stdev(a)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "IihsrlEHCO6d" }, "outputs": [], "source": [ "import statistics as s\n", "print('Mean: {:0.3f}'.format(s.mean(a)))\n", "print('Median: {:0.3f}'.format(s.median(a)))\n", "print('StDev: {:0.3f}'.format(s.stdev(a)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5xSHqBQexhgf", "outputId": "611a3110-f9e3-4fce-9250-9c88897bd7af" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Mean: 3.143\n", "Median: 3.000\n", "StDev: 1.574\n" ] } ], "source": [ "from statistics import *\n", "print('Mean: {:0.3f}'.format(mean(a)))\n", "print('Median: {:0.3f}'.format(median(a)))\n", "print('StDev: {:0.3f}'.format(stdev(a)))" ] }, { "cell_type": "markdown", "metadata": { "id": "21b2_lG47JQK" }, "source": [ "**Matplotlib Demo**" ] }, { "cell_type": "markdown", "metadata": { "id": "aM8kpnPEaHpU" }, "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "WusMlvky7Ly6" }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "labels = ['G1', 'G2', 'G3', 'G4', 'G5']\n", "men_means = [20, 34, 30, 35, 27]\n", "women_means = [25, 32, 34, 20, 25]\n", "\n", "x = np.arange(len(labels)) # the label locations\n", "width = 0.35 # the width of the bars\n", "\n", "fig, ax = plt.subplots()\n", "rects1 = ax.bar(x - width/2, men_means, width, label='Men')\n", "rects2 = ax.bar(x + width/2, women_means, width, label='Women')\n", "# Add some text for labels, title and custom x-axis tick labels, etc.\n", "ax.set_ylabel('Scores')\n", "ax.set_title('Scores by group and gender')\n", "ax.set_xticks(x)\n", "ax.set_xticklabels(labels)\n", "ax.legend()\n", "# Only available in matplotlib ver 3.4+\n", "# ax.bar_label(rects1, padding=3)\n", "# ax.bar_label(rects2, padding=3)\n", "\n", "fig.tight_layout()\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "WRXAb1Wb7NcE", "outputId": "18a74f58-b1c9-4e17-f30b-c1b13671e366" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'3.2.2'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 8 } ], "source": [ "import matplotlib\n", "matplotlib.__version_" ] }, { "cell_type": "markdown", "metadata": { "id": "KSCan2XX9akl" }, "source": [ "**pandas Demo**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "3qqNhnXX8TMS" }, "outputs": [], "source": [ "import pandas as pd\n", "housing = pd.read_csv(\"sample_data/california_housing_test.csv\")\n", "housing" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "qfV0h530-SBO" }, "outputs": [], "source": [ "housing.head(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Rv_Vihji-gCC" }, "outputs": [], "source": [ "housing.tail(8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "lOf3dui__8ZU" }, "outputs": [], "source": [ "housing['housing_median_age'].max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "eFoYfeAWAA4j" }, "outputs": [], "source": [ "housing['housing_median_age'].min()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "nv-Xt5dg-lvC" }, "outputs": [], "source": [ "housing['housing_median_age'].mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "f6tEHDIe-tdG" }, "outputs": [], "source": [ "housing['housing_median_age'].median()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "eU3d6NA9_0e4" }, "outputs": [], "source": [ "housing['housing_median_age'].std()" ] }, { "cell_type": "markdown", "metadata": { "id": "m8m2rvkMAY4K" }, "source": [ "**NumPy Demo**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "kNo0BI3HALaS" }, "outputs": [], "source": [ "import numpy as np\n", "a = np.arange(15).reshape(3, 5)\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "u-TA04qhBq1v" }, "outputs": [], "source": [ "a.shape\n", "# shape gives you how many rows and columns" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "CUlErrANBv98" }, "outputs": [], "source": [ "b = np.arange(14,-1,-1).reshape(3, 5)\n", "# range func. list of numbers from 14 to -1, decrease by 1\n", "# shape into 3 x 5 matrix\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "_4PhwvvFCMCN" }, "outputs": [], "source": [ "c = a + b\n", "c" ] }, { "cell_type": "markdown", "metadata": { "id": "C4jYT64U4BuV" }, "source": [ "**Exercise 9.2 - Not Applicable**" ] }, { "cell_type": "markdown", "metadata": { "id": "8VBq5JTP4I27" }, "source": [ "**The End**" ] }, { "cell_type": "code", "source": [], "metadata": { "id": "MdtTs2nUWIxH" }, "execution_count": null, "outputs": [] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }