Added Midterm, Week 8 and Week 9 Files
++ Midterm ++ Week 8 Files ++ Week 9 Files
This commit is contained in:
parent
f981b3aa3b
commit
eb2e0fedd3
4 changed files with 3483 additions and 0 deletions
242
Week-8/Module_06_mini_homework.ipynb
Normal file
242
Week-8/Module_06_mini_homework.ipynb
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"!dlroW olleH\n",
|
||||
".noitisop yb retemarap a gnissap fo elpmaxe na si sihT\n",
|
||||
".eman yb retemarap a gnissap fo elpmaxe na si sihT\n",
|
||||
"[5, 4, 3, 2, 1]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# This is an example of how to set up a function. It includes the \"def\" keyword, a name, and a \n",
|
||||
"# parameter with a default value. This function simply prints the parameter in reverse.\n",
|
||||
"def example_function(input_value='Hello World!'):\n",
|
||||
" # Syntax for reversing a list\n",
|
||||
" print(input_value[::-1])\n",
|
||||
" \n",
|
||||
"# Note that if we don't supply a value for the parameter, the function uses the default value.\n",
|
||||
"example_function()\n",
|
||||
"\n",
|
||||
"# By default, parameters are assigned in the same order that they are specified.\n",
|
||||
"example_function(\"This is an example of passing a parameter by position.\")\n",
|
||||
"\n",
|
||||
"# We can also specify the name of the variable to which we want to assign a value.\n",
|
||||
"example_function(input_value=\"This is an example of passing a parameter by name.\")\n",
|
||||
"\n",
|
||||
"# Note that we don't have to stick with any particular data type here, but the success of our function depends\n",
|
||||
"# on passing an iterable / subscriptable type like a list or a string.\n",
|
||||
"example_function([1,2,3,4,5])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Unlike the function above, this function uses the \"return\" keyword. The return keyword\n",
|
||||
"# ends the function by returning a value, and nothing within the same conditional branch can occur\n",
|
||||
"# after a return statement.\n",
|
||||
"\n",
|
||||
"def function_with_output():\n",
|
||||
" return 42"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"!dlroW olleH\n",
|
||||
"42\n",
|
||||
"None\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Calling two functions and assigning the output to variables a and b.\n",
|
||||
"a = function_with_output()\n",
|
||||
"b = example_function()\n",
|
||||
"\n",
|
||||
"# Printing the output of the two functions. Since the first function has output (has a return statement),\n",
|
||||
"# it is stored in the variable. The second function doesn't have a return statement, so its return value / output\n",
|
||||
"# is simply \"None.\"\n",
|
||||
"print(a)\n",
|
||||
"print(b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "'int' object is not subscriptable",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"Input \u001b[1;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# We get an error here! Explain what's going wrong by editing the text cell below.\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mexample_function\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\n",
|
||||
"Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36mexample_function\u001b[1;34m(input_value)\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexample_function\u001b[39m(input_value\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mHello World!\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Syntax for reversing a list\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43minput_value\u001b[49m\u001b[43m[\u001b[49m\u001b[43m:\u001b[49m\u001b[43m:\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m)\n",
|
||||
"\u001b[1;31mTypeError\u001b[0m: 'int' object is not subscriptable"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# We get an error here! Explain what's going wrong by editing the text cell below.\n",
|
||||
"example_function(2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question \\#1**\n",
|
||||
"\n",
|
||||
"Explain why we get an error in the cell above.\n",
|
||||
"\n",
|
||||
"Integers are not a compatible type here, whatever you pass to this function must be traversable, aka anything that can use [] and return some value from doing that."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question \\#2**\n",
|
||||
"\n",
|
||||
"Defining a function and calling a function are very different actions. Explain the difference.\n",
|
||||
"\n",
|
||||
"Defining the function is like a recipe, you have steps, but no ingredients. Calling the function is like using that recipe that you have written, you give it ingredients, and follow the steps, and the output is your finished recipe."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question \\#3**\n",
|
||||
"\n",
|
||||
"Complete the function definition below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def sum_of_two_numbers(num1, num2):\n",
|
||||
" return num1 + num2\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question \\#4**\n",
|
||||
"\n",
|
||||
"Call the sum_of_two_numbers function that you defined."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"428"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Your code here, summing 42 and 386.\n",
|
||||
"output = sum_of_two_numbers(42, 386)\n",
|
||||
"output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question \\#5**\n",
|
||||
"\n",
|
||||
"I'm trying to calculate the product of a list, but I can't seem to get any output from the function!\n",
|
||||
"What am I doing wrong?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"None\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def prod(input_list):\n",
|
||||
" output = 1\n",
|
||||
" # This is a loop! It walks through an iterable data type like a list such that \"item\" takes on each value\n",
|
||||
" # in whatever iterable is provided. This is shorthand for saying item = input_list[0], performing a calculation,\n",
|
||||
" # then item = input_list[1], performing a calculation, etc. Keep in mind that any operation within the indented\n",
|
||||
" # block will be repeated every time we iterate through the for loop.\n",
|
||||
" for item in input_list:\n",
|
||||
" output = output * item\n",
|
||||
" \n",
|
||||
"print(prod([1,2,3,4,5]))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Since nothing from this function is returned, it automatically returns a None type, a default python type. You would beed to return the list if you pass it in like this, otherwise, defining the list outside of the function, then giving it the list and looking at the list afterward would give you the output as well, since lists are mutable types, you wouldn't need to return anything from the function. Otherwise, returning the values would give you the correct values."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue