From e71a17ba675506819c43379865ef005daa9379fe Mon Sep 17 00:00:00 2001 From: msyamkumar <msyamkumar@wisc.edu> Date: Fri, 16 Sep 2022 08:05:11 -0500 Subject: [PATCH] Removing checkpoints --- .../lec_05_Using_Functions-checkpoint.ipynb | 1129 ----------------- ..._Using_Functions_template-checkpoint.ipynb | 552 -------- 2 files changed, 1681 deletions(-) delete mode 100644 f22/meena_lec_notes/lec-05/.ipynb_checkpoints/lec_05_Using_Functions-checkpoint.ipynb delete mode 100644 f22/meena_lec_notes/lec-05/.ipynb_checkpoints/lec_05_Using_Functions_template-checkpoint.ipynb diff --git a/f22/meena_lec_notes/lec-05/.ipynb_checkpoints/lec_05_Using_Functions-checkpoint.ipynb b/f22/meena_lec_notes/lec-05/.ipynb_checkpoints/lec_05_Using_Functions-checkpoint.ipynb deleted file mode 100644 index 7e6ad11..0000000 --- a/f22/meena_lec_notes/lec-05/.ipynb_checkpoints/lec_05_Using_Functions-checkpoint.ipynb +++ /dev/null @@ -1,1129 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Using Functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Review" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Valid variable names" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# this is a possible quiz / exam question\n", - "# self-check: decide if the variables are valid in Python:\n", - "\n", - "#my.name = \"Meena\" # can't use .\n", - "#1st_place = \"Andy\" # can't start with a digit\n", - "#stop&go = False # can't use & and other special chars\n", - "#type = 100 # bad to use keywords as variable names\n", - "end_game = True # valid variable; good style to use _" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Short-circuiting with `and`, `or`\n", - "\n", - "- if the first half of an AND is False, skip the second half for evaluation\n", - "- if the first half of an OR is True, skip the second half for evaluation\n", - "\n", - "Predict the output of each expression and then uncomment and run the expression, to validate." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "#7+3 == 9 and 4/0 == 0 # False\n", - "#7 + 3 == 10 or 4/0 == 0 # True\n", - "#4/0 == 0 and 7+3 == 9 # ZeroDivisionError\n", - "#4/0 == 0 or 7+3 == 10 # ZeroDivisionError\n", - "4+5 == 9 or 5**10000000 < 10000000 # True" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Learning Objectives\n", - "\n", - "- Call `input()` to accept and process string input\n", - "- Call type cast functions to convert to appropriate type: `int()`, `bool()`, `float()`, `str()`\n", - " - Concatenate values onto strings within a print function by converting to `str()`\n", - "- Using correct vocabulary, explain a function call\n", - " - call/invoke, parameter, argument, keyword arguments, return value\n", - "- Use `sep`, `end` keyword arguments with the print() function\n", - "- Yse some common built-in functions such as round(), abs()\n", - "- import functions from a built-in module using `import`, `from`\n", - "- Call functions in modules using the attribute operator `.`" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Vocabulary\n", - "\n", - "- function definition: a grouping of lines of code; a way for us to tell our program to run that entire group of code\n", - "- call / invoke: a statement in Python code that instructs the program to run all the lines of code in a function definition, and then come back afterward\n", - "- parameter: variable that receives input to function\n", - "- argument: value sent to a function (lines up with parameter)\n", - "- keyword argument: argument explicitly tied to a parameter\n", - "- return value: function output sent back to calling code\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Recall print(...), type(...) functions" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello\n", - "world\n" - ] - } - ], - "source": [ - "print(\"hello\\nworld\")" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "int" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "type(1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You could either display the return value of a function into an output box or capture it into a variable." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "<class 'bool'>\n" - ] - } - ], - "source": [ - "t = type(1 == 1.0)\n", - "print(t)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "One function's return value can become another function's argument directly." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "<class 'bool'>\n" - ] - } - ], - "source": [ - "print(type(1 == 1.0))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## input()\n", - "- accepts user input\n", - "- argument to input() function is a prompt\n", - " - prompt provides meaningful information to the user\n", - "- return value of input() function is always of type str" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Example 1: Accept user input for name and say hello." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Enter your name: Viyan\n", - "<class 'str'>\n", - "Hello Viyan!\n" - ] - } - ], - "source": [ - "#TODO: call input function \n", - "name = input(\"Enter your name: \")\n", - "#TODO: print type of the variable name\n", - "print(type(name))\n", - "print(\"Hello \" + name + \"!\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Example 2a: Accept user input for age (incorrect version)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Enter your age: 2\n", - "<class 'str'>\n" - ] - }, - { - "ename": "TypeError", - "evalue": "can only concatenate str (not \"int\") to str", - "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_42489/307506462.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Let's celebrate a birthday :)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mage\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;31m# What is wrong?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" - ] - } - ], - "source": [ - "age = input(\"Enter your age: \")\n", - "#TODO: print type of the variable age\n", - "print(type(age))\n", - "# Let's celebrate a birthday :)\n", - "age += 1 # What is wrong?\n", - "print(age)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Type casting functions\n", - "- convert one type into another type\n", - "- int(), float(), str(), bool()" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "32\n", - "2022\n" - ] - }, - { - "ename": "ValueError", - "evalue": "invalid literal for int() with base 10: 'nineteen'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_42489/3389418016.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Some conversions don't make sense to us\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# So they won't make sense to Python either\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"nineteen\"\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;31mValueError\u001b[0m: invalid literal for int() with base 10: 'nineteen'" - ] - } - ], - "source": [ - "# Let's try these int conversions\n", - "print(int(32.5))\n", - "print(int(\"2012\") + 10)\n", - "# Some conversions don't make sense to us\n", - "# So they won't make sense to Python either\n", - "print(int(\"nineteen\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "32\n", - "32\n" - ] - }, - { - "ename": "ValueError", - "evalue": "invalid literal for int() with base 10: '32.5'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_42489/1366206231.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# this doesn't work, less intuitive (tricky case!)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"32.5\"\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;31mValueError\u001b[0m: invalid literal for int() with base 10: '32.5'" - ] - } - ], - "source": [ - "print(int(32.5)) # this works\n", - "print(int(float(\"32.5\"))) # this works\n", - "\n", - "# this doesn't work, less intuitive (tricky case!)\n", - "print(int(\"32.5\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "26.0\n", - "6.28\n" - ] - }, - { - "ename": "ValueError", - "evalue": "could not convert string to float: 'three point one'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_42489/1013542327.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Some conversions don't make sense to us\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# So they won't make sense to Python either\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"three point one\"\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;31mValueError\u001b[0m: could not convert string to float: 'three point one'" - ] - } - ], - "source": [ - "# Let's try these float conversions\n", - "print(float(26))\n", - "print(float(\"3.14\") * 2)\n", - "# Some conversions don't make sense to us\n", - "# So they won't make sense to Python either\n", - "print(float(\"three point one\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "can only concatenate str (not \"int\") to str", - "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_42489/1518523691.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Let's try these str conversions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0myear\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m2022\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Next year will be: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0myear\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\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 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"It is good to be: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Who wants a pi? \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.14\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[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" - ] - } - ], - "source": [ - "# Let's try these str conversions\n", - "year = 2022\n", - "print(\"Next year will be: \" + year+1)\n", - "print(\"It is good to be: \" + str(True))\n", - "print(\"Who wants a pi? \" + str(3.14))\n", - "#Pretty much you can convert anything to string in Python" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Example 2b: Accept user input for age (correct version)" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Enter your age: 2\n", - "3\n" - ] - } - ], - "source": [ - "age = input(\"Enter your age: \")\n", - "#TODO: convert age to int type\n", - "age = int(age) # Just saying int(age) won't work, you have to re-assign age variable\n", - "# Let's celebrate a birthday :)\n", - "age += 1 \n", - "print(age)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Example 3: Accept user input for temperature" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Enter your temperature: 98.6\n", - "<class 'bool'>\n", - "OK to enter: True\n" - ] - } - ], - "source": [ - "# To enter their day care, kids have their temp checked\n", - "temp = float(input(\"Enter your temperature: \"))\n", - "temp = float(temp)\n", - "#TODO: check that temp is less than equal to 98.6\n", - "temp_ok = temp <= 98.6\n", - "#TODO: print type of temp_ok\n", - "print(type(temp_ok))\n", - "print(\"OK to enter: \" + str(temp_ok))" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Somebody had a birthday: Viyan 3 years old :)\n" - ] - } - ], - "source": [ - "# TODO: figure out how this works\n", - "print(\"Somebody had a birthday: \" + name + \" \" + str(age) + \" years old :)\")\n", - "# TODO: What error will you get when you take out str(...) around age?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### parameters for print(...) function\n", - "- can accept multiple arguments:\n", - " - all arguments get printed on the same line\n", - "- `sep` allows you to configure separator between the arguments:\n", - " - default value: space `\" \"`\n", - "- `end` allows you to configure what character gets printed after\n", - " - default value: newline `\"\\n\"`" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello world\n", - "hello cs220/cs319\n" - ] - } - ], - "source": [ - "print(\"hello\", \"world\")\n", - "print(\"hello\", \"cs220/cs319\")" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "hello|world;\n", - "hello^meena...\n", - "****||######<END>******||########<END>\n", - "****||######<END>\n", - "******||########<END>\n" - ] - } - ], - "source": [ - "# Guess what will get printed\n", - "print(\"hello\", \"world\", sep = \"|\", end = \";\\n\")\n", - "print(\"hello\", \"meena\", sep = \"^\", end = \"...\\n\")\n", - "\n", - "print(\"*\" * 4, \"#\" * 6, sep = \"||\", end = \"<END>\")\n", - "print(\"*\" * 6, \"#\" * 8, sep = \"||\", end = \"<END>\")\n", - "\n", - "print(\"\\n\", end = \"\")\n", - "\n", - "print(\"*\" * 4, \"#\" * 6, sep = \"||\", end = \"<END>\\n\")\n", - "print(\"*\" * 6, \"#\" * 8, sep = \"||\", end = \"<END>\\n\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## More built-in functions\n", - "- print(), type(), int(), float(), str(), bool() are all examples of built-in functions\n", - "- built-in functions are functions you can use readily (without importing anything)\n", - "- you definitely don't have to know all the built-in functions, but here is the list: https://docs.python.org/3/library/functions.html" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "abs function" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "10\n", - "10\n" - ] - } - ], - "source": [ - "# Guess what will get printed\n", - "print(abs(-10))\n", - "print(abs(10))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "round function" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "10.5253\n", - "5.3\n", - "5.24\n" - ] - } - ], - "source": [ - "# Guess what will get printed\n", - "print(round(10.525252, 4))\n", - "print(round(5.2953, 2))\n", - "print(round(5.2423, 2))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Modules\n", - "- collection of similar functions and variables\n", - "- requires import\n", - "- `import`, `from` keywords\n", - "- 2 styles of import:\n", - " 1. `import` module_name: \n", - " - need to specify module_name`.`function_name to call it. \n", - " - `.` is called attribute operator.\n", - " 2. `from` module_name `import` function_name\n", - " - can call the function without specifying module name\n", - "- `help` module_name:\n", - " - documentation for all functions and variables inside a module" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'sqrt' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_42489/992675185.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Let's try to find square root of 10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0msqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# doesn't work because it is part of math module\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'sqrt' is not defined" - ] - } - ], - "source": [ - "# Let's try to find square root of 10\n", - "sqrt(10) # doesn't work because it is part of math module" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "It is a good practice to include all your import statements at the top of your notebook file. We will make exception just for a couple of lectures." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "import math" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3.1622776601683795" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "math.sqrt(10)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "-0.9999999999964793" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "math.cos(3.14159)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Modules contain useful variables too." - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3.141592653589793" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "math.pi" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Help on module math:\n", - "\n", - "NAME\n", - " math\n", - "\n", - "MODULE REFERENCE\n", - " https://docs.python.org/3.9/library/math\n", - " \n", - " The following documentation is automatically generated from the Python\n", - " source files. It may be incomplete, incorrect or include features that\n", - " are considered implementation detail and may vary between Python\n", - " implementations. When in doubt, consult the module reference at the\n", - " location listed above.\n", - "\n", - "DESCRIPTION\n", - " This module provides access to the mathematical functions\n", - " defined by the C standard.\n", - "\n", - "FUNCTIONS\n", - " acos(x, /)\n", - " Return the arc cosine (measured in radians) of x.\n", - " \n", - " The result is between 0 and pi.\n", - " \n", - " acosh(x, /)\n", - " Return the inverse hyperbolic cosine of x.\n", - " \n", - " asin(x, /)\n", - " Return the arc sine (measured in radians) of x.\n", - " \n", - " The result is between -pi/2 and pi/2.\n", - " \n", - " asinh(x, /)\n", - " Return the inverse hyperbolic sine of x.\n", - " \n", - " atan(x, /)\n", - " Return the arc tangent (measured in radians) of x.\n", - " \n", - " The result is between -pi/2 and pi/2.\n", - " \n", - " atan2(y, x, /)\n", - " Return the arc tangent (measured in radians) of y/x.\n", - " \n", - " Unlike atan(y/x), the signs of both x and y are considered.\n", - " \n", - " atanh(x, /)\n", - " Return the inverse hyperbolic tangent of x.\n", - " \n", - " ceil(x, /)\n", - " Return the ceiling of x as an Integral.\n", - " \n", - " This is the smallest integer >= x.\n", - " \n", - " comb(n, k, /)\n", - " Number of ways to choose k items from n items without repetition and without order.\n", - " \n", - " Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates\n", - " to zero when k > n.\n", - " \n", - " Also called the binomial coefficient because it is equivalent\n", - " to the coefficient of k-th term in polynomial expansion of the\n", - " expression (1 + x)**n.\n", - " \n", - " Raises TypeError if either of the arguments are not integers.\n", - " Raises ValueError if either of the arguments are negative.\n", - " \n", - " copysign(x, y, /)\n", - " Return a float with the magnitude (absolute value) of x but the sign of y.\n", - " \n", - " On platforms that support signed zeros, copysign(1.0, -0.0)\n", - " returns -1.0.\n", - " \n", - " cos(x, /)\n", - " Return the cosine of x (measured in radians).\n", - " \n", - " cosh(x, /)\n", - " Return the hyperbolic cosine of x.\n", - " \n", - " degrees(x, /)\n", - " Convert angle x from radians to degrees.\n", - " \n", - " dist(p, q, /)\n", - " Return the Euclidean distance between two points p and q.\n", - " \n", - " The points should be specified as sequences (or iterables) of\n", - " coordinates. Both inputs must have the same dimension.\n", - " \n", - " Roughly equivalent to:\n", - " sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q)))\n", - " \n", - " erf(x, /)\n", - " Error function at x.\n", - " \n", - " erfc(x, /)\n", - " Complementary error function at x.\n", - " \n", - " exp(x, /)\n", - " Return e raised to the power of x.\n", - " \n", - " expm1(x, /)\n", - " Return exp(x)-1.\n", - " \n", - " This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.\n", - " \n", - " fabs(x, /)\n", - " Return the absolute value of the float x.\n", - " \n", - " factorial(x, /)\n", - " Find x!.\n", - " \n", - " Raise a ValueError if x is negative or non-integral.\n", - " \n", - " floor(x, /)\n", - " Return the floor of x as an Integral.\n", - " \n", - " This is the largest integer <= x.\n", - " \n", - " fmod(x, y, /)\n", - " Return fmod(x, y), according to platform C.\n", - " \n", - " x % y may differ.\n", - " \n", - " frexp(x, /)\n", - " Return the mantissa and exponent of x, as pair (m, e).\n", - " \n", - " m is a float and e is an int, such that x = m * 2.**e.\n", - " If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.\n", - " \n", - " fsum(seq, /)\n", - " Return an accurate floating point sum of values in the iterable seq.\n", - " \n", - " Assumes IEEE-754 floating point arithmetic.\n", - " \n", - " gamma(x, /)\n", - " Gamma function at x.\n", - " \n", - " gcd(*integers)\n", - " Greatest Common Divisor.\n", - " \n", - " hypot(...)\n", - " hypot(*coordinates) -> value\n", - " \n", - " Multidimensional Euclidean distance from the origin to a point.\n", - " \n", - " Roughly equivalent to:\n", - " sqrt(sum(x**2 for x in coordinates))\n", - " \n", - " For a two dimensional point (x, y), gives the hypotenuse\n", - " using the Pythagorean theorem: sqrt(x*x + y*y).\n", - " \n", - " For example, the hypotenuse of a 3/4/5 right triangle is:\n", - " \n", - " >>> hypot(3.0, 4.0)\n", - " 5.0\n", - " \n", - " isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)\n", - " Determine whether two floating point numbers are close in value.\n", - " \n", - " rel_tol\n", - " maximum difference for being considered \"close\", relative to the\n", - " magnitude of the input values\n", - " abs_tol\n", - " maximum difference for being considered \"close\", regardless of the\n", - " magnitude of the input values\n", - " \n", - " Return True if a is close in value to b, and False otherwise.\n", - " \n", - " For the values to be considered close, the difference between them\n", - " must be smaller than at least one of the tolerances.\n", - " \n", - " -inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n", - " is, NaN is not close to anything, even itself. inf and -inf are\n", - " only close to themselves.\n", - " \n", - " isfinite(x, /)\n", - " Return True if x is neither an infinity nor a NaN, and False otherwise.\n", - " \n", - " isinf(x, /)\n", - " Return True if x is a positive or negative infinity, and False otherwise.\n", - " \n", - " isnan(x, /)\n", - " Return True if x is a NaN (not a number), and False otherwise.\n", - " \n", - " isqrt(n, /)\n", - " Return the integer part of the square root of the input.\n", - " \n", - " lcm(*integers)\n", - " Least Common Multiple.\n", - " \n", - " ldexp(x, i, /)\n", - " Return x * (2**i).\n", - " \n", - " This is essentially the inverse of frexp().\n", - " \n", - " lgamma(x, /)\n", - " Natural logarithm of absolute value of Gamma function at x.\n", - " \n", - " log(...)\n", - " log(x, [base=math.e])\n", - " Return the logarithm of x to the given base.\n", - " \n", - " If the base not specified, returns the natural logarithm (base e) of x.\n", - " \n", - " log10(x, /)\n", - " Return the base 10 logarithm of x.\n", - " \n", - " log1p(x, /)\n", - " Return the natural logarithm of 1+x (base e).\n", - " \n", - " The result is computed in a way which is accurate for x near zero.\n", - " \n", - " log2(x, /)\n", - " Return the base 2 logarithm of x.\n", - " \n", - " modf(x, /)\n", - " Return the fractional and integer parts of x.\n", - " \n", - " Both results carry the sign of x and are floats.\n", - " \n", - " nextafter(x, y, /)\n", - " Return the next floating-point value after x towards y.\n", - " \n", - " perm(n, k=None, /)\n", - " Number of ways to choose k items from n items without repetition and with order.\n", - " \n", - " Evaluates to n! / (n - k)! when k <= n and evaluates\n", - " to zero when k > n.\n", - " \n", - " If k is not specified or is None, then k defaults to n\n", - " and the function returns n!.\n", - " \n", - " Raises TypeError if either of the arguments are not integers.\n", - " Raises ValueError if either of the arguments are negative.\n", - " \n", - " pow(x, y, /)\n", - " Return x**y (x to the power of y).\n", - " \n", - " prod(iterable, /, *, start=1)\n", - " Calculate the product of all the elements in the input iterable.\n", - " \n", - " The default start value for the product is 1.\n", - " \n", - " When the iterable is empty, return the start value. This function is\n", - " intended specifically for use with numeric values and may reject\n", - " non-numeric types.\n", - " \n", - " radians(x, /)\n", - " Convert angle x from degrees to radians.\n", - " \n", - " remainder(x, y, /)\n", - " Difference between x and the closest integer multiple of y.\n", - " \n", - " Return x - n*y where n*y is the closest integer multiple of y.\n", - " In the case where x is exactly halfway between two multiples of\n", - " y, the nearest even value of n is used. The result is always exact.\n", - " \n", - " sin(x, /)\n", - " Return the sine of x (measured in radians).\n", - " \n", - " sinh(x, /)\n", - " Return the hyperbolic sine of x.\n", - " \n", - " sqrt(x, /)\n", - " Return the square root of x.\n", - " \n", - " tan(x, /)\n", - " Return the tangent of x (measured in radians).\n", - " \n", - " tanh(x, /)\n", - " Return the hyperbolic tangent of x.\n", - " \n", - " trunc(x, /)\n", - " Truncates the Real x to the nearest Integral toward 0.\n", - " \n", - " Uses the __trunc__ magic method.\n", - " \n", - " ulp(x, /)\n", - " Return the value of the least significant bit of the float x.\n", - "\n", - "DATA\n", - " e = 2.718281828459045\n", - " inf = inf\n", - " nan = nan\n", - " pi = 3.141592653589793\n", - " tau = 6.283185307179586\n", - "\n", - "FILE\n", - " /Users/msyamkumar/opt/anaconda3/lib/python3.9/lib-dynload/math.cpython-39-darwin.so\n", - "\n", - "\n" - ] - } - ], - "source": [ - "# Let's get help\n", - "help(math)" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], - "source": [ - "from random import randint" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "9\n" - ] - } - ], - "source": [ - "print(randint(1,10)) # correct invocation" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'random' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/var/folders/k6/kcy8b4f57hx9f1wh4sbs8mn40000gn/T/ipykernel_42489/3716947679.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[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# incorrect invocation\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;31mNameError\u001b[0m: name 'random' is not defined" - ] - } - ], - "source": [ - "print(random.randint(1,10)) # incorrect invocation" - ] - } - ], - "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-05/.ipynb_checkpoints/lec_05_Using_Functions_template-checkpoint.ipynb b/f22/meena_lec_notes/lec-05/.ipynb_checkpoints/lec_05_Using_Functions_template-checkpoint.ipynb deleted file mode 100644 index daa131c..0000000 --- a/f22/meena_lec_notes/lec-05/.ipynb_checkpoints/lec_05_Using_Functions_template-checkpoint.ipynb +++ /dev/null @@ -1,552 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Using Functions" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Review" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Valid variable names" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# this is a possible quiz / exam question\n", - "# self-check: decide if the variables are valid in Python:\n", - "\n", - "#my.name = \"Meena\" \n", - "#1st_place = \"Andy\" \n", - "#stop&go = False \n", - "#type = 100 \n", - "#end_game = True " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Short-circuiting with `and`, `or`\n", - "\n", - "- if the first half of an AND is False, skip the second half for evaluation\n", - "- if the first half of an OR is True, skip the second half for evaluation\n", - "\n", - "Predict the output of each expression and then uncomment and run the expression, to validate." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#7+3 == 9 and 4/0 == 0 \n", - "#7 + 3 == 10 or 4/0 == 0 \n", - "#4/0 == 0 and 7+3 == 9 \n", - "#4/0 == 0 or 7+3 == 10 \n", - "#4+5 == 9 or 5**10000000 < 10000000 " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Learning Objectives\n", - "\n", - "- Call `input()` to accept and process string input\n", - "- Call type cast functions to convert to appropriate type: `int()`, `bool()`, `float()`, `str()`\n", - " - Concatenate values onto strings within a print function by converting to `str()`\n", - "- Using correct vocabulary, explain a function call\n", - " - call/invoke, parameter, argument, keyword arguments, return value\n", - "- Use `sep`, `end` keyword arguments with the print() function\n", - "- Yse some common built-in functions such as round(), abs()\n", - "- import functions from a built-in module using `import`, `from`\n", - "- Call functions in modules using the attribute operator `.`" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Vocabulary\n", - "\n", - "- function definition: a grouping of lines of code; a way for us to tell our program to run that entire group of code\n", - "- call / invoke: a statement in Python code that instructs the program to run all the lines of code in a function definition, and then come back afterward\n", - "- parameter: variable that receives input to function\n", - "- argument: value sent to a function (lines up with parameter)\n", - "- keyword argument: argument explicitly tied to a parameter\n", - "- return value: function output sent back to calling code\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Recall print(...), type(...) functions" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(\"hello\\nworld\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "type(1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "You could either display the return value of a function into an output box or capture it into a variable." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "t = type(1 == 1.0)\n", - "print(t)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "One function's return value can become another function's argument directly." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(type(1 == 1.0))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## input()\n", - "- accepts user input\n", - "- argument to input() function is a prompt\n", - " - prompt provides meaningful information to the user\n", - "- return value of input() function is always of type str" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Example 1: Accept user input for name and say hello." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#TODO: call input function \n", - "\n", - "#TODO: print type of the variable name\n", - "\n", - "print(\"Hello \" + name + \"!\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Example 2a: Accept user input for age (incorrect version)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "age = input(\"Enter your age: \")\n", - "#TODO: print type of the variable age\n", - "\n", - "# Let's celebrate a birthday :)\n", - "age ??? 1 # What is wrong?\n", - "print(age)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Type casting functions\n", - "- convert one type into another type\n", - "- int(), float(), str(), bool()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Let's try these int conversions\n", - "print(int(32.5))\n", - "print(int(\"2012\") + 10)\n", - "# Some conversions don't make sense to us\n", - "# So they won't make sense to Python either\n", - "print(int(\"nineteen\"))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(int(32.5)) # this works\n", - "print(int(float(\"32.5\"))) # this works\n", - "\n", - "# this doesn't work, less intuitive (tricky case!)\n", - "print(int(\"32.5\"))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Let's try these float conversions\n", - "print(float(26))\n", - "print(float(\"3.14\") * 2)\n", - "# Some conversions don't make sense to us\n", - "# So they won't make sense to Python either\n", - "print(float(\"three point one\"))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Let's try these str conversions\n", - "year = 2022\n", - "print(\"Next year will be: \" + year+1)\n", - "print(\"It is good to be: \" + str(True))\n", - "print(\"Who wants a pi? \" + str(3.14))\n", - "#Pretty much you can convert anything to string in Python" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Example 2b: Accept user input for age (correct version)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "age = input(\"Enter your age: \")\n", - "#TODO: convert age to int type\n", - "\n", - "# Let's celebrate a birthday :)\n", - "age += 1 \n", - "print(age)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Example 3: Accept user input for temperature" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# To enter their day care, kids have their temp checked\n", - "temp = float(input(\"Enter your temperature: \"))\n", - "#TODO: convert temp to float type\n", - "temp = ???\n", - "#TODO: check that temp is less than equal to 98.6\n", - "temp_ok = ???\n", - "#TODO: print type of temp_ok\n", - "\n", - "print(\"OK to enter: \" + temp_ok) # What's wrong?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# TODO: figure out how this works\n", - "print(\"Somebody had a birthday: \" + name + \" \" + str(age) + \" years old :)\")\n", - "# TODO: What error will you get when you take out str(...) around age?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### parameters for print(...) function\n", - "- can accept multiple arguments:\n", - " - all arguments get printed on the same line\n", - "- `sep` allows you to configure separator between the arguments:\n", - " - default value: space `\" \"`\n", - "- `end` allows you to configure what character gets printed after\n", - " - default value: newline `\"\\n\"`" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(\"hello\", \"world\")\n", - "print(\"hello\", \"cs220/cs319\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Guess what will get printed\n", - "print(\"hello\", \"world\", sep = \"|\", end = \";\\n\")\n", - "print(\"hello\", \"meena\", sep = \"^\", end = \"...\\n\")\n", - "\n", - "print(\"*\" * 4, \"#\" * 6, sep = \"||\", end = \"<END>\")\n", - "print(\"*\" * 6, \"#\" * 8, sep = \"||\", end = \"<END>\")\n", - "\n", - "print(\"\\n\", end = \"\")\n", - "\n", - "print(\"*\" * 4, \"#\" * 6, sep = \"||\", end = \"<END>\\n\")\n", - "print(\"*\" * 6, \"#\" * 8, sep = \"||\", end = \"<END>\\n\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## More built-in functions\n", - "- print(), type(), int(), float(), str(), bool() are all examples of built-in functions\n", - "- built-in functions are functions you can use readily (without importing anything)\n", - "- you definitely don't have to know all the built-in functions, but here is the list: https://docs.python.org/3/library/functions.html" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "abs function" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Guess what will get printed\n", - "print(abs(-10))\n", - "print(abs(10))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "round function" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Guess what will get printed\n", - "print(round(10.525252, 4))\n", - "print(round(5.2953, 2))\n", - "print(round(5.2423, 2))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Modules\n", - "- collection of similar functions and variables\n", - "- requires import\n", - "- `import`, `from` keywords\n", - "- 2 styles of import:\n", - " 1. `import` module_name: \n", - " - need to specify module_name`.`function_name to call it. \n", - " - `.` is called attribute operator.\n", - " 2. `from` module_name `import` function_name\n", - " - can call the function without specifying module name\n", - "- `help` module_name:\n", - " - documentation for all functions and variables inside a module" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Let's try to find square root of 10\n", - "sqrt(10) # doesn't work because it is part of math module" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "It is a good practice to include all your import statements at the top of your notebook file. We will make exception just for a couple of lectures." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import math" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "math.sqrt(10)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "math.cos(3.14159)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Modules contain useful variables too." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "math.pi" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Let's get help\n", - "help(math)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from random import randint" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(randint(1,10)) # correct invocation" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(random.randint(1,10)) # incorrect invocation" - ] - } - ], - "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 -} -- GitLab