diff --git a/f24/Louis_Lecture_Notes/05_Using_Functions/Lec_05_Using_Functions.ipynb b/f24/Louis_Lecture_Notes/05_Using_Functions/Lec_05_Using_Functions.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..dd80a05de9fc9f36decd117bda94735a687cc6b4 --- /dev/null +++ b/f24/Louis_Lecture_Notes/05_Using_Functions/Lec_05_Using_Functions.ipynb @@ -0,0 +1,490 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Warmup" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 1 \n", + "# Create 3 variables.\n", + "# Assign each one a data value of a different type.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 2\n", + "# Use the debugger to view these three variables\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 3\n", + "# Write expressions that meet the following criteria:\n", + "\n", + "# exp 1 -- use the \"or\" operator that prevents division by zero\n", + "# using short-circuiting\n", + "\n", + "# exp 2 -- use the \"and\" operator that prevents division by zero\n", + "# using short-circuiting\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 4\n", + "\n", + "# For each of the following commented-out lines of code,\n", + "# uncomment only those that use allowed variable names\n", + "# For the ones you leave commented-out, state why it is not allowed\n", + "\n", + "# fungi = \"morel\"\n", + "\n", + "# age@middle_school = 12\n", + "\n", + "# ugly.duckling = True\n", + "\n", + "# bucky-badger = \"Winner\"\n", + "\n", + "# true = \"Happy\"\n", + "\n", + "# is_detectable = fungi == \"morel\" and not 3 <= 2.1\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Reminder\n", + "\n", + "* a **literal values** are when you type the value yourself (e.g. `\"hello\"`, `3.14`, `True`)\n", + "* a **variable** is a named memory location that stores a value that can change as the program runs -- know the rules for variable names (also use meaninful names)\n", + "* An **assignment statement** is of the form `variable = expression`\n", + "* **Errors** can be divided into three categories:\n", + " * Syntax errors\n", + " * Runtime errors\n", + " * Semantic errors\n", + "* **Jupyterlab features**: use debugger to view kernel state, ways of running Python code, shortcuts" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Python Variable Names\n", + "*Python* requires that variables...\n", + " - Only use letters a-z (upper and lower), numbers, and underscores.\n", + " - Don’t start with a number\n", + " - Don’t use Python keywords (e.g., `and`, `False`, etc)\n", + " \n", + "In addition, *CS220 Course Staff* ask that variables...\n", + " - Only use the English alphabet and numbers\n", + " - Use snake_case for variable names (as opposed to camelCase or PascalCase)\n", + " - Make variable names *meaningful*\n", + " \n", + "Python won't run for the first set of rules; CS220 course staff will take off points for the second set of rules." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 2\n", + "# Decide if the following variables are valid in Python.\n", + "# Write VALID or INVALID next to them.\n", + "\n", + "# my.name = \"Meena\"\n", + "# 1st_place = \"Andy\"\n", + "# stop&go = False \n", + "# False = 100 \n", + "# end_game = True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 3\n", + "print(True and not False) # Write your prediction here!\n", + "print(type((5 // 1.2))) # Write your prediction here!\n", + "print(1 <= 2 and \"a\" < \"A\") # Write your prediction here!\n", + "print(1 <= 2 or \"a\" < \"A\") # Write your prediction here!\n", + "print(len(\"2\") + 1) # Write your prediction here!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 4\n", + "\n", + "# Calculate how old a dog is in dog years given the year they were born\n", + "# 1 year is equal to 7 dog years!\n", + "# e.g. a dog born in 2019 is 28 years old (7 * 4)\n", + "\n", + "year_born = 2017\n", + "dog_years = ???\n", + "print(\"Your dog is \" + str(dog_years) + \" years old!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 5\n", + "\n", + "# Given the number of seconds, compute the number of hours, minutes, and seconds.\n", + "# e.g. if seconds is 65, the correct output (with \\n in-between) is 0 1 5\n", + "# e.g. if seconds is 192, the correct output (with \\n in-between) is 0 3 12\n", + "# e.g. if seconds is 3739, the correct output (with \\n in-between) is 1 2 19\n", + "\n", + "seconds = 3739\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# CS220: Lecture 05\n", + "\n", + "\n", + "## 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 (named) arguments, return value\n", + "- Use `sep`, `end` keyword arguments with the print() function\n", + "- Use 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", + "| Term | Definition |\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 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 (named) argument | argument explicitly tied to a parameter |\n", + "| return value | function output sent back to calling code |" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Previously Used Functions.\n", + "We have used functions before! Namely `print`, `type`, and `len`.\n", + "\n", + "What parameters did they have; what arguments did we send them?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"hello world\")\n", + "print(len(\"apples\"))\n", + "print(type(1 < 2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's take a closer look at the `print` function.\n", + " 1. Use the [official Python reference](https://docs.python.org/3/library/functions.html#print)\n", + " 2. Use the [w3schools reference](https://www.w3schools.com/python/ref_func_print.asp)\n", + " 3. Use the `help` function" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "help(print)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"1\", \"two\", \"3!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Can you add a keyword argument that will connect the names with the word 'and'?\n", + "print('Bob', 'Alice', 'Dylan', 'Gretchen')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Can you add a keyword argument that will make the output appear on a single line?\n", + "print('Good')\n", + "print('Morning!')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Guess what will get printed\n", + "print(\"hello\", \"world\", sep = \"|\", end = \";\\n\")\n", + "print(\"its\", \"so\", \"cold\", sep = \"^\", end = \"...\\n\")\n", + "\n", + "print(\"*\" * 4, \"#\" * 6, sep = \"\\t\", end = \"<END>\")\n", + "print(\"*\" * 6, \"#\" * 8, sep = \"||\", end = \"<END>\")\n", + "\n", + "print(\"\\n\", end = \"\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Type Conversion Functions\n", + "\n", + "| Function | Purpose |\n", + "| :- | :- |\n", + "| `int(value)` | Turns `value` into an integer |\n", + "| `float(value)` | Turns `value` into a float |\n", + "| `str(value)` | Turns `value` into a string |\n", + "| `bool(value)` | Turns `value` into a boolean |" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# This code works!\n", + "name = input(\"Enter your name: \")\n", + "print(type(name))\n", + "print(\"Hello \" + name + \"!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# This code doesn't work! Try to fix it.\n", + "age = input(\"Enter your age: \")\n", + "print(type(age))\n", + "age = age + 10 # we can shorten this to age += 10\n", + "print(\"In 10 years, you will be \" + str(age))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# This code doesn't work! Try to fix it.\n", + "temp = input(\"Enter your temperature: \")\n", + "print(type(temp))\n", + "temp_ok = temp < 101.1\n", + "print(\"You can enter lab: \" + str(temp_ok))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Other Built-In Functions\n", + "We can refer to [this resource](https://www.w3schools.com/python/python_ref_functions.asp) to see other built-in functions in Python. Let's practice using `round` and `abs`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Print the help documentation for the round function\n", + "\n", + "# Print the help documentation for the abs function\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Round the number 84.7\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Round the number 19.74812 to the second decimal place\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Get the absolute value of -12\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Get the square root of 81\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Importing From Other Modules\n", + "We'll look at two other common modules (collections of functions): `math` and `random`.\n", + "\n", + "There are a few ways we can do this...\n", + " - We can write `import <module_name>` and use a function by `<module_name>.<function_name>`.\n", + " - We can write `from <module_name> import <function_name>` and use a function(s) directly by its name.\n", + " - We can write `from <module_name> import *` and directly use any function by name." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Normally we like to have our imports at the top of our notebook\n", + "import math\n", + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Use the . (attribute accessor)\n", + "math.sqrt(17)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import the function directly\n", + "from math import sqrt\n", + "print(sqrt(17))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from random import randint\n", + "print(randint(1,10))\n", + "\n", + "# wrong way to call randint\n", + "#print(random.randint(1,10))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import all functions directly\n", + "from math import *" + ] + } + ], + "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.10.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/f24/Louis_Lecture_Notes/05_Using_Functions/Lec_05_Using_Functions_Solution_Oliphant.ipynb b/f24/Louis_Lecture_Notes/05_Using_Functions/Lec_05_Using_Functions_Solution_Oliphant.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..5c7e8c0f437442317996959c130d837a32e5dd50 --- /dev/null +++ b/f24/Louis_Lecture_Notes/05_Using_Functions/Lec_05_Using_Functions_Solution_Oliphant.ipynb @@ -0,0 +1,696 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 1 \n", + "# Create 3 variables.\n", + "# Assign each one a data value of a different type.\n", + "my_cool_name = \"Jon\"\n", + "my_fav_num = 8\n", + "pi = 3.14" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Python Variable Names\n", + "*Python* requires that variables...\n", + " - Only use letters a-z (upper and lower), numbers, and underscores.\n", + " - Don’t start with a number\n", + " - Don’t use Python keywords (e.g., `and`, `False`, etc)\n", + " \n", + "In addition, *CS220 Course Staff* ask that variables...\n", + " - Only use the English alphabet and numbers\n", + " - Use snake_case for variable names (as opposed to camelCase or PascalCase)\n", + " - Make variable names *meaningful*\n", + " \n", + "Python won't run for the first set of rules; CS220 course staff will take off points for the second set of rules." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 2\n", + "# Decide if the following variables are valid in Python.\n", + "# Write VALID or INVALID next to them.\n", + "\n", + "# my.name = \"Meena\" #INVALID\n", + "# 1st_place = \"Andy\" #INVALID\n", + "# stop&go = False #INVALID \n", + "# False = 100 #INVALID \n", + "# end_game = True #VALID\n", + "# endGame = True #VALID But doesn't follow the rules of CS220 (use snake case, not camel case)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "<class 'float'>\n", + "False\n", + "True\n", + "2\n" + ] + } + ], + "source": [ + "# Warmup 3\n", + "print(True and not False) # True\n", + "print(type((5 // 1.2))) # float\n", + "print(1 <= 2 and \"a\" < \"A\") # False\n", + "print(1 <= 2 or \"a\" < \"A\") # True\n", + "print(len(\"2\") + 1) # 2" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your dog is 49 years old!\n" + ] + } + ], + "source": [ + "# Warmup 4\n", + "\n", + "# Calculate how old a dog is in dog years given the year they were born\n", + "# 1 year is equal to 7 dog years!\n", + "# e.g. a dog born in 2019 is 28 years old (7 * 4)\n", + "\n", + "year_born = 2017\n", + "dog_years = (2024 - year_born) * 7\n", + "print(\"Your dog is \" + str(dog_years) + \" years old!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "19\n" + ] + } + ], + "source": [ + "# Warmup 5\n", + "\n", + "# Given the number of seconds, compute the number of hours, minutes, and seconds.\n", + "# e.g. if seconds is 65, the correct output (with \\n in-between) is 0 1 5\n", + "# e.g. if seconds is 192, the correct output (with \\n in-between) is 0 3 12\n", + "# e.g. if seconds is 3739, the correct output (with \\n in-between) is 1 2 19\n", + "\n", + "seconds = 3739\n", + "\n", + "# First, we have to get the number of hours.\n", + "# There are 60 * 60 = 3600 seconds in an hour, so\n", + "# we use integer division to get th ewhole number of hours\n", + "\n", + "hours = seconds // 3600\n", + "\n", + "# What we have left over is the remainder of seconds.\n", + "# We use the modulus operator to get the remainder\n", + "\n", + "seconds = seconds % 3600\n", + "\n", + "# Same thing for minutes!\n", + "# We use integer division to get the whole number of minutes\n", + "\n", + "minutes = seconds // 60\n", + "\n", + "# What we have left over is the remainder of seconds.\n", + "\n", + "seconds = seconds % 60\n", + "\n", + "# Print out the results\n", + "print(hours)\n", + "print(minutes)\n", + "print(seconds)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# CS220: Lecture 05\n", + "\n", + "\n", + "## 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 (named) arguments, return value\n", + "- Use `sep`, `end` keyword arguments with the print() function\n", + "- Use 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", + "| Term | Definition |\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 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 (named) argument | argument explicitly tied to a parameter |\n", + "| return value | function output sent back to calling code |" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Previously Used Functions.\n", + "We have used functions before! Namely `print`, `type`, and `len`.\n", + "\n", + "What parameters did they have; what arguments did we send them?" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world\n", + "6\n", + "<class 'bool'>\n" + ] + } + ], + "source": [ + "print(\"hello world\")\n", + "print(len(\"apples\"))\n", + "print(type(1 < 2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's take a closer look at the `print` function.\n", + " 1. Use the [official Python reference](https://docs.python.org/3/library/functions.html#print)\n", + " 2. Use the [w3schools reference](https://www.w3schools.com/python/ref_func_print.asp)\n", + " 3. Use the `help` function" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function print in module builtins:\n", + "\n", + "print(*args, sep=' ', end='\\n', file=None, flush=False)\n", + " Prints the values to a stream, or to sys.stdout by default.\n", + " \n", + " sep\n", + " string inserted between values, default a space.\n", + " end\n", + " string appended after the last value, default a newline.\n", + " file\n", + " a file-like object (stream); defaults to the current sys.stdout.\n", + " flush\n", + " whether to forcibly flush the stream.\n", + "\n" + ] + } + ], + "source": [ + "help(print)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 two 3!\n" + ] + } + ], + "source": [ + "print(\"1\", \"two\", \"3!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Bob and Alice and Dylan and Gretchen\n" + ] + } + ], + "source": [ + "# Can you add a keyword argument that will connect the names with the word 'and'?\n", + "print('Bob', 'Alice', 'Dylan', 'Gretchen',sep=' and ')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Good Morning!\n" + ] + } + ], + "source": [ + "# Can you add a keyword argument that will make the output appear on a single line?\n", + "print('Good',end=\" \")\n", + "print('Morning!')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello|world;\n", + "its^so^cold...\n", + "****\t######<END>******||########<END>\n" + ] + } + ], + "source": [ + "# Guess what will get printed\n", + "print(\"hello\", \"world\", sep = \"|\", end = \";\\n\") # hello|world;\n", + "print(\"its\", \"so\", \"cold\", sep = \"^\", end = \"...\\n\") # its^so^cold...\n", + "\n", + "print(\"*\" * 4, \"#\" * 6, sep = \"\\t\", end = \"<END>\") # ****\t######<END>\n", + "print(\"*\" * 6, \"#\" * 8, sep = \"||\", end = \"<END>\") # ******||########<END>\n", + "\n", + "print(\"\\n\", end = \"\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Type Conversion Functions\n", + "\n", + "| Function | Purpose |\n", + "| :- | :- |\n", + "| `int(value)` | Turns `value` into an integer |\n", + "| `float(value)` | Turns `value` into a float |\n", + "| `str(value)` | Turns `value` into a string |\n", + "| `bool(value)` | Turns `value` into a boolean |" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter your name: louis\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'str'>\n", + "Hello louis!\n" + ] + } + ], + "source": [ + "# This code works!\n", + "name = input(\"Enter your name: \")\n", + "print(type(name))\n", + "print(\"Hello \" + name + \"!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter your age: 53\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'str'>\n", + "In 10 years, you will be 63\n" + ] + } + ], + "source": [ + "# This code doesn't work! Try to fix it.\n", + "age = input(\"Enter your age: \")\n", + "print(type(age))\n", + "age = int(age) + 10 # we can shorten this to age += 10\n", + "print(\"In 10 years, you will be \" + str(age))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter your temperature: 87\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<class 'str'>\n", + "You can enter lab: True\n" + ] + } + ], + "source": [ + "# This code doesn't work! Try to fix it.\n", + "temp = input(\"Enter your temperature: \")\n", + "print(type(temp))\n", + "temp_ok = float(temp) < 101.1\n", + "print(\"You can enter lab: \" + str(temp_ok))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Other Built-In Functions\n", + "We can refer to [this resource](https://www.w3schools.com/python/python_ref_functions.asp) to see other built-in functions in Python. Let's practice using `round` and `abs`." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function round in module builtins:\n", + "\n", + "round(number, ndigits=None)\n", + " Round a number to a given precision in decimal digits.\n", + " \n", + " The return value is an integer if ndigits is omitted or None. Otherwise\n", + " the return value has the same type as the number. ndigits may be negative.\n", + "\n", + "None\n", + "Help on built-in function abs in module builtins:\n", + "\n", + "abs(x, /)\n", + " Return the absolute value of the argument.\n", + "\n", + "None\n" + ] + } + ], + "source": [ + "# Print the help documentation for the round function\n", + "print(help(round))\n", + "# Print the help documentation for the abs function\n", + "print(help(abs))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "85" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Round the number 84.7\n", + "round(84.7)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "19.75" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Round the number 19.74812 to the second decimal place\n", + "round(19.74812,ndigits=2)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Get the absolute value of -12\n", + "abs(-12)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9.0" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Get the square root of 81\n", + "pow(81,0.5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Importing From Other Modules\n", + "We'll look at two other common modules (collections of functions): `math` and `random`.\n", + "\n", + "There are a few ways we can do this...\n", + " - We can write `import <module_name>` and use a function by `<module_name>.<function_name>`.\n", + " - We can write `from <module_name> import <function_name>` and use a function(s) directly by its name.\n", + " - We can write `from <module_name> import *` and directly use any function by name." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "# Normally we like to have our imports at the top of our notebook\n", + "import math\n", + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4.123105625617661" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Use the . (attribute accessor)\n", + "math.sqrt(17)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4.123105625617661\n" + ] + } + ], + "source": [ + "# Import the function directly\n", + "from math import sqrt\n", + "print(sqrt(17))" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n" + ] + } + ], + "source": [ + "from random import randint\n", + "print(randint(1,10))\n", + "\n", + "# wrong way to call randint\n", + "#print(random.randint(1,10))" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.0\n", + "-1.0\n" + ] + } + ], + "source": [ + "# Import all functions directly\n", + "from math import *\n", + "print(sin(0))\n", + "print(cos(pi))" + ] + } + ], + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}