diff --git a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/copy_lec_06-checkpoint.ipynb b/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/copy_lec_06-checkpoint.ipynb deleted file mode 100644 index 84c0401c93a2541feee964b0e24f978ad44a1f8a..0000000000000000000000000000000000000000 --- a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/copy_lec_06-checkpoint.ipynb +++ /dev/null @@ -1,917 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Creating Functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Review - pre-installed modules\n", - "\n", - "### Two ways of importing functions from a module\n", - "1. import \\<module\\>\n", - " - requires you to use attribute operator: `.`\n", - " - \\<module\\>.\\<function\\>\n", - "2. from \\<module\\> import \\<function\\>\n", - " - function can be called just with its name\n", - " \n", - "Let's learn about time module" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# Add all your import statements to this cell\n", - "\n", - "# TODO: import time module using import style of import\n", - "import time\n", - "\n", - "# TODO: use from style of import to import log10 function from math module\n", - "from math import log10\n", - "\n", - "# Bad style to import everything from a module\n", - "# Not recommended to do\n", - "#from math import *\n", - "# If you want to import everything, you need to \n", - "# follow import style of import\n", - "import math" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "time module time function shows the current time in seconds since epoch.\n", - "\n", - "What is epoch? epoch is January 1, 1970. **FUN FACT:** epoch is considered beginning of time for computers." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1644107903.650124\n", - "1644107909.54414\n", - "5.8940160274505615\n" - ] - } - ], - "source": [ - "start_time = time.time()\n", - "x = 2 ** 1000000000 # some large computation\n", - "end_time = time.time()\n", - "\n", - "# TODO: change the line below to compute difference\n", - "difference = (end_time - start_time)\n", - "\n", - "# TODO: add a separator of '\\n'\n", - "print(start_time, end_time, difference, sep = \"\\n\") \n", - "\n", - "# TODO: discuss - how can you use time() function to time your project code?" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.0\n" - ] - } - ], - "source": [ - "# TODO: call log10 function to determine log base 10 of 1000\n", - "print(log10(1000))\n", - "\n", - "# Recall that you cannot use math. when you use from style of import\n", - "# print(math.log10(1000)) #doesn't work" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3.141592653589793" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# TODO: can you access pi variable inside math module?\n", - "#pi # TODO: discuss why this didn't work\n", - "\n", - "# TODO: go back to the import cell and import math \n", - "# TODO: fix line 2, so that you are now able to access pi inside math module\n", - "math.pi" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Learning Objectives\n", - "\n", - "- Explain the syntax of a function header:\n", - " - def, ( ), :, tabbing, return\n", - "- Write a function with:\n", - " - correct header and indentation\n", - " - a return value (fruitful function) or without (void function)\n", - " - parameters that have default values\n", - "- Write a function knowing the difference in outcomes of print and return statements\n", - "- Explain how positional, keyword, and default arguments are copied into parameters\n", - "- Make function calls using positional, keyword, and default arguments and determine the result.\n", - "- Trace function invocations to determine control flow" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 1: Cube of a number\n", - "- Input: number (to be cubed)\n", - "- Output: cubed number" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: Let's define the cube function\n", - "def cube(side):\n", - " return side ** 3" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "125\n", - "512\n" - ] - } - ], - "source": [ - "# Let's call cube function\n", - "print(cube(5))\n", - "# TODO: discuss what is different about the below line of code\n", - "print(cube(cube(2)))" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "37\n", - "37\n" - ] - } - ], - "source": [ - "# TODO: compute 4 cube + -3 cube\n", - "# version 1\n", - "print(cube(4) + cube(-3))\n", - "\n", - "# version 2\n", - "cube_of_4 = cube(4)\n", - "cube_of_minus_3 = cube(-3)\n", - "print(cube_of_4 + cube_of_minus_3)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "-1728\n" - ] - } - ], - "source": [ - "# TODO: compute 4 cube * -3 cube\n", - "# Now which one of the above two versions is better?\n", - "\n", - "print(cube_of_4 * cube_of_minus_3)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "# Whenever you think you are going to reuse a function call's\n", - "# output, save it in a variable\n", - "\n", - "# Rookie programmer mistake: calling the same function with the \n", - "# same arguments will always give the same return value\n", - "# Running the same function call twice takes twice the time" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`return` vs `print`\n", - "- `return` enables us to send output from a function to the calling place\n", - " - default `return` value is `None`\n", - " - that means, when you don't have a `return` statement, `None` will be returned\n", - "- `print` function simply displays / prints something\n", - " - it cannot enable you to produce output from a function" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: Go back to cube function defintion, change return to print\n", - "# Run the above function call cells to see what happens" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Tracing function invocations\n", - "- PythonTutor is a great tool to learn control flow\n", - "- Let's use PythonTutor to trace cube function invocation\n", - "- TODO: Copy-paste cube function defintion into PythonTutor (course website > tools > PythonTutor)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 2: is_between(lower, num, upper)\n", - "- Purpose: check whether number is within the range of lower and upper (inclusive)\n", - "- Input: lower bound, number, upper bound\n", - "- Output: boolean value (`True` or `False`)\n", - "- Keyword: `pass`:\n", - " - placeholder statement\n", - " - you cannot run a cell with an empty function definition" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n", - "True\n" - ] - } - ], - "source": [ - "def is_between(lower, num, upper):\n", - " #pass # TODO: remove this and try to run this cell\n", - " # version 1\n", - " return lower <= num <= upper\n", - " # version 2\n", - " #return lower <= num and num <= upper\n", - " \n", - "# you can call a function in the same cell that you defined it\n", - "print(is_between(3, 7, 21))\n", - "print(is_between(2, 14, 5))\n", - "print(is_between(100, cube(5), 200))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Types of arguments\n", - "- positional\n", - "- keyword\n", - "- default\n", - "\n", - "Python fills arguments in this order: positional, keyword, " - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 100\n", - "y = 10\n", - "z = 5\n", - "x = 100\n", - "y = 10\n", - "z = 5\n" - ] - }, - { - "data": { - "text/plain": [ - "115" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def add3(x, y = 100, z = 100): \n", - " \"\"\"adds three numbers\"\"\" #documentation string\n", - " print (\"x = \" + str(x))\n", - " print (\"y = \" + str(y))\n", - " print (\"z = \" + str(z))\n", - " return x + y + z\n", - "\n", - "sum = add3(100, 10, 5) \n", - "# TODO: 1. sum is a bad variable, discuss: why. What would be a better variable name?\n", - "# TODO: 2. what type of arguments are 100, 10, and 5? Positional\n", - "\n", - "total = add3(100, 10, 5)\n", - "total" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1\n", - "y = 5\n", - "z = 2\n", - "8\n" - ] - } - ], - "source": [ - "print(add3(x = 1, z = 2, y = 5)) #TODO: what type of arguments are these? Keyword" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 5\n", - "y = 6\n", - "z = 100\n" - ] - }, - { - "data": { - "text/plain": [ - "111" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add3(5, 6) # TODO: what type of argument gets filled for the parameter z? Default value" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Positional arguments need to be specified before keyword arguments." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "positional argument follows keyword argument (1597961864.py, line 2)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_49460/1597961864.py\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m add3(z = 5, 2, 7)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" - ] - } - ], - "source": [ - "# Incorrect function call\n", - "add3(z = 5, 2, 7) \n", - "# TODO: what category of error is this?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Similarly, parameters with default values should be defined after parameters without default values." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "non-default argument follows default argument (1367324985.py, line 2)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_49460/1367324985.py\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m def add3_bad_version(x = 10, y, z):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" - ] - } - ], - "source": [ - "# Incorrect function definition\n", - "def add3_bad_version(x = 10, y, z): \n", - " \"\"\"adds three numbers\"\"\" #documentation string\n", - " return x + y + z" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python expects exactly same number of arguments as parameters." - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "add3() got multiple values for argument 'x'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_49460/100225854.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Incorrect function call\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0madd3\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m4\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 3\u001b[0m \u001b[0;31m# TODO: what category of error is this?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mTypeError\u001b[0m: add3() got multiple values for argument 'x'" - ] - } - ], - "source": [ - "# Incorrect function call\n", - "add3(5, 3, 10, x = 4)\n", - "# TODO: what category of error is this?" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "add3() missing 1 required positional argument: 'x'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_49460/2441706151.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# TODO: will this function call work?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0madd3\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\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: add3() missing 1 required positional argument: 'x'" - ] - } - ], - "source": [ - "# TODO: will this function call work?\n", - "add3(y = 5, z = 10)" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "add3() missing 1 required positional argument: 'x'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_49460/2146538074.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# TODO: will this function call work?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0madd3\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[0;31mTypeError\u001b[0m: add3() missing 1 required positional argument: 'x'" - ] - } - ], - "source": [ - "# TODO: will this function call work?\n", - "add3()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 3: Generate a height x width grid\n", - "- Input: width, height, grid symbol, title of the grid\n", - "- Output: string containing title, a newline, and the grid\n", - "- Pseudocode steps:\n", - " 1. Generate a single row of symb (width dimension). What string operator do you need?\n", - " 2. Capture single row into a variable\n", - " 3. Add newline to single row variable.\n", - " 4. Generate multiple rows (height dimension). What string operator do you need?\n", - " 5. Generate the output string to be returned by adding title with a newline with the output from step 4." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: how many parameters have default values in the below function?\n", - "def get_grid(width, height, symb = '#', title = 'My Grid:'):\n", - " row = symb * width\n", - " grid = (row + '\\n') * height\n", - " return title + '\\n' + grid" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "My Grid:\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "\n" - ] - } - ], - "source": [ - "# TODO: generate various sized grids, by exploring\n", - "# three types of arguments\n", - "# Here is one example\n", - "print(get_grid(10, 8))" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: use PythonTutor to trace get_grid function call" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "My Grid:\n", - "@@@@@\n", - "@@@@@\n", - "@@@@@\n", - "@@@@@\n", - "\n" - ] - } - ], - "source": [ - "print(get_grid(5, 4, symb = \"@\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Some Grid:\n", - "..\n", - "..\n", - "..\n", - "\n" - ] - } - ], - "source": [ - "print(get_grid(2, 3, \".\", \"Some Grid:\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Some other grid:\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "\n" - ] - } - ], - "source": [ - "print(get_grid(width = 4, height = 7, symb = \"^\", title = \"Some other grid:\"))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Revisiting `print` function\n", - "- Let's look at `help(print)` to learn about print's parameters\n", - " - Default value for `sep` is space, that is: \" \"\n", - " - Default value for `end` is newline, that is: \"\\n\"" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function print in module builtins:\n", - "\n", - "print(...)\n", - " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", - " \n", - " Prints the values to a stream, or to sys.stdout by default.\n", - " Optional keyword arguments:\n", - " file: a file-like object (stream); defaults to the current sys.stdout.\n", - " sep: string inserted between values, default a space.\n", - " end: string appended after the last value, default a newline.\n", - " flush: whether to forcibly flush the stream.\n", - "\n" - ] - } - ], - "source": [ - "help(print)" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7 True 34....\n", - "7\tTrue\t34\n" - ] - } - ], - "source": [ - "# TODO: predict output, then run to validate your prediction\n", - "print(3 + 4, 3 < 4, \"3\" + \"4\", end = \"....\\n\" ) # sep default is \" \"\n", - "print(3 + 4, 3 < 4, \"3\" + \"4\", sep = \"\\t\" ) # end default is \"\\n\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## fruitful function versus void function\n", - "- fruitful function: returns something\n", - " - ex: add3\n", - "- void function: doesn't return anything\n", - " - ex: bad_add3_v1" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "None\n" - ] - } - ], - "source": [ - "# Example of void function\n", - "def bad_add3_v1(x, y, z):\n", - " print(x + y + z)\n", - "\n", - "print(bad_add3_v1(4, 2, 1))" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n" - ] - }, - { - "ename": "TypeError", - "evalue": "unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_49460/1070455296.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbad_add3_v1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m**\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Cannot apply operations to None\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'" - ] - } - ], - "source": [ - "print(bad_add3_v1(4, 2, 1) ** 2) # Cannot apply mathematical operator to None" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### `return` statement is final\n", - "- exactly *one* `return` statement gets executed for a function call\n", - "- immediately after encountering `return`, function execution terminates" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "50" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def bad_add3_v2(x, y, z): \n", - " return x\n", - " return x + y + z # will never execute\n", - "\n", - "bad_add3_v2(50, 60, 70)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Default return type from a function is None. \n", - "None is a special type in Python (similar to null in Java)." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Trace this example\n", - "- manually\n", - "- then use PythonTutor" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "A1\n", - "B1\n", - "C\n", - "B2\n", - "A2\n" - ] - } - ], - "source": [ - "def func_c():\n", - " print(\"C\")\n", - "\n", - "def func_b():\n", - " print(\"B1\")\n", - " func_c()\n", - " print(\"B2\")\n", - "\n", - "def func_a():\n", - " print(\"A1\")\n", - " func_b()\n", - " print(\"A2\")\n", - "\n", - "func_a()" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/demo_lec_07-checkpoint.ipynb b/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/demo_lec_07-checkpoint.ipynb deleted file mode 100644 index 13ebce703231fadfee9ffbcda150f4a3bc054cf4..0000000000000000000000000000000000000000 --- a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/demo_lec_07-checkpoint.ipynb +++ /dev/null @@ -1,1180 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Correction from last lecture\n", - "- In Python, you *can* convert float value to int. Ex: int(10.56).\n", - "- In Python, you *cannot* convert a str with a float value into an int. Ex: int('10.56') will not work." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# Try it out\n", - "# int(10.56)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "# Try it out\n", - "# int('10.56')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Absolute of a number" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "abs(-10)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "abs(10)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's define our own absolute function." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "def absolute(x):\n", - " return (x ** 2) ** 0.5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To use a function, you need to call / invoke it." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "10.0" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "absolute(10)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "10.0" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "absolute(-10)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Square root of a number" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "# sqrt(16) # wouldn't work because module was not imported!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's import sqrt the math module.\n", - "Keywords: from, import" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4.0" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from math import sqrt # import sqrt function from math module\n", - "sqrt(16)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Modules are collections of similar functions and variables.\n", - "math module also contains a variable called 'pi'." - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "# pi # doesn't work, because we haven't imported pi yet" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3.141592653589793" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from math import * # import all functions and variables from math module\n", - "pi" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's define our own square root function." - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [], - "source": [ - "def square_root(x):\n", - " return x ** 0.5" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4.0" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "square_root(16)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Square root of a negative number gives you an imaginary number.\n", - "We won't be talking about imaginary numbers in this course." - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(2.4492935982947064e-16+4j)" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "square_root(-16)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Power: raising x to the power y" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8.0" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "pow(2, 3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's define our own power function." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [], - "source": [ - "def power(base, exp = 2):\n", - " return base ** exp" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "power(2, 3) # 2 goes into base and 3 goes into exp; these are examples of positional arguments" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "power(base = 2, exp = 3) # example of named keyword arguments" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "8" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "power(exp = 3, base = 2) # with named keyword arguments, you could switch the order of the parameters!" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "100" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "power(10) # 10 goes into base (positional arugment) and exp gets default argument as 2" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "# power(exp = 3, 2) # wouldn't work because positional arguments should come before keyword arguments" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Let's play with a dog" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "from dog import *" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK \n" - ] - } - ], - "source": [ - "speak()" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'stick'" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fetch()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's capture return value of speak and fetch functions." - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK \n" - ] - } - ], - "source": [ - "speak_val = speak()" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], - "source": [ - "fetch_val = fetch()" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [], - "source": [ - "speak_val # Doesn't display anything because there is no value inside this variable!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Default return type from a function is None. \n", - "None is a special type in Python (similar to null in Java)." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "None\n" - ] - } - ], - "source": [ - "print(speak_val) " - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "stick\n" - ] - } - ], - "source": [ - "print(fetch_val)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Let's play with the cat, by creating it" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [], - "source": [ - "from cat import *" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now cat functions replaced dog functions :(" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Meow!\n" - ] - } - ], - "source": [ - "speak()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Fetch is still from the dog module." - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'stick'" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fetch()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Two ways of importing functions from a module\n", - "1. from \\<module\\> import *\n", - "2. import module\n", - " - requires you to use attribute operator: “.”\n", - " - \\<module\\>.\\<function\\>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try second style of import with dog and cat." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Meow!\n", - "BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK \n" - ] - } - ], - "source": [ - "import cat\n", - "import dog\n", - "\n", - "cat.speak()\n", - "dog.speak()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Getting details about module\n", - "- Try type(module) and type(function)\n", - "- dir(module): gives you a list of the functions defined inside the module\n", - "- \\<module\\>.\\<function\\>.__doc__: gives you documentation about the function" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "module" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(dog)" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "function" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(dog.speak)" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['__builtins__',\n", - " '__cached__',\n", - " '__doc__',\n", - " '__file__',\n", - " '__loader__',\n", - " '__name__',\n", - " '__package__',\n", - " '__spec__',\n", - " 'fetch',\n", - " 'speak']" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(dog)" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['__builtins__',\n", - " '__cached__',\n", - " '__doc__',\n", - " '__file__',\n", - " '__loader__',\n", - " '__name__',\n", - " '__package__',\n", - " '__spec__',\n", - " 'speak']" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(cat)" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": {}, - "outputs": [], - "source": [ - "# dir(math) # doesn't work because dir works only with second style of import" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's import the math module." - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": {}, - "outputs": [], - "source": [ - "import math" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['__doc__',\n", - " '__file__',\n", - " '__loader__',\n", - " '__name__',\n", - " '__package__',\n", - " '__spec__',\n", - " 'acos',\n", - " 'acosh',\n", - " 'asin',\n", - " 'asinh',\n", - " 'atan',\n", - " 'atan2',\n", - " 'atanh',\n", - " 'ceil',\n", - " 'comb',\n", - " 'copysign',\n", - " 'cos',\n", - " 'cosh',\n", - " 'degrees',\n", - " 'dist',\n", - " 'e',\n", - " 'erf',\n", - " 'erfc',\n", - " 'exp',\n", - " 'expm1',\n", - " 'fabs',\n", - " 'factorial',\n", - " 'floor',\n", - " 'fmod',\n", - " 'frexp',\n", - " 'fsum',\n", - " 'gamma',\n", - " 'gcd',\n", - " 'hypot',\n", - " 'inf',\n", - " 'isclose',\n", - " 'isfinite',\n", - " 'isinf',\n", - " 'isnan',\n", - " 'isqrt',\n", - " 'ldexp',\n", - " 'lgamma',\n", - " 'log',\n", - " 'log10',\n", - " 'log1p',\n", - " 'log2',\n", - " 'modf',\n", - " 'nan',\n", - " 'perm',\n", - " 'pi',\n", - " 'pow',\n", - " 'prod',\n", - " 'radians',\n", - " 'remainder',\n", - " 'sin',\n", - " 'sinh',\n", - " 'sqrt',\n", - " 'tan',\n", - " 'tanh',\n", - " 'tau',\n", - " 'trunc']" - ] - }, - "execution_count": 40, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dir(math)" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "<function math.ceil(x, /)>" - ] - }, - "execution_count": 41, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "math.ceil" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "builtin_function_or_method" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(math.ceil)" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "math.ceil(3.1)" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Return the ceiling of x as an Integral.\n", - "\n", - "This is the smallest integer >= x.\n" - ] - } - ], - "source": [ - "print(math.ceil.__doc__)" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'this makes the dog bark a LOT'" - ] - }, - "execution_count": 45, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dog.speak.__doc__" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Let's learn how to write documentation for the cat module\n", - "- After you have imported a module, if you want to make changes, you have to run the input cell with import again!\n", - "- Easier way: Kernel > Restart & Run All." - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'the cat says whatever it likes'" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "cat.speak.__doc__" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Time module" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": {}, - "outputs": [], - "source": [ - "from time import time" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1632325558.3295722" - ] - }, - "execution_count": 48, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "time()" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "We're about to time how fast Python prints things!\n", - "Printing took 0.0007770061492919922 seconds.\n" - ] - } - ], - "source": [ - "start_time = time() \n", - "print(\"We're about to time how fast Python prints things!\")\n", - "end_time = time()\n", - "print(\"Printing took\", end_time - start_time, \"seconds.\")\n", - "\n", - "#You could use time() function to time your project code. \n", - "#In an initial cell, initialize start_time\n", - "#In the last cell, initialize end_time" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Random module" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "6\n" - ] - } - ], - "source": [ - "from random import randint\n", - "print(randint(1, 10))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Print versus return\n", - "- return statement is final in a function execution!\n", - "- Let's do this demo in Python Tutor" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n" - ] - } - ], - "source": [ - "def sequence_v1():\n", - " print(1)\n", - " print(2)\n", - " print(3)\n", - " \n", - "sequence_v1()" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n" - ] - } - ], - "source": [ - "# once you return, no other statements get executed\n", - "def sequence_v2():\n", - " return (1)\n", - " return (2)\n", - " return (3)\n", - " \n", - "seq_val = sequence_v2()\n", - "print(seq_val)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "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.8.8" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/demo_lec_07_template-checkpoint.ipynb b/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/demo_lec_07_template-checkpoint.ipynb deleted file mode 100644 index 3f5dc33a1a9df9fa0d5c56a9fdec6a4f5776993d..0000000000000000000000000000000000000000 --- a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/demo_lec_07_template-checkpoint.ipynb +++ /dev/null @@ -1,690 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Correction from last lecture\n", - "- In Python, you *can* convert float value to int. Ex: int(10.56).\n", - "- In Python, you *cannot* convert a str with a float value into an int. Ex: int('10.56') will not work." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Try it out\n", - "# int(10.56)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Try it out\n", - "# int('10.56')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Absolute of a number" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "abs(-10)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "abs(10)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's define our own absolute function." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To use a function, you need to call / invoke it." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Square root of a number" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# sqrt(16) # wouldn't work because module was not imported!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's import sqrt the math module.\n", - "Keywords: from, import" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Modules are collections of similar functions and variables.\n", - "math module also contains a variable called 'pi'." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "pi # doesn't work, because we haven't imported pi yet" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's define our own square root function." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Square root of a negative number gives you an imaginary number.\n", - "We won't be talking about imaginary numbers in this course." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Power: raising x to the power y" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "pow(2, 3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's define our own power function." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "power(2, 3) # 2 goes into base and 3 goes into exp; these are examples of positional arguments" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "power(base = 2, exp = 3) # example of named keyword arguments" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "power(exp = 3, base = 2) # with named keyword arguments, you could switch the order of the parameters!" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "power(10) # 10 goes into base (positional arugment) and exp gets default argument as 2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "power(exp = 3, 2) # wouldn't work because positional arguments should come before keyword arguments" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Let's play with a dog" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from dog import *" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "speak()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "fetch()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's capture return value of speak and fetch functions." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Default return type from a function is None. \n", - "None is a special type in Python (similar to null in Java)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Let's play with the cat, by creating it" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from cat import *" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now cat functions replaced dog functions :(" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "speak()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Fetch is still from the dog module." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "fetch()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Two ways of importing functions from a module\n", - "1. from \\<module\\> import *\n", - "2. import module\n", - " - requires you to use attribute operator: “.”\n", - " - \\<module\\>.\\<function\\>" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try second style of import with dog and cat." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import cat\n", - "import dog\n", - "\n", - "cat.speak()\n", - "dog.speak()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Getting details about module\n", - "- Try type(module) and type(function)\n", - "- dir(module): gives you a list of the functions defined inside the module\n", - "- \\<module\\>.\\<function\\>.__doc__: gives you documentation about the function" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "type(dog)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "type(dog.speak)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dir(dog)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dir(cat)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dir(math) # doesn't work because dir works only with second style of import" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's import the math module." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import math" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dir(math)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "math.ceil" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "type(math.ceil)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "math.ceil(3.1)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(math.ceil.__doc__)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dog.speak.__doc__" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Let's learn how to write documentation for the cat module\n", - "- After you have imported a module, if you want to make changes, you have to run the input cell with import again!\n", - "- Easier way: Kernel > Restart & Run All." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "cat.speak.__doc__" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Time module" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from time import time" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "time()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "start_time = time() \n", - "print(\"We're about to time how fast Python prints things!\")\n", - "end_time = time()\n", - "print(\"Printing took\", end_time - start_time, \"seconds.\")\n", - "\n", - "#You could use time() function to time your project code. \n", - "#In an initial cell, initialize start_time\n", - "#In the last cell, initialize end_time" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Random module" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from random import randint\n", - "print(randint(1, 10))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Print versus return\n", - "- return statement is final in a function execution!\n", - "- Let's do this demo in Python Tutor" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def sequence_v1():\n", - " print(1)\n", - " print(2)\n", - " print(3)\n", - " \n", - "sequence_v1()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# once you return, no other statements get executed\n", - "def sequence_v2():\n", - " return (1)\n", - " return (2)\n", - " return (3)\n", - " \n", - "seq_val = sequence_v2()\n", - "print(seq_val)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "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.8.8" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/lec_06_Creating_Functions-checkpoint.ipynb b/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/lec_06_Creating_Functions-checkpoint.ipynb deleted file mode 100644 index 04f63e4bf9b6a87a813880c15c7967c08a266b71..0000000000000000000000000000000000000000 --- a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/lec_06_Creating_Functions-checkpoint.ipynb +++ /dev/null @@ -1,926 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Creating Functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Review - pre-installed modules\n", - "\n", - "### Two ways of importing functions from a module\n", - "1. import \\<module\\>\n", - " - requires you to use attribute operator: `.`\n", - " - \\<module\\>.\\<function\\>\n", - "2. from \\<module\\> import \\<function\\>\n", - " - function can be called just with its name\n", - " \n", - "Let's learn about time module" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# Add all your import statements to this cell\n", - "\n", - "# TODO: import time module using import style of import\n", - "import time\n", - "\n", - "# TODO: use from style of import to import log10 function from math module\n", - "from math import log10\n", - "\n", - "# Bad style to import everything from a module\n", - "# Not recommended to do\n", - "#from math import *\n", - "# If you want to import everything, you need to \n", - "# follow import style of import\n", - "import math" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "time module time function shows the current time in seconds since epoch.\n", - "\n", - "What is epoch? epoch is January 1, 1970. **FUN FACT:** epoch is considered beginning of time for computers." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1644250723.254493\n", - "1644250729.630728\n", - "6.376235008239746\n" - ] - } - ], - "source": [ - "start_time = time.time()\n", - "x = 2 ** 1000000000 # some large computation\n", - "end_time = time.time()\n", - "\n", - "# TODO: change the line below to compute difference\n", - "difference = (end_time - start_time)\n", - "\n", - "# TODO: add a separator of '\\n'\n", - "print(start_time, end_time, difference, sep = \"\\n\") \n", - "\n", - "# TODO: discuss - how can you use time() function to time your project code?" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.0\n" - ] - } - ], - "source": [ - "# TODO: call log10 function to determine log base 10 of 1000\n", - "print(log10(1000))\n", - "\n", - "# Recall that you cannot use math. when you use from style of import\n", - "# print(math.log10(1000)) #doesn't work" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3.141592653589793" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# TODO: can you access pi variable inside math module?\n", - "#pi # TODO: discuss why this didn't work\n", - "\n", - "# TODO: go back to the import cell and import math \n", - "# TODO: fix line 2, so that you are now able to access pi inside math module\n", - "math.pi" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Learning Objectives\n", - "\n", - "- Explain the syntax of a function header:\n", - " - def, ( ), :, tabbing, return\n", - "- Write a function with:\n", - " - correct header and indentation\n", - " - a return value (fruitful function) or without (void function)\n", - " - parameters that have default values\n", - "- Write a function knowing the difference in outcomes of print and return statements\n", - "- Explain how positional, keyword, and default arguments are copied into parameters\n", - "- Make function calls using positional, keyword, and default arguments and determine the result.\n", - "- Trace function invocations to determine control flow" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 1: Cube of a number\n", - "- Input: number (to be cubed)\n", - "- Output: cubed number" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: Let's define the cube function\n", - "def cube(side):\n", - " return side ** 3" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "125\n", - "512\n" - ] - } - ], - "source": [ - "# Let's call cube function\n", - "print(cube(5))\n", - "# TODO: discuss what is different about the below line of code\n", - "print(cube(cube(2)))" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "37\n", - "37\n" - ] - } - ], - "source": [ - "# TODO: compute 4 cube + -3 cube\n", - "# version 1\n", - "print(cube(4) + cube(-3))\n", - "\n", - "# version 2\n", - "cube_of_4 = cube(4)\n", - "cube_of_minus_3 = cube(-3)\n", - "print(cube_of_4 + cube_of_minus_3)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "-1728\n" - ] - } - ], - "source": [ - "# TODO: compute 4 cube * -3 cube\n", - "# Now which one of the above two versions is better?\n", - "\n", - "print(cube_of_4 * cube_of_minus_3)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "# Whenever you think you are going to reuse a function call's\n", - "# output, save it in a variable\n", - "\n", - "# Rookie programmer mistake: calling the same function with the \n", - "# same arguments will always give the same return value\n", - "# Running the same function call twice takes twice the time" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`return` vs `print`\n", - "- `return` enables us to send output from a function to the calling place\n", - " - default `return` value is `None`\n", - " - that means, when you don't have a `return` statement, `None` will be returned\n", - "- `print` function simply displays / prints something\n", - " - it cannot enable you to produce output from a function" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: Go back to cube function defintion, change return to print\n", - "# Run the above function call cells to see what happens" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Tracing function invocations\n", - "- PythonTutor is a great tool to learn control flow\n", - "- Let's use PythonTutor to trace cube function invocation\n", - "- TODO: Copy-paste cube function defintion into PythonTutor (course website > tools > PythonTutor)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 2: is_between(lower, num, upper)\n", - "- Purpose: check whether number is within the range of lower and upper (inclusive)\n", - "- Input: lower bound, number, upper bound\n", - "- Output: boolean value (`True` or `False`)\n", - "- Keyword: `pass`:\n", - " - placeholder statement\n", - " - you cannot run a cell with an empty function definition" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n", - "True\n" - ] - } - ], - "source": [ - "def is_between(lower, num, upper):\n", - " #pass # TODO: remove this and try to run this cell\n", - " # version 1\n", - " return lower <= num <= upper\n", - " # version 2\n", - " #return lower <= num and num <= upper\n", - " \n", - "# you can call a function in the same cell that you defined it\n", - "print(is_between(3, 7, 21))\n", - "print(is_between(2, 14, 5))\n", - "print(is_between(100, cube(5), 200))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Types of arguments\n", - "- positional\n", - "- keyword\n", - "- default\n", - "\n", - "Python fills arguments in this order: positional, keyword, " - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 100\n", - "y = 10\n", - "z = 5\n", - "x = 100\n", - "y = 10\n", - "z = 5\n" - ] - }, - { - "data": { - "text/plain": [ - "115" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def add3(x, y = 100, z = 100): \n", - " \"\"\"adds three numbers\"\"\" #documentation string\n", - " print (\"x = \" + str(x))\n", - " print (\"y = \" + str(y))\n", - " print (\"z = \" + str(z))\n", - " return x + y + z\n", - "\n", - "sum = add3(100, 10, 5) \n", - "# TODO: 1. sum is a bad variable, discuss: why. What would be a better variable name?\n", - "# TODO: 2. what type of arguments are 100, 10, and 5? Positional\n", - "\n", - "total = add3(100, 10, 5)\n", - "total" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 1\n", - "y = 5\n", - "z = 2\n", - "8\n" - ] - } - ], - "source": [ - "print(add3(x = 1, z = 2, y = 5)) #TODO: what type of arguments are these? Keyword" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "x = 5\n", - "y = 6\n", - "z = 100\n" - ] - }, - { - "data": { - "text/plain": [ - "111" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "add3(5, 6) # TODO: what type of argument gets filled for the parameter z? Default value" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Positional arguments need to be specified before keyword arguments." - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "positional argument follows keyword argument (1597961864.py, line 2)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_58929/1597961864.py\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m add3(z = 5, 2, 7)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n" - ] - } - ], - "source": [ - "# Incorrect function call\n", - "add3(z = 5, 2, 7) \n", - "# TODO: what category of error is this?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Similarly, parameters with default values should be defined after parameters without default values." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "non-default argument follows default argument (1367324985.py, line 2)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_58929/1367324985.py\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m def add3_bad_version(x = 10, y, z):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" - ] - } - ], - "source": [ - "# Incorrect function definition\n", - "def add3_bad_version(x = 10, y, z): \n", - " \"\"\"adds three numbers\"\"\" #documentation string\n", - " return x + y + z" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python expects exactly same number of arguments as parameters." - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "add3() got multiple values for argument 'x'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_58929/100225854.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Incorrect function call\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0madd3\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m4\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 3\u001b[0m \u001b[0;31m# TODO: what category of error is this?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mTypeError\u001b[0m: add3() got multiple values for argument 'x'" - ] - } - ], - "source": [ - "# Incorrect function call\n", - "add3(5, 3, 10, x = 4)\n", - "# TODO: what category of error is this?" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "add3() missing 1 required positional argument: 'x'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_58929/2441706151.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# TODO: will this function call work?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0madd3\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\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: add3() missing 1 required positional argument: 'x'" - ] - } - ], - "source": [ - "# TODO: will this function call work?\n", - "add3(y = 5, z = 10)" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "add3() missing 1 required positional argument: 'x'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_58929/2146538074.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# TODO: will this function call work?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0madd3\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[0;31mTypeError\u001b[0m: add3() missing 1 required positional argument: 'x'" - ] - } - ], - "source": [ - "# TODO: will this function call work?\n", - "add3()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 3: Generate a height x width grid\n", - "- Input: width, height, grid symbol, title of the grid\n", - "- Output: string containing title, a newline, and the grid\n", - "- Pseudocode steps:\n", - " 1. Generate a single row of symb (width dimension). What string operator do you need?\n", - " 2. Capture single row into a variable\n", - " 3. Add newline to single row variable.\n", - " 4. Generate multiple rows (height dimension). What string operator do you need?\n", - " 5. Generate the output string to be returned by adding title with a newline with the output from step 4." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: how many parameters have default values in the below function?\n", - "def get_grid(width, height, symb = '#', title = 'My Grid:'):\n", - " row = symb * width\n", - " grid = (row + '\\n') * height\n", - " return title + '\\n' + grid" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "My Grid:\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "##########\n", - "\n" - ] - } - ], - "source": [ - "# TODO: generate various sized grids, by exploring\n", - "# three types of arguments\n", - "# Here is one example\n", - "print(get_grid(10, 8))" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: use PythonTutor to trace get_grid function call" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "My Grid:\n", - "@@@@@\n", - "@@@@@\n", - "@@@@@\n", - "@@@@@\n", - "\n" - ] - } - ], - "source": [ - "print(get_grid(5, 4, symb = \"@\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Some Grid:\n", - "..\n", - "..\n", - "..\n", - "\n" - ] - } - ], - "source": [ - "print(get_grid(2, 3, \".\", \"Some Grid:\"))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "When you use keyword arguments, the order of the arguments need not match with the parameters.\n", - "This is because we tie the arguments to the parameters, by explicitly saying parameter = argument" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Some other grid:\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "^^^^\n", - "\n" - ] - } - ], - "source": [ - "# TODO: Try using all keyword arguments and use different order than the order of the parameters.\n", - "print(get_grid(symb = \"^\", title = \"Some other grid:\", width = 4, height = 7))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Revisiting `print` function\n", - "- Let's look at `help(print)` to learn about print's parameters\n", - " - Default value for `sep` is space, that is: \" \"\n", - " - Default value for `end` is newline, that is: \"\\n\"" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on built-in function print in module builtins:\n", - "\n", - "print(...)\n", - " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", - " \n", - " Prints the values to a stream, or to sys.stdout by default.\n", - " Optional keyword arguments:\n", - " file: a file-like object (stream); defaults to the current sys.stdout.\n", - " sep: string inserted between values, default a space.\n", - " end: string appended after the last value, default a newline.\n", - " flush: whether to forcibly flush the stream.\n", - "\n" - ] - } - ], - "source": [ - "help(print)" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7 True 34....\n", - "7\tTrue\t34\n" - ] - } - ], - "source": [ - "# TODO: predict output, then run to validate your prediction\n", - "print(3 + 4, 3 < 4, \"3\" + \"4\", end = \"....\\n\" ) # sep default is \" \"\n", - "print(3 + 4, 3 < 4, \"3\" + \"4\", sep = \"\\t\" ) # end default is \"\\n\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## fruitful function versus void function\n", - "- fruitful function: returns something\n", - " - ex: add3\n", - "- void function: doesn't return anything\n", - " - ex: bad_add3_v1" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n", - "None\n" - ] - } - ], - "source": [ - "# Example of void function\n", - "def bad_add3_v1(x, y, z):\n", - " print(x + y + z)\n", - "\n", - "print(bad_add3_v1(4, 2, 1))" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n" - ] - }, - { - "ename": "TypeError", - "evalue": "unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_58929/71664968.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbad_add3_v1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m**\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Cannot apply mathematical operator to None\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'" - ] - } - ], - "source": [ - "print(bad_add3_v1(4, 2, 1) ** 2) # Cannot apply mathematical operator to None" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### `return` statement is final\n", - "- exactly *one* `return` statement gets executed for a function call\n", - "- immediately after encountering `return`, function execution terminates" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "50" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def bad_add3_v2(x, y, z): \n", - " return x\n", - " return x + y + z # will never execute\n", - "\n", - "bad_add3_v2(50, 60, 70)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Default return type from a function is None. \n", - "None is a special type in Python (similar to null in Java)." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Trace this example\n", - "- manually\n", - "- then use PythonTutor" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "A1\n", - "B1\n", - "C\n", - "B2\n", - "A2\n" - ] - } - ], - "source": [ - "def func_c():\n", - " print(\"C\")\n", - "\n", - "def func_b():\n", - " print(\"B1\")\n", - " func_c()\n", - " print(\"B2\")\n", - "\n", - "def func_a():\n", - " print(\"A1\")\n", - " func_b()\n", - " print(\"A2\")\n", - "\n", - "func_a()" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/lec_06_Creating_functions_template-checkpoint.ipynb b/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/lec_06_Creating_functions_template-checkpoint.ipynb deleted file mode 100644 index 9b194630c48097df2ef238db435f29ead72cce9f..0000000000000000000000000000000000000000 --- a/f22/meena_lec_notes/lec-06/.ipynb_checkpoints/lec_06_Creating_functions_template-checkpoint.ipynb +++ /dev/null @@ -1,601 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Creating Functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Review - pre-installed modules\n", - "\n", - "### Two ways of importing functions from a module\n", - "1. import \\<module\\>\n", - " - requires you to use attribute operator: `.`\n", - " - \\<module\\>.\\<function\\>\n", - "2. from \\<module\\> import \\<function\\>\n", - " - function can be called just with its name\n", - " \n", - "Let's learn about time module" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Add all your import statements to this cell\n", - "\n", - "# TODO: import time module using import style of import\n", - "\n", - "# TODO: use from style of import to import log10 function from math module\n", - "\n", - "# Bad style to import everything from a module\n", - "# Not recommended to do\n", - "#from math import *\n", - "# If you want to import everything, you need to \n", - "# follow import style of import" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "time module time function shows the current time in seconds since epoch.\n", - "\n", - "What is epoch? epoch is January 1, 1970. **FUN FACT:** epoch is considered beginning of time for computers." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "start_time = time.time()\n", - "x = 2 ** 1000000000 # some large computation\n", - "end_time = time.time()\n", - "\n", - "# TODO: change the line below to compute difference\n", - "difference = ???\n", - "\n", - "# TODO: add a separator of '\\n'\n", - "print(start_time, end_time, difference, ???) \n", - "\n", - "# TODO: discuss - how can you use time() function to time your project code?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: call log10 function to determine log base 10 of 1000\n", - "\n", - "# Recall that you cannot use math. when you use from style of import\n", - "# print(math.log10(1000)) #doesn't work" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: can you access pi variable inside math module?\n", - "pi # TODO: discuss why this didn't work\n", - "\n", - "# TODO: go back to the import cell and import math \n", - "# TODO: fix line 2, so that you are now able to access pi inside math module" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Learning Objectives\n", - "\n", - "- Explain the syntax of a function header:\n", - " - def, ( ), :, tabbing, return\n", - "- Write a function with:\n", - " - correct header and indentation\n", - " - a return value (fruitful function) or without (void function)\n", - " - parameters that have default values\n", - "- Write a function knowing the difference in outcomes of print and return statements\n", - "- Explain how positional, keyword, and default arguments are copied into parameters\n", - "- Make function calls using positional, keyword, and default arguments and determine the result.\n", - "- Trace function invocations to determine control flow" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 1: Cube of a number\n", - "- Input: number (to be cubed)\n", - "- Output: cubed number" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: Let's define the cube function\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Let's call cube function\n", - "print(cube(5))\n", - "# TODO: discuss what is different about the below line of code\n", - "print(cube(cube(2)))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: compute 4 cube + -3 cube\n", - "# version 1\n", - "\n", - "# version 2\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: compute 4 cube * -3 cube\n", - "# Now which one of the above two versions is better?\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Whenever you think you are going to reuse a function call's\n", - "# output, save it in a variable\n", - "\n", - "# Rookie programmer mistake: calling the same function with the \n", - "# same arguments will always give the same return value\n", - "# Running the same function call twice takes twice the time" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "`return` vs `print`\n", - "- `return` enables us to send output from a function to the calling place\n", - " - default `return` value is `None`\n", - " - that means, when you don't have a `return` statement, `None` will be returned\n", - "- `print` function simply displays / prints something\n", - " - it cannot enable you to produce output from a function" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: Go back to cube function defintion, change return to print\n", - "# Run the above function call cells to see what happens" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Tracing function invocations\n", - "- PythonTutor is a great tool to learn control flow\n", - "- Let's use PythonTutor to trace cube function invocation\n", - "- TODO: Copy-paste cube function defintion into PythonTutor (course website > tools > PythonTutor)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 2: is_between(lower, num, upper)\n", - "- Purpose: check whether number is within the range of lower and upper (inclusive)\n", - "- Input: lower bound, number, upper bound\n", - "- Output: boolean value (`True` or `False`)\n", - "- Keyword: `pass`:\n", - " - placeholder statement\n", - " - you cannot run a cell with an empty function definition" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def is_between(lower, num, upper):\n", - " pass\n", - " \n", - "# you can call a function in the same cell that you defined it\n", - "print(is_between(3, 7, 21))\n", - "print(is_between(2, 14, 5))\n", - "print(is_between(100, cube(5), 200))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Types of arguments\n", - "- positional\n", - "- keyword\n", - "- default\n", - "\n", - "Python fills arguments in this order: positional, keyword, " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def add3(x, y = 100, z = 100): \n", - " \"\"\"adds three numbers\"\"\" #documentation string\n", - " print (\"x = \" + str(x))\n", - " print (\"y = \" + str(y))\n", - " print (\"z = \" + str(z))\n", - " return x + y + z\n", - "\n", - "sum = add3(100, 10, 5) \n", - "# TODO: 1. sum is a bad variable, discuss: why. What would be a better variable name?\n", - "# TODO: 2. what type of arguments are 100, 10, and 5?\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(add3(x = 1, z = 2, y = 5)) #TODO: what type of arguments are these?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "add3(5, 6) # TODO: what type of argument gets filled for the parameter z?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Positional arguments need to be specified before keyword arguments." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Incorrect function call\n", - "add3(z = 5, 2, 7) \n", - "# TODO: what category of error is this?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Similarly, parameters with default values should be defined after parameters without default values." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Incorrect function definition\n", - "def add3_bad_version(x = 10, y, z): \n", - " \"\"\"adds three numbers\"\"\" #documentation string\n", - " print (\"y = \" + str(y))\n", - " return x + y + z" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Python expects exactly same number of arguments as parameters." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Incorrect function call\n", - "add3(5, 3, 10, x = 4)\n", - "# TODO: what category of error is this?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: will this function call work?\n", - "add3(y = 5, z = 10)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: will this function call work?\n", - "add3()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example 3: Generate a height x width grid\n", - "- Input: width, height, grid symbol, title of the grid\n", - "- Output: string containing title, a newline, and the grid\n", - "- Pseudocode steps:\n", - " 1. Generate a single row of symb (width dimension). What string operator do you need?\n", - " 2. Capture single row into a variable\n", - " 3. Add newline to single row variable.\n", - " 4. Generate multiple rows (height dimension). What string operator do you need?\n", - " 5. Generate the output string to be returned by adding title with a newline with the output from step 4." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: how many parameters have default values in the below function?\n", - "def get_grid(width, height, symb = '#', title = 'My Grid:'):\n", - " row = symb * width\n", - " grid = (row + '\\n') * height\n", - " return title + '\\n' + grid" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: generate various sized grids, by exploring\n", - "# three types of arguments\n", - "# Here is one example\n", - "print(get_grid(10, 8))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: use PythonTutor to trace get_grid function call" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "When you use keyword arguments, the order of the arguments need not match with the parameters.\n", - "This is because we tie the arguments to the parameters, by explicitly saying parameter = argument." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: Try using all keyword arguments and use different order than the order of the parameters.\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Revisiting `print` function\n", - "- Let's look at `help(print)` to learn about print's parameters\n", - " - Default value for `sep` is space, that is: \" \"\n", - " - Default value for `end` is newline, that is: \"\\n\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "help(print)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: predict output, then run to validate your prediction\n", - "print(3 + 4, 3 < 4, \"3\" + \"4\", end = \"....\\n\" ) # sep default is \" \"\n", - "print(3 + 4, 3 < 4, \"3\" + \"4\", sep = \"\\t\" ) # end default is \"\\n\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## fruitful function versus void function\n", - "- fruitful function: returns something\n", - " - ex: add3\n", - "- void function: doesn't return anything\n", - " - ex: bad_add3_v1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Example of void function\n", - "def bad_add3_v1(x, y, z):\n", - " pass\n", - "\n", - "print(bad_add3_v1(4, 2, 1))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(bad_add3_v1(4, 2, 1) ** 2) # Cannot apply mathematical operator to None" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### `return` statement is final\n", - "- exactly *one* `return` statement gets executed for a function call\n", - "- immediately after encountering `return`, function execution terminates" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def bad_add3_v2(x, y, z): \n", - " return x\n", - " return x + y + z # will never execute\n", - "\n", - "bad_add3_v2(50, 60, 70)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Default return type from a function is None. \n", - "None is a special type in Python (similar to null in Java)." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Trace this example\n", - "- manually\n", - "- then use PythonTutor" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def func_c():\n", - " print(\"C\")\n", - "\n", - "def func_b():\n", - " print(\"B1\")\n", - " func_c()\n", - " print(\"B2\")\n", - "\n", - "def func_a():\n", - " print(\"A1\")\n", - " func_b()\n", - " print(\"A2\")\n", - "\n", - "func_a()" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.9.7" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/f22/meena_lec_notes/lec-06/__pycache__/cat.cpython-38.pyc b/f22/meena_lec_notes/lec-06/__pycache__/cat.cpython-38.pyc deleted file mode 100644 index 48252cce316a3025b475ab935ecad357de9f6465..0000000000000000000000000000000000000000 Binary files a/f22/meena_lec_notes/lec-06/__pycache__/cat.cpython-38.pyc and /dev/null differ diff --git a/f22/meena_lec_notes/lec-06/__pycache__/dog.cpython-38.pyc b/f22/meena_lec_notes/lec-06/__pycache__/dog.cpython-38.pyc deleted file mode 100644 index d9f4777bea2056a1f3e0ff807cad9028b95eb687..0000000000000000000000000000000000000000 Binary files a/f22/meena_lec_notes/lec-06/__pycache__/dog.cpython-38.pyc and /dev/null differ