Commit be25570a authored by Emilio Ambite's avatar Emilio Ambite

filled directory

parent ad54ac4c
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Repeating Actions with Loops"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We found some suspicious features in our firs dataset \"inflammation-01.csv\". It is possible that other datasets are also affected by this?? We would like to check... Fast!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Repeat tasks:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have a lot of data sets and we have to tell the computer how to repeat things.\n",
"<br>\n",
"Let's first do something simpler: Show each character in a string!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"l\n",
"e\n",
"a\n",
"d\n"
]
}
],
"source": [
"word = 'lead' # From wich number to wich number we had to go? Ask people\n",
"print(word[0])\n",
"print(word[1])\n",
"print(word[2])\n",
"print(word[3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bad approach: tedious and if the string is larger it misses some characters, and if it shorter:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"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[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-61757d5ac451>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mword\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mword\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mword\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\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: string index out of range"
]
}
],
"source": [
"word = 'tin' # Bad approach, if the string is larger it misses characters and if it shorter it fails!\n",
"print(word[0])\n",
"print(word[1])\n",
"print(word[2])\n",
"print(word[3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### There is a better approach: Recursion\n",
"Recursion is made by for loops:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 1. A better way:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"l\n",
"e\n",
"a\n",
"d\n"
]
}
],
"source": [
"word = 'lead'\n",
"for char in word:\n",
" print(char)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The method supports strings of any lenght:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"o\n",
"x\n",
"i\n",
"g\n",
"e\n",
"n\n"
]
}
],
"source": [
"word = 'oxigen'\n",
"for char in word:\n",
" print (char)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see it in action:\n",
"<img src=\"https://scw-ss.github.io/python-novice-inflammation-2016-06-27-cfmehu/fig/loops_image.png\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### General form of a for-loop:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img width=\"800px\" src=\"https://www.safaribooksonline.com/library/view/head-first-python/9781449397524/httpatomoreillycomsourceoreillyimages1368346.png.jpg\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can choose any name we want for variables. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"o\n",
"x\n",
"y\n",
"g\n",
"e\n",
"n\n"
]
}
],
"source": [
"word = 'oxygen'\n",
"for banana in word:\n",
" print(banana)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2. This loop repeatedly updates a variable that acts as counter. It is very usual to find this structure:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There are 5 vowels\n"
]
}
],
"source": [
"lenght = 0\n",
"for vowel in 'aeiou': # The process details can be explained here\n",
" lenght = lenght + 1 # Explain what happens if lenght is inside\n",
"print('There are ',lenght,'vowels')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The for body is executed 5 times\n",
"<ul>\n",
"<li>1st: vowel eqs a; lenght eqs 1\n",
"<li>2st: vowel eqs e; lenght eqs 2\n",
"<li>etc.."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Off-topic tip: Some loops frecuently used are wraped inside very useful methods, such as <code>len</code> which returns the length of the variable. The methods avaiable by default in Python without import a thing are called <em>built-in</em> methods"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(len('aeiou')) # built-in function to get the length of a string (and the length of many other things...). \n",
" # Is faster and much easier to read than a for loop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### From 1 to N"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python has a built-in function called range that creates a sequence of numbers. Using range, write a loop that uses range to print the first 3 odd natural numbers. Use the builtin help!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n"
]
}
],
"source": [
"for num in range(1,6,2):\n",
" print (num)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment