{ "cells": [ { "cell_type": "markdown", "id": "acb2b6ed", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", "
\n", " Run in Google Colab\n", " \n", " View on Github\n", " \n", " View raw on Github\n", "
" ] }, { "cell_type": "markdown", "id": "85aa8cf4-9e06-41af-8748-19fc1bac30af", "metadata": { "id": "85aa8cf4-9e06-41af-8748-19fc1bac30af" }, "source": [ "# Module 1: Optional Python review" ] }, { "cell_type": "markdown", "id": "eb2f7855-5cd8-4d47-9f4d-3dcf5f8be68c", "metadata": { "id": "eb2f7855-5cd8-4d47-9f4d-3dcf5f8be68c" }, "source": [ "Are you still new to Python? Or your python skill is a bit rusty? \n", "\n", "Then, feel free to use this to learn or refresh Python basics! You don't have to submit this notebook. It is just for your own reference." ] }, { "cell_type": "markdown", "id": "8b825ccf-4c8b-49a1-95fb-21df3d937eda", "metadata": { "id": "8b825ccf-4c8b-49a1-95fb-21df3d937eda" }, "source": [ "## 1. Built-in object types" ] }, { "cell_type": "markdown", "id": "80fc0133-bfbd-416f-8c58-9d2ae81f3a48", "metadata": { "id": "80fc0133-bfbd-416f-8c58-9d2ae81f3a48" }, "source": [ "### 1.1 Int and Float" ] }, { "cell_type": "code", "execution_count": null, "id": "a17cb32f-73ea-4aab-9ca4-f38c050f18cb", "metadata": { "id": "a17cb32f-73ea-4aab-9ca4-f38c050f18cb" }, "outputs": [], "source": [ "# Declaring an integer and a float variable\n", "x = 10 \n", "y = 3.0\n", "k = x + y + 100\n", "\n", "# Comments start with the hash symbol\n", "# y = 10\n", "\n", "# The value stored in the variable can be inspected by using print statement. \n", "# Type of a variable var can be checked by calling type(var) \n", "print(\"The value of x is\", x, \"and it is of type\", type(x))\n", "\n", "# f-strings can be used to write more elegant print statement. \n", "print(f\"The value of y is {y} and it is of type {type(y)}\")\n", "print(f\"The value of k is {k} and it is of type {type(k)}\")\n", "\n", "# casting int to float\n", "print(f\"x is of type {type(x)}\")\n", "x = float(x)\n", "print(f\"x is of type {type(x)} after casting\")" ] }, { "cell_type": "code", "execution_count": null, "id": "054512cf-e521-4160-92aa-a254e5ad01fe", "metadata": { "id": "054512cf-e521-4160-92aa-a254e5ad01fe" }, "outputs": [], "source": [ "# Arithmetic operators\n", "\n", "# Addition\n", "z = x + y\n", "print(f\"Adding x and y gives {z}\")\n", "\n", "# Subtraction\n", "z = x - y\n", "print(f\"Subtracting y from x gives {z}\")\n", "\n", "# Multiplication\n", "z = x * y\n", "print(f\"Multiplying x and y gives {z}\")\n", "\n", "# Division\n", "z = x / y \n", "print(f\"x divided by y gives {z}\") \n", "\n", "# Floor Division\n", "z = x // y \n", "print(f\"x divided by y gives {z} as quotient\") \n", "\n", "# Modulus Operator\n", "z = x % y \n", "print(f\"x divided by y gives {z} as reminder\")\n", "\n", "# Exponentiation\n", "z = x ** y\n", "print(f\"x raised to y gives {z}\") \n", "\n", "# self increment by 1\n", "x = x + 1\n", "# This is equivalent to x += 1\n", "print(f\"x + 1 gives {x}\")" ] }, { "cell_type": "markdown", "id": "c6b55c10-018d-4975-8169-1996c2500caa", "metadata": { "id": "c6b55c10-018d-4975-8169-1996c2500caa" }, "source": [ "### 1.2 Booleans and None" ] }, { "cell_type": "code", "execution_count": null, "id": "4dfac1df-4cbe-4cbf-91e9-e94717f90e5c", "metadata": { "id": "4dfac1df-4cbe-4cbf-91e9-e94717f90e5c" }, "outputs": [], "source": [ "# True and False are the key words that represent bool values in python\n", "a = True\n", "b = False\n", "\n", "print(f\"a is {a} and b is {b}\")\n", "print(f\"Type of variable a and b is {type(a)}\")\n", "\n", "# None in python represents the absence of something; similar to null value\n", "c = None \n", "print(f\"c is {c} and is of type {type(c)}\")\n", "\n", "# Any non-zero integer value is true and zero is false.\n", "# Also anything with a non-zero length is true and empty sequences are false." ] }, { "cell_type": "code", "execution_count": null, "id": "7139a6a2-7698-433c-8562-b8d38f0e03d0", "metadata": { "id": "7139a6a2-7698-433c-8562-b8d38f0e03d0" }, "outputs": [], "source": [ "# logical operators \n", "\n", "# and, or and not operate on bool variables\n", "# OR operator: Gives True when either of the expressions evaluates to True\n", "# expr1 or expr2\n", "print(f\"a or b is {a or b}\")\n", "print(f\"a or a is {a or a}\")\n", "print(f\"b or b is {b or b}\")\n", "\n", "# AND operator: Gives True when both the expressions evaluates to True\n", "# expr1 and expr2\n", "print(f\"a and b is {a and b}\")\n", "print(f\"a and a is {a and a}\")\n", "print(f\"b and b is {b and b}\")\n", "\n", "# NOT operator: negates a bool\n", "# not expr1\n", "print(f\"Not of a is {not a}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "167b836b-e975-402f-be4e-c638a6bb9063", "metadata": { "id": "167b836b-e975-402f-be4e-c638a6bb9063" }, "outputs": [], "source": [ "# comparison operators\n", "\n", "x = 10\n", "y = 3.0\n", "z = 5\n", "\n", "# greater that, less than, greater than equal to and lesser than equal to\n", "x > y\n", "x >= y\n", "x < y\n", "x <= y\n", "\n", "# equals and not equals\n", "x == y\n", "x != y\n", "\n", "# Chained Expressions \n", "x > y > z \n", "(x > y) or (x > z)" ] }, { "cell_type": "markdown", "id": "8f9abbdd-3fcb-4c42-8d28-8b3a5b342671", "metadata": { "id": "8f9abbdd-3fcb-4c42-8d28-8b3a5b342671" }, "source": [ "### 1.3 Strings" ] }, { "cell_type": "code", "execution_count": null, "id": "9b65b9b7-f12c-4e4e-be55-27bfd8b8dd4b", "metadata": { "id": "9b65b9b7-f12c-4e4e-be55-27bfd8b8dd4b" }, "outputs": [], "source": [ "# strings are represented using single or double quotes\n", "first_name = \"Adam\" \n", "last_name = 'Eve'\n", "\n", "# \\ is used to escape characters\n", "middle_name = 'zero\\'s'\n", "\n", "# string concatenation\n", "full_name = first_name +' ' + middle_name + ' ' + last_name\n", "\n", "print(f\"Full name is {full_name}\")\n", "print(f\"Full name is of type {type(full_name)}\")\n", "\n", "# strings can be indexed and sliced similar to lists and tuples. \n", "# List indexing is discussed in the list section" ] }, { "cell_type": "code", "execution_count": null, "id": "20e2aeb1-2019-4c1b-8813-f76fa3ebe69f", "metadata": { "id": "20e2aeb1-2019-4c1b-8813-f76fa3ebe69f" }, "outputs": [], "source": [ "# casting str to int \n", "total = int('1') + int('2')\n", "print(f\"The value of total is {total} and it is of type {type(total)}\")\n", "\n", "# casting int to str\n", " \n", "total = str(1) + str(2)\n", "print(f\"The value of total is {total} and it is of type {type(total)}\")" ] }, { "cell_type": "markdown", "id": "dd04a861-34f0-4120-8204-fdf0a20cb83a", "metadata": { "id": "dd04a861-34f0-4120-8204-fdf0a20cb83a" }, "source": [ "### 1.4 Lists and Tuples" ] }, { "cell_type": "code", "execution_count": null, "id": "1fc59fad-1e15-4e75-bd58-586198a2f2e2", "metadata": { "id": "1fc59fad-1e15-4e75-bd58-586198a2f2e2" }, "outputs": [], "source": [ "# Arrays are implemented as lists in python\n", "\n", "# creating empty list\n", "names = []\n", "names = list()\n", "\n", "# list of strings\n", "names = ['Zach', 'Jay']\n", "print(names)\n", "\n", "# list of intergers\n", "nums = [1, 2, 3, 4, 5]\n", "print(nums)\n", "\n", "# list of different data types\n", "l = ['Zach', 1, True, None]\n", "print(l)\n", "\n", "# list of lists\n", "ll = [[1, 3], [2, 3], [3, 4]]\n", "\n", "# finding the length of list\n", "length = len(l)\n", "print(length)" ] }, { "cell_type": "code", "execution_count": null, "id": "2f9fa813-f922-4b1f-9447-6d3315ad893d", "metadata": { "id": "2f9fa813-f922-4b1f-9447-6d3315ad893d" }, "outputs": [], "source": [ "# Lists are mutable\n", "\n", "names = names + ['Ravi']\n", "names.append('Richard')\n", "names.extend(['Abi', 'Kevin'])\n", "print(names)" ] }, { "cell_type": "code", "execution_count": null, "id": "61266e91-5ce6-404c-abd2-4028a9538790", "metadata": { "id": "61266e91-5ce6-404c-abd2-4028a9538790" }, "outputs": [], "source": [ "# List indexing and slicing\n", "# an element or a subset of list can be accessed using element's index or slice of indices\n", "# same notation applies for strings but at char level\n", "\n", "# some_list[index]\n", "# some_list[start_index: end_index(not included)]\n", "\n", "numbers = [0, 1, 2, 3, 4, 5, 6]\n", "\n", "# indices start from 0 in python\n", "print(f'The first element in numbers is {numbers[0]}')\n", "print(f'The third element in numbers is {numbers[2]}')\n", "\n", "print(f'Elements from 1st to 5th index are {numbers[1:6]}')\n", "print(f'Elements from start to 5th index are {numbers[:6]}')\n", "print(f'Elements from 4th index to end are {numbers[4:]}')\n", "\n", "print(f'Last Element is {numbers[-1]}')\n", "print(f'Last four element are {numbers[-4:]}')\n", "\n", "# changing 1st element in the numbers list\n", "numbers[0] = 100\n", "print(numbers)\n", "\n", "# changing first 3 numbers\n", "numbers[0: 3] = [100, 200, 300]\n", "print(numbers)" ] }, { "cell_type": "code", "execution_count": null, "id": "22a085ea-e9ae-42ba-b675-7a9ba4c3e799", "metadata": { "id": "22a085ea-e9ae-42ba-b675-7a9ba4c3e799" }, "outputs": [], "source": [ "# Tuples are immutable lists. They are created using () instead of [].\n", "\n", "names = tuple()\n", "names = ('Zach', 'Jay') \n", "print(names[0])\n", "\n", "# trying to alter the tuple gives an error\n", "names[0] = 'Richard'\n", "\n", "# similar to tuples, strings are also immutable" ] }, { "cell_type": "markdown", "id": "fae7231c-2a39-423d-a778-9641597a7dd6", "metadata": { "id": "fae7231c-2a39-423d-a778-9641597a7dd6" }, "source": [ "### 1.5 Dictionary" ] }, { "cell_type": "code", "execution_count": null, "id": "6eeecc35-b7e1-4975-b2a8-27f7c13d1041", "metadata": { "id": "6eeecc35-b7e1-4975-b2a8-27f7c13d1041" }, "outputs": [], "source": [ "# hash maps in python are called Dictionaries\n", "# dict{key: value}\n", "\n", "# Empty dictionary\n", "phonebook = dict()\n", "\n", "# contruction dict using sequences of key-value pairs\n", "dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])\n", "\n", "# Dictionary with one item\n", "phonebook = {'jack': 4098}\n", "\n", "# Add another item\n", "phonebook['guido'] = 4127\n", "\n", "print(phonebook)\n", "print(phonebook['jack'])\n", "print(phonebook.items())\n", "print(phonebook.keys())\n", "print(phonebook.values())\n", "\n", "print('jack' in phonebook)\n", "print('Kevin' in phonebook)\n", "\n", "# Delete an item\n", "del phonebook['jack'] \n", "print(phonebook)" ] }, { "cell_type": "markdown", "id": "4cff6196-180a-48fb-a7fa-8b8ff9525c2d", "metadata": { "id": "4cff6196-180a-48fb-a7fa-8b8ff9525c2d" }, "source": [ "## 2. Flow control statements" ] }, { "cell_type": "markdown", "id": "b29d0e9b-8728-41a5-a349-8a7a05239433", "metadata": { "id": "b29d0e9b-8728-41a5-a349-8a7a05239433" }, "source": [ "### 2.1 if... elif... else..." ] }, { "cell_type": "code", "execution_count": null, "id": "8c191344-37e8-4b25-9510-3a95c36e48ad", "metadata": { "id": "8c191344-37e8-4b25-9510-3a95c36e48ad" }, "outputs": [], "source": [ "# if expr1:\n", "# code1\n", "# elif expr2:\n", "# code2\n", "# .\n", "# .\n", "# .\n", "# .\n", "# else:\n", "# code_n\n", "\n", "# code1 is executed if expr1 is evaluated to true. Else it moves to expr2 and checks for true \n", "# condition and moves to the next if not true. \n", "# Finally if all the excpression's are false, code_n is executed\n", "\n", "x = int(input(\"Please enter an integer: \"))\n", "\n", "if x < 0:\n", " x = 0\n", " print('Negative changed to zero')\n", "elif x == 0:\n", " print('Zero')\n", "elif x == 1:\n", " print('Single')\n", "else:\n", " print('More')" ] }, { "cell_type": "markdown", "id": "be364b86-a255-4c42-a859-3af323450725", "metadata": { "id": "be364b86-a255-4c42-a859-3af323450725" }, "source": [ "## 3. Loops" ] }, { "cell_type": "markdown", "id": "78c8e0c4-e26d-46c4-b0f0-00666445dba9", "metadata": { "id": "78c8e0c4-e26d-46c4-b0f0-00666445dba9" }, "source": [ "### 3.1 For loops" ] }, { "cell_type": "code", "execution_count": null, "id": "869bf8dd-166d-4c5e-88d4-7fc2162362bb", "metadata": { "id": "869bf8dd-166d-4c5e-88d4-7fc2162362bb" }, "outputs": [], "source": [ "# for loop is used to iter over any iterable object\n", "\n", "# iterating over list\n", "for name in ['Steve', 'Jill', 'Venus']:\n", " print(name)" ] }, { "cell_type": "code", "execution_count": null, "id": "a9e4e188-3b50-4227-a429-e8fec9d9f671", "metadata": { "id": "a9e4e188-3b50-4227-a429-e8fec9d9f671" }, "outputs": [], "source": [ "# iterating over string\n", "for char in \"Hellooooo\":\n", " print(char)" ] }, { "cell_type": "code", "execution_count": null, "id": "aec1440d-9740-4470-a12a-197d6e558185", "metadata": { "id": "aec1440d-9740-4470-a12a-197d6e558185" }, "outputs": [], "source": [ "# iterating over dict keys\n", "phone_nos = {\"Henry\": 6091237458,\n", " \"James\": 1234556789, \n", " \"Larry\": 5698327549, \n", " \"Rocky\": 8593876589}\n", "for name, no in phone_nos.items():\n", " print(name, no)" ] }, { "cell_type": "code", "execution_count": null, "id": "f7e8497e-b50c-44d0-8066-392147cf0a6e", "metadata": { "id": "f7e8497e-b50c-44d0-8066-392147cf0a6e" }, "outputs": [], "source": [ "# To iterate over a sequence of numbers we use range() function. \n", "# range(start=0, end, step=1)\n", "for i in range(2, 20, 2):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "id": "8ea485ec-1828-4c7d-b975-1126af17fd1c", "metadata": { "id": "8ea485ec-1828-4c7d-b975-1126af17fd1c" }, "outputs": [], "source": [ "# using len of list/tuple in range\n", "names = ['Steve', 'Rock', 'Harry']\n", "for i in range(len(names)):\n", " print(names[i])" ] }, { "cell_type": "markdown", "id": "b16bb972-463a-45bc-8c62-2ea262a09354", "metadata": { "id": "b16bb972-463a-45bc-8c62-2ea262a09354" }, "source": [ "#### 3.2 While Loop" ] }, { "cell_type": "code", "execution_count": null, "id": "b1f13c0b-a977-499e-be44-73872e7f0076", "metadata": { "id": "b1f13c0b-a977-499e-be44-73872e7f0076" }, "outputs": [], "source": [ "# While loop executes as long as the condition remains true.\n", "# while cond1:\n", "# pass\n", "\n", "i = 0\n", "while i < 10:\n", " print(i)\n", " i += 1" ] }, { "cell_type": "code", "execution_count": null, "id": "a690c2ca-e800-46c2-a268-e55d69a057a3", "metadata": { "id": "a690c2ca-e800-46c2-a268-e55d69a057a3" }, "outputs": [], "source": [ "# Forever loop: The below code runs for ever\n", "\n", "# while True:\n", "# print('Forever...')" ] }, { "cell_type": "markdown", "id": "46cf3126-b99c-4d7f-9616-529ee9589ee5", "metadata": { "id": "46cf3126-b99c-4d7f-9616-529ee9589ee5" }, "source": [ "#### 3.3 Break, Continue and Pass statements" ] }, { "cell_type": "code", "execution_count": null, "id": "40440c9e-df88-41d6-b325-0b3448810c2c", "metadata": { "id": "40440c9e-df88-41d6-b325-0b3448810c2c" }, "outputs": [], "source": [ "# break statement breaks out of the the loop\n", "while True:\n", " print('We’re stuck in a loop...')\n", " break # Break out of the while loop\n", "print(\"not!\")" ] }, { "cell_type": "code", "execution_count": null, "id": "6498cf96-7dfa-4fcf-84ec-8d7bf0d64509", "metadata": { "id": "6498cf96-7dfa-4fcf-84ec-8d7bf0d64509" }, "outputs": [], "source": [ "# continue statement skips a loop\n", "for i in range(5):\n", " continue \n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "id": "019b483a-5149-4a0c-ae3f-762d34203b9d", "metadata": { "id": "019b483a-5149-4a0c-ae3f-762d34203b9d" }, "outputs": [], "source": [ "# pass statement does nothing and is used as a placeholder\n", "for i in range(10):\n", " pass" ] }, { "cell_type": "markdown", "id": "ba96b02d-c12d-48ff-ab44-8e26ffa6e3ab", "metadata": { "id": "ba96b02d-c12d-48ff-ab44-8e26ffa6e3ab" }, "source": [ "## 4. Importing Modules" ] }, { "cell_type": "code", "execution_count": null, "id": "11630e54-9cf4-4495-b9a7-cb5ed6975372", "metadata": { "id": "11630e54-9cf4-4495-b9a7-cb5ed6975372" }, "outputs": [], "source": [ "# importing modules using import statement\n", "import math\n", "\n", "# Import under an alias (avoid this pattern except with the most \n", "# common modules like pandas, numpy, etc.)\n", "import math as m\n", "\n", "# Access components with pkg.fn\n", "m.pow(2, 3) \n", "\n", "# Import specific submodules/functions (not usually recommended \n", "# because it can be confusing to know where a function comes from)\n", "from math import pow\n", "pow(2, 3)" ] }, { "cell_type": "markdown", "id": "2bbe5ca7-fd34-4c9c-99c1-fb1ce0554916", "metadata": { "id": "2bbe5ca7-fd34-4c9c-99c1-fb1ce0554916" }, "source": [ "## 5. Defining Functions" ] }, { "cell_type": "code", "execution_count": null, "id": "60bc088b-5032-45dd-affc-b3c705778e3f", "metadata": { "id": "60bc088b-5032-45dd-affc-b3c705778e3f" }, "outputs": [], "source": [ "# Functions in python are defined using key word \"def\"\n", "\n", "# simple function to print greetings `greet_word` is an optional argument.\n", "def greet(name, greet_word='Hello'):\n", " print(f\"{greet_word} {name}. How are you doing?\")\n", "\n", "# here greet has 'Hello' as default argument for greet_word\n", "print(greet('James'))\n", "print(greet(\"Steven\", greet_word=\"Howdy\"))\n", "\n", "# Observe that the function by default returns None" ] }, { "cell_type": "code", "execution_count": null, "id": "aaa9caf7-1fb5-4da8-ab91-6c8747067fae", "metadata": { "id": "aaa9caf7-1fb5-4da8-ab91-6c8747067fae" }, "outputs": [], "source": [ "# Function to print nth fibonacci number\n", "def fib(n):\n", " if n <= 1:\n", " return 1\n", " else:\n", " return fib(n-1)+fib(n-2)\n", "n = 10\n", "print(f\"{n}th fibonacci number is {fib(n)}\")" ] }, { "cell_type": "markdown", "id": "6e7a9304-8098-481d-ba01-05833f065e83", "metadata": { "id": "6e7a9304-8098-481d-ba01-05833f065e83" }, "source": [ "## Questions\n", "\n", "For each of the following question, first think about the result without running the code. Then test it by running the code. Reach out if you don't understand why!" ] }, { "cell_type": "markdown", "id": "4211cdd2-424b-44f1-a878-852a44b002d5", "metadata": { "id": "4211cdd2-424b-44f1-a878-852a44b002d5" }, "source": [ "### What's the output?\n", "\n", "```\n", "def func(a):\n", " a = a + 2\n", " a = a * 2\n", " return a\n", " \n", "print(func(2))\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "HakY-iPu8nTz", "metadata": { "id": "HakY-iPu8nTz" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3861eb4c-8327-480e-8a5a-fd952a1b1264", "metadata": { "id": "3861eb4c-8327-480e-8a5a-fd952a1b1264" }, "source": [ "### True? False? Why?\n", "\n", "```\n", "0.1 + 0.2 == 0.3\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "bSnmfSB59Sja", "metadata": { "id": "bSnmfSB59Sja" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "39af73d3-7a93-4d25-828f-1a5644bcdaf7", "metadata": { "id": "39af73d3-7a93-4d25-828f-1a5644bcdaf7" }, "source": [ "### 3. What is `list_1` and `list_2` and why?\n", "\n", "\n", "```\n", "list_1 = [1,2,3]\n", "list_2 = list_1\n", "list_1.append(4)\n", "list_2 += [5]\n", "list_2 = list_2 + [10]\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "id": "5QbzAozH9pZr", "metadata": { "id": "5QbzAozH9pZr" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "4608b477-13b1-4460-aeef-231831c200c6", "metadata": { "id": "4608b477-13b1-4460-aeef-231831c200c6" }, "source": [ "### What's the output?\n", "\n", "```\n", "l = [i**2 for i in range(10)]\n", "l[-4:2:-3]\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "id": "fp4ETHH976Fx", "metadata": { "id": "fp4ETHH976Fx" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "c1ee0055-b213-4cb0-92f1-965bc292f52d", "metadata": { "id": "c1ee0055-b213-4cb0-92f1-965bc292f52d" }, "source": [ "### What does the code do? If the ordering doesn't matter, how can it be simplified?\n", "\n", "```\n", "def func1(lst):\n", " a = []\n", " for i in lst:\n", " if i not in a:\n", " a.append(i)\n", " return a\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "id": "cUSmxbtN7urO", "metadata": { "id": "cUSmxbtN7urO" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "6d3db63e-f7da-4409-baf5-2a333f1c81a4", "metadata": { "id": "6d3db63e-f7da-4409-baf5-2a333f1c81a4" }, "source": [ "### What would be the output? \n", "\n", "```\n", "val = [0, 10, 15, 20]\n", "data = 15\n", "try:\n", " data = data/val[0]\n", "except ZeroDivisionError:\n", " print(\"zero division error - 1\")\n", "except:\n", " print(\"zero division error - 2\")\n", "finally:\n", " print(\"zero division error - 3\")\n", "\n", "val = [0, 10, 15, 20]\n", "data = 15\n", "try:\n", " data = data/val[4]\n", "except ZeroDivisionError:\n", " print(\"zero division error - 1\")\n", "except:\n", " print(\"zero division error - 2\")\n", "finally:\n", " print(\"zero division error - 3\")\n", "\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "id": "UyCQ3cgt-37V", "metadata": { "id": "UyCQ3cgt-37V" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "e3628dd2-06b8-4374-b707-255bf99b70e8", "metadata": { "id": "e3628dd2-06b8-4374-b707-255bf99b70e8" }, "source": [ "### What does the code do?\n", "\n", "```\n", "def func(s):\n", " d = {}\n", " for c in s:\n", " if c in d:\n", " d[n] += 1\n", " else:\n", " d[n] = 1\n", " return d\n", "```\n", "\n", "(Btw, the same operation can be done by simply running `Counter(s)` by using `Counter` data structure in the `collections` module.)" ] }, { "cell_type": "code", "execution_count": null, "id": "TmMjgtOg_Vmv", "metadata": { "id": "TmMjgtOg_Vmv" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "44ba616d-cb20-4804-b58d-1bf55325a337", "metadata": { "id": "44ba616d-cb20-4804-b58d-1bf55325a337" }, "source": [ "### What's the output?\n", "\n", "```\n", "def func(l):\n", " l.append(10)\n", " return l\n", "\n", "a = [1,2,3]\n", "b = func(a)\n", "a == b\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "XELKg-ljAApb", "metadata": { "id": "XELKg-ljAApb" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "a1f73a0a-9f34-4071-bc50-129a3f13934a", "metadata": { "id": "a1f73a0a-9f34-4071-bc50-129a3f13934a" }, "source": [ "### What's happening to `a` in each step? Why?\n", "\n", "```\n", "# step 1\n", "a = [ [ ] ] * 5\n", "# step 2\n", "a[0].append(10)\n", "# step 3\n", "a[1].append(20)\n", "# step 4\n", "a.append(30)\n", "\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "l3ALe2iZCXRI", "metadata": { "id": "l3ALe2iZCXRI" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "2b12f89c-705f-4978-82f2-2e101d829d7a", "metadata": { "id": "2b12f89c-705f-4978-82f2-2e101d829d7a" }, "source": [ "### What's the output?\n", "\n", "```\n", "L = list('abcdefghijk')\n", "L[1] = L[4] = 'x'\n", "L[3] = L[-3]\n", "print(L)\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "id": "BRiSJNm7DDb-", "metadata": { "id": "BRiSJNm7DDb-" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "6646badf-698f-4a4e-8cd5-0445019a0c68", "metadata": { "id": "6646badf-698f-4a4e-8cd5-0445019a0c68" }, "source": [ "### What's the output?\n", "\n", "```\n", "y = 8\n", "z = lambda x : x * y\n", "print (z(6))\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1JxYk-_tDNdx", "metadata": { "id": "1JxYk-_tDNdx" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] }, { "cell_type": "markdown", "id": "1f5b4f99-97cf-43a6-98fa-9ecd8f7b065c", "metadata": { "id": "1f5b4f99-97cf-43a6-98fa-9ecd8f7b065c" }, "source": [ "### What's the output?\n", "\n", "```\n", "count = 1 \n", "\n", "def func(count):\n", " for i in (1, 2, 3): \n", " count += 1\n", "func(count = 10)\n", "count += 5\n", "print(count)\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "id": "VBW0j2e3DVFU", "metadata": { "id": "VBW0j2e3DVFU" }, "outputs": [], "source": [ "# YOUR SOLUTION HERE" ] } ], "metadata": { "colab": { "provenance": [ { "file_id": "1SScsxYIDu6SZqU4TbSCTndwr9ymrA1F4", "timestamp": 1644168134630 } ] }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.8" }, "vscode": { "interpreter": { "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e" } } }, "nbformat": 4, "nbformat_minor": 5 }