Added Midterm, Week 8 and Week 9 Files

++ Midterm
++ Week 8 Files
++ Week 9 Files
This commit is contained in:
Charles Descamps 2022-10-27 11:42:22 -07:00
parent f981b3aa3b
commit eb2e0fedd3
4 changed files with 3483 additions and 0 deletions

File diff suppressed because one or more lines are too long

2725
Midterm/class_list.csv Normal file

File diff suppressed because it is too large Load diff

View 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
}

View file

@ -0,0 +1,359 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Remember to import any libraries that you need!\n",
"from datascience import *\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The condition is true!\n",
"Math still works\n"
]
}
],
"source": [
"# This is an \"if statement,\" or \"conditional statement,\" and the logic is only evaluated if the condition is true\n",
"some_condition = True\n",
"if some_condition:\n",
" print(\"The condition is true!\")\n",
" \n",
"# You can also test the inverse of the condition using the \"not\" keyword\n",
"if not some_condition:\n",
" print(\"The condition is false!\")\n",
" \n",
"# There's no need to assign the condition to a variable! Python will evaluate your expressions for you\n",
"# Remember how '==' returns a boolean value?\n",
"if 1 == 1:\n",
" print(\"Math still works\")\n",
"if 1 == 2:\n",
" print(\"Math is broken\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"One condition is true!\n",
"One condition is true!\n"
]
}
],
"source": [
"# You can chain conditions together in an if statement with \"and\" and \"or\"\n",
"some_condition = True\n",
"some_other_condition = False\n",
"\n",
"# This tests that 'both' conditions are true\n",
"if some_condition and some_other_condition:\n",
" print(\"Both conditions are true!\")\n",
"\n",
"# This tests that 'at least' one condition is true, but also succeeds if 'both' values are true\n",
"if some_condition or some_other_condition:\n",
" print(\"One condition is true!\")\n",
"\n",
"\n",
" \n",
" \n",
"# Instead of writing a brand new \"if statement\" every time we have a new condition, we can\n",
"# chain them together using \"if, elif, else\". The rules are that for each \"conditional\" statement:\n",
"# You must have exactly 1 \"if\" case\n",
"# You may have 0 to infinite \"elif\" case\n",
"# You may have 0 or one \"else\" cases\n",
"\n",
"# An \"if\" is your basic use case. If the condition is true, the logic will be evaluated.\n",
"# An \"elif\" (short for else if) will only be tested if the base \"if\" case was false and if each of the preceeding\n",
"# \"elif\"s were false\n",
"# An \"else\" statement contains logic for \"if all other cases fail.\"\n",
"if some_condition and some_other_condition:\n",
" print(\"Both conditions are true!\")\n",
"elif some_condition or some_other_condition:\n",
" print(\"One condition is true!\")\n",
"else:\n",
" print(\"Neither condition is true!\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4] evaluates to True!\n",
"[] evaluates to False!\n",
"0 evaluates to False!\n",
"1 evaluates to True!\n",
"Some string value evaluates to True!\n",
" evaluates to False!\n",
"[0] evaluates to True!\n"
]
}
],
"source": [
"# Python is a little bit weird, and everything has either a True or False value assigned to it.\n",
"# These refer to the \"truthiness\" or \"falsiness\" of the value. Test a few of them out!\n",
"\n",
"truthiness_test_vals = [[1,2,3,4],[],0,1, \"Some string value\", \"\", [0]]\n",
"\n",
"for value in truthiness_test_vals:\n",
" if value:\n",
" print(f'{str(value)} evaluates to True!')\n",
" else:\n",
" print(f'{str(value)} evaluates to False!')\n",
" \n",
"# As a general rule, if a variable contains a non-zero or non-empty value, it is true."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([8, 7, 9, 0, 2, 5, 3, 0, 4, 6])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# From your reading, you'll see that this generates 10 random integers between 0 (inclusive) and 10 (exclusive)\n",
"random_ints = np.random.choice(10,10)\n",
"random_ints"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.39315903, 0.86328183, 0.65969407, 0.97453189, 0.92070023,\n",
" 0.36266504, 0.49156882, 0.20067649, 0.33757991, 0.76892529])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Similarly, this generates random floats between 0 and 1. \n",
"# The parameter dictates the number of random floats you get back\n",
"random_floats = np.random.random(10)\n",
"random_floats"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"44\n",
"Some high, some low\n"
]
}
],
"source": [
"print(sum(random_ints))\n",
"if sum(random_ints) < 30:\n",
" print(\"Mostly low numbers\")\n",
"elif sum(random_ints) >= 30 and sum(random_ints) <60:\n",
" print(\"Some high, some low\")\n",
"else:\n",
" print(\"Mostly high numbers\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Question 1:\n",
"Create two variables, (first_num and second_num), and assign them random values (ints or floats -- it doesn't matter). Use if statements to compare the values and output a message that indicates the largest value.\n",
"\n",
"I.e. if first_num == 10 and second_num = 1, you should get a message like \"first number is greater!\""
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"first number: 65 is greater than second number: -85\n"
]
}
],
"source": [
"# create a first and second number variable, let it be within range of -100 to 99.\n",
"first_num = np.random.choice(np.arange(-100, 100))\n",
"second_num = np.random.choice(np.arange(-100, 100))\n",
"# using if statements and formatted strings, check to see which number is greate\n",
"if first_num > second_num:\n",
" print(f\"first number: {first_num} is greater than second number: {second_num}\")\n",
"elif second_num > first_num:\n",
" print(f\"second number: {second_num} is greater than first number: {first_num}\")\n",
"else:\n",
" print(f\"Both numbers {first_num} and {second_num} are equal!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Question 2:\n",
"\n",
"Consider the following code. Even though \"num\" is less than 2, we're still getting the message \"less than 10\" every time. Explain why."
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Less than 10\n"
]
}
],
"source": [
"num = 1\n",
"if num < 10:\n",
" print(\"Less than 10\")\n",
"elif num < 5:\n",
" print(\"Less than 5\")\n",
"elif num < 2:\n",
" print(\"Less than 2\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Whenever any value is entered that is less than 10, only the first if statement executes. It would never get to the 5 or 2 checks, and only display what is in the first if statement. If you reverse the order of the if statements, this would work as intended."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Question 3:\n",
"In your last mini homework (and in this week's reading) we saw a \"for loop,\" which is used to iterate through any iterable item (like a list, string, etc.). Create a list of random floats, then use a for loop to print each value individually"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"value 1 : 0.0409743281636753\n",
"value 2 : 0.6943827044075414\n",
"value 3 : 0.9193827692519987\n",
"value 4 : 0.34529162046253525\n",
"value 5 : 0.5680070954504679\n",
"value 6 : 0.333482737370685\n",
"value 7 : 0.24474460886039917\n",
"value 8 : 0.9479221591217194\n",
"value 9 : 0.9241714541116084\n",
"value 10 : 0.17094329526544638\n",
"value 11 : 0.5130324935270965\n",
"value 12 : 0.920872760969936\n",
"value 13 : 0.15156731317904037\n",
"value 14 : 0.5419109732740256\n",
"value 15 : 0.043521794935426406\n"
]
}
],
"source": [
"# create 15 random float values and store in printed_values\n",
"printed_values = np.random.random(15)\n",
"# using a for loop and formatted strings, list position of value and what is stored there.\n",
"for index in range(len(printed_values)):\n",
" print(f\"value {index+1} : {printed_values[index]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Question 4:\n",
"Perform a simulation in which you roll a die 10 times. Store the output in an array called \"rolls\""
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# Replace the ellipses with your code\n",
"# rolls a six-sided die, 10 times.\n",
"# modify the range to get a different die,\n",
"# modify the second parameter of the random choice function to get any number of rolls.\n",
"rolls = np.random.choice(np.arange(1, 7), 10)"
]
}
],
"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
}