Jupyter Lab 000

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 17

{

"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1> Lab 0: Introduction to Python </h1>\n",
"\n",
"Welcome to your first notebook of MATH 1LS3! Throughout the term, you will use
programming (Jupyter notebooks) to help you make sense of math concepts and enhance
your skills in working with mathematical models.\n",
"\n",
"The purpose of Lab 0 is to provide an introduction to the basics of Python so
you can become more comfortable with the programming language and the platform,
Jupyter. This notebook will also serve as a helpful reference point throughout the
term as you complete your graded lab assignments. \n",
"\n",
"You can navigate through the contents of this notebook (and your labs in the
future) using the <i> Sections </i> cell. Questions that you need to answer in
Childsmath using Jupyter notebooks should be outlined with a red border (does not
show up on all browsers). \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b> This lab has 6 questions. </b>\n",
"\n",
"As you work through this lab, you will encounter, and answer, each of the 6
questions (called \"Problems\"). Enter your answers into\n",
"<a href=\"https://www.childsmath.ca/childsa/forms/main_login.php\">
Childsmath</a> by logging in with your Mac ID and password. \n",
"\n",
"<b>There is no submit button on Childsmath; save your work often.</b> You can
change your answers as long as the lab is open (so, you can start your lab today,
do part of it, and save; then come back tomorrow, fix the answer that you realized
was incorrect, and continue working on the lab). The answers that are there at the
moment when the lab closes are taken as your final answers."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2> Sections </h2>\n",
"\n",
"<ul>\n",
"<li><a href='#first_steps'>First Steps</a>\n",
"<li><a href='#simple_Calculations'>Simple Calculations</a>\n",
"<ul>\n",
"<li><a href='#q1'>Problem 1</a></li>\n",
"</ul>\n",
"</li>\n",
" <li><a href='#variables'>Using Variables</a></li>\n",
"<ul>\n",
"<li><a href='#q2'>Problem 2</a></li>\n",
"<li><a href='#q3'>Problem 3</a></li>\n",
"</ul>\n",
"<li><a href='#lists'>Working with Lists</a>\n",
"<ul>\n",
"<li><a href='#q4'>Problem 4</a></li>\n",
"</ul>\n",
"</li>\n",
"<li><a href='#loops'>Working with Loops</a>\n",
"<ul>\n",
"<li><a href='#q5'>Problem 5</a></li>\n",
"</ul>\n",
"</li>\n",
"<li><a href='#exercise'>Application from Epidemiology</a>\n",
"<ul>\n",
"<li><a href='#q6'>Problem 6</a></li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='first_steps'></a>\n",
"<h2> First Steps </h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each notebook is comprised of \"cells\" with either <i>Python code</i> or
<i>text</i>. This cell is a <i>text cell</i>. Double-click anywhere in the cell to
see how the text was typed in &mdash; it's the same markup language that is used
for all web pages. \n",
"\n",
"The cell below (with <code> In []: </code> on the left) is a <i>code</i> cell
where we can type Python code. To run contents of a cell, place the cursor anywhere
inside the cell, and select \"Run\" from the menu/toolbar above the window (or use
the keyboard shortcut Control + Return/Enter). "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" \n",
"YOU'RE READY TO GO!\n",
" \n"
]
}
],
"source": [
"# This cell calls/imports packages and assigns shortcuts that we will be using
in 1LS3\n",
"# All notebooks that we will use in 1LS3 will contain a cell identical, or
similar to this one\n",
"# We will not be concerned about what exactly is going on here\n",
"\n",
"import matplotlib.pyplot as plt # MatPlotLib: Plotting library for Python\n",
"import numpy as np # NumPy: N-dimensional array for numerical computation\n",
"%matplotlib inline\n",
"\n",
"# Lines which start with # (called comments) are ignored by Python \n",
"# We use them to comment and/or explain the code, to give additional
information, etc.,\n",
"# so that it is easier to follow the code\n",
"#\n",
"# Placing # in front of a line is called \"commenting it out\" \n",
"# and removing # (making the line an active part of the code) \n",
"# is called \"uncommenting\"\n",
"# \n",
"# To run the Python code in a cell you need to\n",
"# place the cursor anywhere inside the cell, and select Run from the\n",
"# menu/toolbar above the window (alternatively, press Control + Return/Enter)\
n",
"#\n",
"# TO START, RUN THIS CELL\n",
"\n",
"print(\" \")\n",
"print(\"YOU'RE READY TO GO!\")\n",
"print(\" \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='simple_Calculations'></a>\n",
"<h2> Simple Calculations </h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python can be used as a calculator. For example, we can type a simple
calculation...\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# run this cell\n",
"12+2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"... and we get 14. Perfect! Now, try it out yourself using other common
mathematical operations:\n",
"<table align=\"left\", style=\"width:60%\">\n",
" <tr>\n",
" <th>OPERATOR</th>\n",
" <th>NAME</th>\n",
" <th>EXAMPLE</th>\n",
" </tr>\n",
" <tr>\n",
" <td><code>+</code></td>\n",
" <td>addition</td>\n",
" <td><code>12+2</code></td>\n",
" </tr>\n",
" <tr>\n",
" <td><code>-</code></td>\n",
" <td>subtraction</td>\n",
" <td><code>12-2</code></td>\n",
" </tr>\n",
" <tr>\n",
" <td><code>*</code></td>\n",
" <td>multiplication</td>\n",
" <td><code>12*2</code></td>\n",
" </tr>\n",
" <tr>\n",
" <td><code>/</code></td>\n",
" <td>division</td>\n",
" <td><code>12/2</code></td>\n",
" </tr>\n",
" <tr>\n",
" <td><code>**</code></td>\n",
" <td>exponentiation</td>\n",
" <td><code>12**2</code></td>\n",
" </tr>\n",
"</table>\n",
"<br><br><br><br><br><br><br><br><br>\n",
"Note that exponentiation is done with the ** operator."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# try it out here! \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='q1'></a>\n",
"<p style=\"border:3px; border-style:solid; border-color:#FF0000; padding:
1em;\"><b> Problem 1</b> (enter your answer to this, and all other questions, into
Childsmath)\n",
"<br>\n",
"Now, try to compute\n",
"(a) $\\displaystyle12\\cdot 5^3$ and \n",
"(b) $ \\dfrac{45-2^{1.5}}{5^{1/2}}$ below. Pay attention to the order of
operations and when and where to use brackets! \n",
" <br>\n",
" <i> Round your answer to 4 decimal places if rounding is required. </i>\
n",
"</p>"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# try it out here!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='variables'></a>\n",
"<h2> Using Variables </h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python stores information in variables. We need to decide what we call them.
For example: "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 Awesome result\n"
]
}
],
"source": [
"# run this cell\n",
"frogs = 10\n",
"time = 4.5\n",
"congrats = \"Awesome result\" # we can store text as a variable by enclosing
characters inside quotation marks \" \"\n",
" # it is called a string \n",
"\n",
"print(frogs, congrats)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the above cell, we <b> assigned </b> the value <code>10</code> to the
variable <code>frogs</code> and the string <code>\"Awesome result\"</code> to the
variable <code>congrats</code>. When we print the <code>frogs</code> and
<code>congrats</code>, it returns <code>10</code> and <code>\"Awesome
result\"</code>. The <code>print</code> command is extremely useful - you can
always check what is going on with your code! Now, try to print the variable
<code>time</code>:\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# try it out here! \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b> Rules for naming variables </b>:\n",
"<ul>\n",
"<li>Variables names must start with a letter (and not a number or a special
character)</li>\n",
"<li>The remainder of your variable name may consist of letters, numbers and
underscores such as <code>t, t0, a13, t_0</code> and so on</li>\n",
"<li>Names are case sensitive: <code>time</code>, <code>Time</code> and
<code>TIME</code> represent different variables</li>\n",
"</ul>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='q2'></a>\n",
"<p style=\"border:3px; border-style:solid; border-color:#FF0000; padding:
1em;\"><b> Problem 2</b>\n",
"<br>\n",
" Which of the following is <b> NOT </b> a valid variable name? <br />\n",
" a)<code>t1</code><br />\n",
" b)<code>DATE</code><br />\n",
" c)<code>+age</code><br />\n",
" d)<code>a14</code>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can change the values of the variables. "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5000\n"
]
}
],
"source": [
"# run this cell\n",
"\n",
"frogs = 5000 # at this moment python forgets that the value of frogs was 10,
because you \n",
"# are assigning to it a new value\n",
"print(frogs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='q3'></a>\n",
"<p style=\"border:3px; border-style:solid; border-color:#FF0000; padding:
1em;\"><b> Problem 3</b>\n",
"<br>\n",
" Now, try to assign <i>your</i> new value (as displayed in the statement of
Problem 3 when you log into Chidsmath) to the variable <code>time</code> and write
an expression that adds <code>2</code> to <code>time</code>. Enter the result of
that expression in Childsmath.\n",
"</p>"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# try it out here! \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='lists'></a>\n",
"<h2> Working with Lists </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far, we learned how to store single values in variables. However, you will
be working with a lot of multiple values in Python. \n",
"\n",
"Take a look at this scenario: you measured the body temperature of a patient
and want to store that information in a variable which you named T (stands for
temperature). \n",
"\n",
"A <b>BAD IDEA</b> would be to introduce variable for every single
measurement:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"T_1 = 39.0 #1st measurement\n",
"T_2 = 38.3 #2nd measurement\n",
"T_3 = 37.9 #3rd measurement\n",
"T_4 = 38.5 #4th measurement"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python has a special way of storing multiple values (like real numbers for
temperature) called a <b>list</b>. Lists are enclosed in square brackets
<code>[]</code> and information within the list is separated by commas. \n",
"\n",
"To access information in lists, we can use something called <b>indexing</b>.
We put the index number inside square brackets beside the list name. In Python, the
index starts at zero. Therefore, <code>&lt;list name&gt;[0]</code> returns the
first element in your list, <code>&lt;list name&gt;[1]</code> returns the second
element, and so on. Let's try it: "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"39.0 38.3 37.9 38.5\n",
"39.0\n",
"38.3 37.9\n",
"38.3 38.5\n"
]
}
],
"source": [
"# run this cell and make sure you understand where the output came from\n",
"T = [39.0, 38.3, 37.9, 38.5] \n",
"\n",
"print(T[0], T[1], T[2], T[3])\n",
"print(T[0])\n",
"print(T[1], T[2])\n",
"print(T[1], T[3])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"38.5\n",
"37.9\n"
]
}
],
"source": [
"# Please feel free to experiment. \n",
"# You shouldn't be able to break Python (if you do, please let the Python
community know that you found a bug).\n",
"# run this cell \n",
"\n",
"print(T[-1]) # what happened?\n",
"print(T[-2]) # so, what does the minus do?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='q4'></a>\n",
"<p style=\"border:3px; border-style:solid; border-color:#FF0000; padding:
1em;\"><b> Problem 4</b>\n",
"<br>\n",
" What does the minus sign in <code>T[-i]</code> do?"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list index out of range",
"output_type": "error",
"traceback": [
"\
u001b[0;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback
(most recent call last)",
"\u001b[0;32m/tmp/ipykernel_198/4170325348.py\u001b[0m in \
u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \
u001b[0;31m# run this cell, does it work?\u001b[0m\u001b[0;34m\u001b[0m\
u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \
u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\
u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mT\u001b[0m\u001b[0;34m[\u001b[0m\
u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\
u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"# run this cell, does it work?\n",
"\n",
"print(T[4]) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Clearly, there is a problem with the code that we just ran. Python is not
happy! Specifically, it is telling you \n",
"\n",
"<code>IndexError: list index out of range</code>\n",
"\n",
"Remember that in Python, the index starts at zero! The first entry is
<code>T[0]</code>, the second entry is <code>T[1]</code> etc... The list
<code>T</code> has only four entries. The code <code>T[4]</code> is asking for the
fifth entry of the list, which does not exist. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also change entries in the list. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# run this cell \n",
"T = [39.0, 38.3, 37.9, 38.5]\n",
"print(T)\n",
"\n",
"# change first entry (e.g., your measurement was wrong)\n",
"T[0] = 38.9\n",
"print(T)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, try to change the fourth entry of the list T to <code>41.7</code> and
print the list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# try it out here!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can add entries to a list, which is called <b>append</b>. When appending to
a list, the new entry is automatically appended to the <b>end of the list</b>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# run this cell\n",
"T.append(42.9)\n",
"print(T)\n",
"print(T[4]) # now it works, we do have 5 entries in the list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, try to add 40.5 to list T. Print out the first and last entry of list T:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# try it out here! \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='loops'></a>\n",
"<h2> Working with Loops </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python has special constructions for lists which are often needed. \n",
"\n",
"The command <code>range(start,stop)</code> generates all numbers from
<code>start</code> to <code>stop-1</code>. For example: <code>range(1,10) =
[1,2,3,4,5,6,7,8,9]</code>. In math notation, range(1,10) is [0,10). \n",
"\n",
"<code>range()</code> is useful when we want to run <b>loops</b>. We use a loop
whenever we want to <u>repeat the same task.</u> There are different types of
loops, but we will focus on <code>for</code> <b>loops</b>. \n",
"\n",
"A <code>for</code> loop is a way to iterate over the elements of a sequence
(e.g., list - we will mostly be dealing with looping over lists in this course).
Here is the general syntax of a <code>for</code> loop in Python: \n",
"\n",
"```python\n",
"for <variable> in <sequence>: \n",
" #body of loop \n",
" #has statements which requires repeated execution\n",
"```\n",
"\n",
"The &lt;variable&gt; is a variable used to iterate over a &lt;sequence&gt;.
After each iteration of the loop, the next element of the sequence is assigned to
the variable, until we reach the end of the sequence. The command that is repeated
through the loop has to be <b>indented</b>. Let's look at a few examples better
understand how loops are used."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Example 1:</b> use <code>range()</code> and print all the elements in the
list\n",
"\n",
"Copy the following code and run it in the cell below:\n",
"```python\n",
"for i in range(1,10): #recall that range(1,10) = [1,2,3,4,5,6,7,8,9]\n",
" print(i)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# try it out here!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Example 2:</b> print each temperature in the temperature list\n",
"\n",
"Copy the following code and run it in the cell below:\n",
"```python\n",
"T = [38.9, 38.3, 37.9, 38.5, 42.9, 40.5]\n",
"for temp in T:\n",
" print(temp)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# try it out here!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Example 3:</b> access more than one list in for loops\n",
"\n",
"Copy the following code and run it in the cell below:\n",
"```python\n",
"days = [\"mon\", \"tues\", \"wed\", \"thurs\", \"fri\", \"sat\"]\n",
"T = [38.9, 38.3, 37.9, 38.5, 42.9, 40.5]\n",
"for i in range(0,6): #range(0,6) = [0,1,2,3,4,5]\n",
" print(days[i],\":\",T[i]) # recall days[i] is how we access the i-th
element of the list, starting at zero\n",
" # e.g., days[0] = mon; T[0] = 38.9 \n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mon : 38.9\n",
"tues : 38.3\n",
"wed : 37.9\n",
"thurs : 38.5\n",
"fri : 42.9\n",
"sat : 40.5\n"
]
}
],
"source": [
"# try it out here!\n",
"\n",
"days = [\"mon\", \"tues\", \"wed\", \"thurs\", \"fri\", \"sat\"]\n",
"T = [38.9, 38.3, 37.9, 38.5, 42.9, 40.5]\n",
"for i in range(0,6): #range(0,6) = [0,1,2,3,4,5]\n",
" print(days[i],\":\",T[i]) # recall days[i] is how we access the i-th
element of the list, starting at zero\n",
" # e.g., days[0] = mon; T[0] = 38.9"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Example 4:</b> indentation matters! \n",
"\n",
"Copy the following code and run it in the cell below:\n",
"```python\n",
"#CODE A\n",
"for t in range(3,7):\n",
" print(t)\n",
"print(\"snow monkeys\") \n",
"\n",
"print(\" \") # this line prints a blank line, so that we can separate the
putputs of the two loops\n",
"print(\" \") \n",
"\n",
"#CODE B\n",
"for t in range(3,7):\n",
" print(t)\n",
" print(\"snow monkeys\") \n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# try it out here! \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='q5'></a>\n",
"<p style=\"border:3px; border-style:solid; border-color:#FF0000; padding:
1em;\"><b> Problem 5</b>\n",
"<br>\n",
" How many <code>snow monkeys</code> do <code>CODE A</code> and <code>CODE
B</code> print, respectively? <i>Enter your answers in Childsmath, separated by a
comma.</i>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='exercise'></a>\n",
"<h2> Application from Epidemiology </h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's do an example combining everything that we have learned! \n",
" <h3> Example of a Discrete Time Dynamical System </h3>\n",
"\n",
"Let $I_t$ denote the number of people infected with the flu, where $t$ is time
in days (so that $t+1$ represents tomorrow if $t$ represents today). It has been
determined that\n",
"\n",
"$\\displaystyle{I_{t+1} = {60I_t \\over 0.5I_t+1.5}}$ \n",
"\n",
"and that initially there are two cases of the flu, i.e., $I_0=2$.\n",
"\n",
"How does the number of people infected with the flu change over time?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## we can write the code in different ways, here is one:\n",
"\n",
"t = 0 # initial time\n",
"I = 2 # number of infected people initially\n",
"print(t,I)\n",
"\n",
"for t in range (1,7): # remember: this will make Python go through the loop 6
times\n",
" I=60*I/(0.5*I+1.5) # substitute the value of I into the right side, and
compute the new value of I\n",
" print(t,I)\n",
"\n",
"# run this code\n",
"# change 7 to 10, or something larger, to obtain more values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p style=\"border:2px; border-style:solid; border-color:#000000; padding:
1em;\">\n",
"Note: In this course, we will not ask you to write a code from scratch, but to
look at the given code, understand what it does, and modify to answer the questions
</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### we can write the code in different ways, here, we use a list:\n",
"\n",
"t = 0 # initial time\n",
"I = [2] # create a list to hold the number of infected people after each day\
n",
" # number of infected people initially = first entry in the list = 2\
n",
"\n",
"for t in range (0,6):\n",
" Inew=60*I[t]/(0.5*I[t]+1.5) # substitute the value of I into the right
side, and compute the new value of I\n",
" I.append(Inew)\n",
"print(I)\n",
"\n",
"#run and compare with the above output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#### yet another way\n",
"# go through this code; what is it doing?\n",
"\n",
"I_t = [2] # initial measurement\n",
"n_steps = 10 # number of iterations\n",
"\n",
"for k in range(n_steps):\n",
" I_t.append(60*I_t[-1] / (0.5*I_t[-1] + 1.5))\n",
"\n",
"print(I_t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>FINAL CHALLENGE!!!!!</b>\n",
"\n",
"Copy the following code and run it in the cell below:\n",
"```python\n",
"t = 0\n",
"I = 2\n",
"print(t,I)\n",
"plt.scatter(t,I) # plots a point with coordinates (t,I)\n",
"\n",
"for t in range(1,5):\n",
" I=60*I/(0.5*I+1.5)\n",
" print(t,I)\n",
" plt.scatter(t,I)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"# try it out here!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='q6'></a>\n",
"<p style=\"border:3px; border-style:solid; border-color:#FF0000; padding:
1em;\"><b> Problem 6</b>\n",
"<br>\n",
"Replace range (1,5) by range (1,20) in the code from the last cell - what
happens? "
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"# try it out here!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>End of lab 0.</h3>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy