Skip to content

Commit

Permalink
uploaded all notebooks to their own dir
Browse files Browse the repository at this point in the history
  • Loading branch information
russodanielp authored Jul 1, 2024
1 parent d5c47ef commit 3ddfe7d
Show file tree
Hide file tree
Showing 10 changed files with 6,441 additions and 0 deletions.
1,162 changes: 1,162 additions & 0 deletions python_notebooks/1. Analyzing Patient Data (numpy).ipynb

Large diffs are not rendered by default.

684 changes: 684 additions & 0 deletions python_notebooks/10. Command-Line Programs.ipynb

Large diffs are not rendered by default.

354 changes: 354 additions & 0 deletions python_notebooks/2. Repeating Actions With Loops.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Repeating actions with loops #\n",
"\n",
"\"How can I do the same operations on many different values?\" \n",
"objectives: \n",
"\"Explain what a for loop does.\" \n",
"\"Correctly write for loops to repeat simple calculations.\" \n",
"\"Trace changes to a loop variable as the loop runs.\" \n",
"\"Trace changes to other variables as they are updated by a for loop.\" keypoints: \n",
"\"Use for variable in collection to process the elements of a collection one at a time.\" \n",
"\"The body of a for loop must be indented.\" \n",
"\"Use len(thing) to determine the length of something that contains other values.\" \n",
"\n",
"In the last lesson, we noticed some abnormalities in our data, for example the file `inflammation-01.csv`\n",
"![Analysis of inflammation-01.csv](fig/03-loop_2_0.png)\n",
"\n",
"If you look in the data folder, you'll notice that we have dozens of datets that we plan on evaluation. Individual evaluation is possible, but not practical. If we want to perform the same action on several items, python allows us to do so by using `for` loops. \n",
"\n",
"As an example, let's say have a variable called `word` which was assigned the string `\"lead\"`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"word = \"lead\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's suppose that we wanted to to print each character of that word. The most straighforward and easiest way to performed this task would be access each character by its index and printing them to the screen. This would look like so:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"l\n",
"e\n",
"a\n",
"d\n"
]
}
],
"source": [
"print(word[0])\n",
"print(word[1])\n",
"print(word[2])\n",
"print(word[3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Well, that worked. But we can see how this wouldn't scale up to larger words or even sentences. Accessing these letters by each's index could take a long time. Additionally, we can run in to situations where we may not be aware of the length of the word. For example, let's say the string `\"tin\"` was stored in our variable `word`. Taking the same approach, we would render an error."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t\n",
"i\n",
"n\n"
]
},
{
"ename": "IndexError",
"evalue": "string index out of range",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-3-d9583df8c6ff>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mword\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mword\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mword\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m: string index out of range"
]
}
],
"source": [
"word = \"tin\"\n",
"\n",
"print(word[0])\n",
"print(word[1])\n",
"print(word[2])\n",
"print(word[3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We get an error, which is unsurprising. The word `\"tin\"` doesn't have four characters, it has three. So, accessing the index `word[3]`, renders an error. Thankfully, we can circumvent this problem by using a `for` loop. `for` loops allow us to access the idividual elements of a collection one-by-one. Using a `for`, we can print each character of `word`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"o\n",
"x\n",
"y\n",
"g\n",
"e\n",
"n\n"
]
}
],
"source": [
"word = \"oxygen\"\n",
"for char in word:\n",
" print(char)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exactly what we wanted, each letter in the variable `word` (in this case `\"oxygen\"`) was printed. Here, the for loop allowed us to print each letter in `word`, but stopped when there were no longer any elements in the variable. This is the general concept of a `for` loop, for each _element_ in a _collection_ do something to that _element_. In our example above, the statment `for char in word` for each _element_ (letter) in our _collection_ (word) assign it to the variable `char`. This allows us to treat `char` like we would any other variable in python, with the difference being that `char` will take on a different value through each element of our collection. We can see how this is this is done graphically:\n",
"\n",
"![loop_image](fig/loops_image.png)\n",
"\n",
"One of the best things about python is the verbosity of the language. We can call the `for` loop variable anything we want. Sometimes, we only need to do something for the length of a variable? What if we wanted to count the number of characters contained within a string variable. We could accomplish that using a `for` loop."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There are 5 vowels\n"
]
}
],
"source": [
"length = 0\n",
"for vowel in 'aeiou':\n",
" length = length + 1\n",
"print('There are', length, 'vowels')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's look at this code piece by piece.\n",
"\n",
"So how many times does the code within the `for` loop execute? The answer is however many elements are in whatever we are using the `for` loop to iterate through. In the case above that would be 5, because their are 5 elements in our string `'aeiou'`. Why did the variable `length` display a value of 5 when we assigned it a value of 0? Before the `for` loop executes we a assign it a value it a value of 0. However, the expression\n",
"```\n",
"length = length + 1\n",
"```\n",
"is assigning a new value to length through each iteration of the `for` loop; where the new value is the old value + 1. After the first iteration of the `for` loop, `length` would be equal to 1 (0 = 0 + 1). Through the second iteration, it would be 2 (2 = 1 + 1). Through the third, 3 (3 = 2 + 1); And so on throughout the length of the string `aeiou`. Which would leave us with the number 5. It's important to remember that even though the variable `vowel` isn't being used within the `for` loop, it is still taking on the value of each element we are iterating over. And it doesn't go after the `for` loop is over. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variable Type Data/Info\n",
"----------------------------\n",
"char str n\n",
"length int 5\n",
"vowel str u\n",
"word str oxygen\n"
]
}
],
"source": [
"% whos"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that it is still in memory and retains the value it was last assigned. "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"u\n"
]
}
],
"source": [
"print(vowel)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, after programming awhile you'll notice that finding the length of a collection of items, such as a string, is a pretty common task. So much so that python even has a built-in function called `len` that returns the amount of elements in collection. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(len('aeiou'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Try it out ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### From 1 to N ###\n",
"\n",
"Python has a built-in function called range that creates a sequence of numbers. Range can accept 1-3 parameters. If one parameter is input, range creates an array of that length, starting at zero and incrementing by 1. If 2 parameters are input, range starts at the first and ends just before the second, incrementing by one. If range is passed 3 parameters, it starts at the first one, ends just before the second one, and increments by the third one. For example, range(3) produces the numbers 0, 1, 2, while range(2, 5) produces 2, 3, 4, and range(3, 10, 3) produces 3, 6, 9. Using range, write a loop that uses range to print the first 3 natural numbers:\n",
">1 \n",
">2 \n",
">3 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Computing Powers With Loops ##\n",
"\n",
"Exponentiation is built into Python:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"125\n"
]
}
],
"source": [
"print(5 ** 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write a loop that calculates the same result as 5 \\** 3 using multiplication (and without exponentiation)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reverse a String ##\n",
"\n",
"Write a loop that takes a string, and produces a new string with the characters in reverse order, so 'Newton' becomes 'notweN'"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading

0 comments on commit 3ddfe7d

Please sign in to comment.