Compare commits
10 commits
6e089958ae
...
eb2e0fedd3
| Author | SHA1 | Date | |
|---|---|---|---|
| eb2e0fedd3 | |||
| f981b3aa3b | |||
| c1239c6afe | |||
| 140ffc6523 | |||
| eb12f00994 | |||
| 1df96d1c77 | |||
| 4fc499169b | |||
| ee3222c4ab | |||
| 0cb4767658 | |||
| 2a22db2605 |
17 changed files with 21758 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
.ipynb_checkpoints
|
||||
157
Midterm/Midterm_Coding_S22.ipynb
Normal file
157
Midterm/Midterm_Coding_S22.ipynb
Normal file
File diff suppressed because one or more lines are too long
2725
Midterm/class_list.csv
Normal file
2725
Midterm/class_list.csv
Normal file
File diff suppressed because it is too large
Load diff
18
README.md
18
README.md
|
|
@ -1,2 +1,20 @@
|
|||
# INF110-Projects
|
||||
A place that I store all the INF110 work that I will do over the coming semester.
|
||||
|
||||
## Cdez's Standards for Committing
|
||||
### Standard 1:
|
||||
Use "++" in the extended description to show what was added, either generally, or specifically.
|
||||
Use "+-" in the extended description to show what was modified/changed, either generally, or specifically.
|
||||
Use "--" in the extended description to show what was removed, either generally, or specifically.
|
||||
### Standard 2:
|
||||
The description will have the biggest changes listed, in the order of biggest to smallest differences.
|
||||
### Standard 3:
|
||||
If committing to main, make sure to have all the above standards in order before committing.
|
||||
### Standard 4:
|
||||
Pull Requests:
|
||||
#### Standard 4.1:
|
||||
Create a new working branch from main. (Or you may use the 'Create a new branch GitWeb function.')
|
||||
#### Standard 4.2:
|
||||
Call the branch the biggest change of the branch, using Standard 1 in it's name with the biggest change/addition/removal.
|
||||
#### Standard 4.3:
|
||||
When done with the branch, merge into main, and make sure to have very detailed descriptions.
|
||||
282
Week-2/01 Pride and Prejudice(1).ipynb
Normal file
282
Week-2/01 Pride and Prejudice(1).ipynb
Normal file
File diff suppressed because one or more lines are too long
4710
Week-2/14111835.omcsettings.txt
Normal file
4710
Week-2/14111835.omcsettings.txt
Normal file
File diff suppressed because it is too large
Load diff
5463
Week-2/64212-0.txt
Normal file
5463
Week-2/64212-0.txt
Normal file
File diff suppressed because it is too large
Load diff
File diff suppressed because one or more lines are too long
6772
Week-2/pg64317.txt
Normal file
6772
Week-2/pg64317.txt
Normal file
File diff suppressed because it is too large
Load diff
95
Week-5/01_basic_names.ipynb
Normal file
95
Week-5/01_basic_names.ipynb
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Names in Python"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** A famous fact in mathematics is that\n",
|
||||
"\n",
|
||||
"$$(1 + \\frac{1}{n})^{n}$$\n",
|
||||
"\n",
|
||||
"gets very close to the number $e$, which is roughly $2.718$, when $n$ is large. Use this fact to compute an approximation to $e$, called `roughly_e`. Your approximation should be correct to at least 5 decimal places."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2.7182820533299275"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"roughly_e = (1 + (1/99999999999))**99999999999\n",
|
||||
"roughly_e"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 2.** In the previous question, you might have noticed that you had to type in the value of $n$ twice when you tried different values (once as `1/n` and once as `**n`). You might have even accidentally used 2 different values.\n",
|
||||
"\n",
|
||||
"Redo the code in the cell below, giving your estimate of $e$ the name `also_roughly_e`. Instead of writing the value of $n$ twice, give it the name `n` in your Python code, and then use that name in your expression for `also_roughly_e`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2.7182820533299275"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"n = 99999999999\n",
|
||||
"also_roughly_e = (1+(1/n))**n\n",
|
||||
"also_roughly_e"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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": 1
|
||||
}
|
||||
75
Week-5/02_confusing_names.ipynb
Normal file
75
Week-5/02_confusing_names.ipynb
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Confusing Names"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** Some physicists and computer scientists were partying late one night, and they decided to figure out roughly how much beer it would take to cover the whole surface of the Sun 3 meters deep. They found the following facts:\n",
|
||||
"\n",
|
||||
"1. The average density (mass per volume) of the Sun is 1410 $\\texttt{kg}/\\texttt{m}^3$.\n",
|
||||
"2. The Sun weighs $1.989 \\times 10^{30} \\texttt{kg}$.\n",
|
||||
"3. The Sun's volume is its weight divided by its density.\n",
|
||||
"4. The Sun is roughly a sphere.\n",
|
||||
"5. The radius of a sphere is $(\\texttt{volume} \\times \\frac{3}{4 \\times \\pi})^{1/3}$\n",
|
||||
"6. $\\pi$ is roughly 3.14159.\n",
|
||||
"7. The surface area of a sphere is $4 \\times \\pi \\times \\texttt{radius}^2$.\n",
|
||||
"\n",
|
||||
"They wrote code in the next cell to compute the answer. But the next morning, they couldn't figure out what their code did, because none of the names made sense. To help clarify their code, replace the names used in the next cell with appropriate names chosen from the following list:\n",
|
||||
"\n",
|
||||
"* `required_beer_volume`\n",
|
||||
"* `sun_radius`\n",
|
||||
"* `sun_volume`\n",
|
||||
"* `sun_surface_area`\n",
|
||||
"* `sun_density`\n",
|
||||
"* `pi`\n",
|
||||
"* `sun_weight`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sun_density = 1410\n",
|
||||
"sun_weight = 1.989 * 10**30\n",
|
||||
"depth = 3\n",
|
||||
"sun_volume = sun_weight / sun_density\n",
|
||||
"pi = 3.14159\n",
|
||||
"sun_radius = (sun_volume * 3/(4*pi))**(1/3)\n",
|
||||
"sun_surface_area = 4 * pi * sun_radius**2\n",
|
||||
"required_beer_volume = depth * sun_surface_area"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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": 1
|
||||
}
|
||||
79
Week-5/03_naming_errors.ipynb
Normal file
79
Week-5/03_naming_errors.ipynb
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Errors in Naming"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** When you run the following cell, Python will produce an slightly-cryptic error message. Explain in the text cell below, in your own words, what's wrong with the code. (Remember, double-click the cell to edit it, and then click the Run button when you're done.)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"4 = 2 + 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*Write your answer here, replacing this text.*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 2.** When you run the following cell, Python will produce an slightly-cryptic error message. **Fix the error,** and then **explain below** in your own words what was wrong with the code."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"two = 2\n",
|
||||
"four = two + two"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Python didn't recognize the value called 'plus' and was probably confused about it being there, and since 'plus' is a mathmatical operation, I replaced 'plus' with '+' which when ran, gave no errors."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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": 1
|
||||
}
|
||||
183
Week-5/04_basic_arithmetic.ipynb
Normal file
183
Week-5/04_basic_arithmetic.ipynb
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Basic Arithmetic in Python\n",
|
||||
"This set of exercises gives you practice writing basic arithmetic expressions in Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** In the next cell, write a Python expression equivalent to this math expression:\n",
|
||||
"\n",
|
||||
"$$1 - \\frac{2^2}{2^{20}}$$\n",
|
||||
"\n",
|
||||
"The value of the expression is around 0.999996. After you have something that works, try to improve your solution by using only as many parentheses as you need. You can add a cell under your initial answer to show your work."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.9999961853027344"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"1 - (2**2 / 2**20)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 2.** Try to predict the value of the expression in the next cell. Before you run the cell, write your prediction in the cell below it."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"29.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"1 - 2 ** 3 / 4 + 5 * 6"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Your prediction here: 29\n",
|
||||
"# Order of Operations: (L-R)\n",
|
||||
"# 2 ** 3 = 8\n",
|
||||
"# 8 / 4 = 2\n",
|
||||
"# 5 * 6 = 30\n",
|
||||
"# 1 - 2 = -1\n",
|
||||
"# -1 + 30 = 29"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 3.** Try to predict whether the value of the expression below is larger or smaller than the value of the expression in the previous question. You can use the next cell to record your answer. Then run the cell to see what happens. If you were wrong, figure out why, and include that in your written answer."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"-1805811300.4191697"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"1 - 2 ** (3 / 4 + 5 * 6)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Prediction: The Value will be smaller, since everything in the parentheses will be done first, then that value will be the exponent for 2, aka 2 ^ value_in_parentheses, and then one will be added to that value(or subtracted depending on how you look at it), and will be shown.\n",
|
||||
"\n",
|
||||
"After cell execution: Right, and that is a rather large negative number."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 4.** A famous fact in mathematics is that\n",
|
||||
"\n",
|
||||
"$$(1 - \\frac{1}{n})^{-n}$$\n",
|
||||
"\n",
|
||||
"gets very close to the number $e$, which is roughly $2.718$, when $n$ is large. Verify that it gets closer to $e$ as $n$ gets larger by writing this expression in Python and trying different values for $n$.\n",
|
||||
"\n",
|
||||
"*Hint:* If you write several expressions in a cell, only the last will be printed. If you want to see the values of several copies of the expression at once, put `print(...)` around them. Alternatively, you can use separate cells for each expression."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2.7182216960543433"
|
||||
]
|
||||
},
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"n = 1000000000000\n",
|
||||
"(1-(1/n))**-n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"When you enter in 10000 for n, you get the 'rough' 2.718, and by adding more zeros, you get a value more precise. However, after adding more than 12 zeros, that number seems to make the rounding happen in a way that the value is no longer 2.718, but 2.719 and increasing the more zeros added..."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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": 1
|
||||
}
|
||||
101
Week-5/05_maxmin.ipynb
Normal file
101
Week-5/05_maxmin.ipynb
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## The Middle"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Suppose that Yulin, another INF110 student, is tasked with writing code to find the middle of three distinct numbers named `a`, `b`, and `c`. (By “middle” we mean the value that is neither the maximum nor the minimum.) For example, given the values 4, 2, and 7, the code should give back 4.\n",
|
||||
"\n",
|
||||
"Yulin has a moment of inspired thought and rapidly types the following into her Jupyter notebook:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"a + b + c - (max(a, b, c) + min(a, b, c))\n",
|
||||
"# Originally: a + b + c - max(a, b, c) + min(a, b, c)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Is her code correct? Why or why not? If not, make a *simple* change to the cell above to fix it.\n",
|
||||
"\n",
|
||||
"*Hint:* Running the cell will produce an error because we haven't defined `a`, `b`, and `c`, but you're free to make any changes you want to test your ideas. (When you submit, just indicate which cell is your final answer.)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"888"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Testing the interesting function: a + b + c - max(a, b, c) + min(a, b, c)\n",
|
||||
"\n",
|
||||
"# Using the test input vals a = 4, b = 2, c = 7\n",
|
||||
"\n",
|
||||
"##a = 4\n",
|
||||
"##b = 2\n",
|
||||
"##c = 7\n",
|
||||
"\n",
|
||||
"a = 999\n",
|
||||
"b = 345\n",
|
||||
"c = 888\n",
|
||||
"\n",
|
||||
"a + b + c - (max(a, b, c) + min(a, b, c))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Code was not correct, would return a value that is not the middle one. By adding parentheses before the call of max and after the call of min, you can return a middle value of three variables every single time."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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": 1
|
||||
}
|
||||
163
Week-5/06_rounding.ipynb
Normal file
163
Week-5/06_rounding.ipynb
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Rounding with Function Calls"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"These exercises give you practice calling functions in Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** Using the function `round`, round the number 1.23456789 to the nearest integer. Give the result the name `rounded`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Round can take two parameters, the first is the number to round, while the second is the decimal places in which to round to.\n",
|
||||
"rounded = round(1.23456789, 0)\n",
|
||||
"rounded"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 2.** Round 1.23456789 to the nearest *thousandth* (.001).\n",
|
||||
"\n",
|
||||
"*Hint:* Running `round?` in a code cell will show you Python's documentation for the `round` function."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1.235"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Using round, with the second parameter as three, you get three decimal point precision.\n",
|
||||
"less_rounded = round(1.23456789, 3)\n",
|
||||
"less_rounded"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Suppose you're counting all the small donations made to a political candidate. The donations were:\n",
|
||||
"\n",
|
||||
"* 32.25\n",
|
||||
"* 15.00\n",
|
||||
"* 24.45\n",
|
||||
"\n",
|
||||
"You need to report the total rounded to the nearest dollar.\n",
|
||||
"\n",
|
||||
"**Question 3.** First you decide to round *each donation* individually and then add up those 3 rounded amounts. Compute that."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"71.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sum_of_rounded_amounts = round(32.25, 0) + round(15.00, 0) + round(24.45, 0)\n",
|
||||
"sum_of_rounded_amounts"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 4.** You suspect this method doesn't work quite right. To check, you decide to try a different method: Adding up the donations, and then rounding the total. Compute that."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"72.0"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"rounded_sum = round((32.25 + 15.00 + 24.45), 0)\n",
|
||||
"rounded_sum"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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": 1
|
||||
}
|
||||
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
|
||||
}
|
||||
359
Week-9/INF110_Mini_HW2.ipynb
Normal file
359
Week-9/INF110_Mini_HW2.ipynb
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue