Commit 39198c7c authored by Emilio Ambite's avatar Emilio Ambite

last version

parent c8ae748f
......@@ -97,7 +97,7 @@
"def normalize_rectangle(rect):\n",
" '''Normalizes a rectangle so that it is at the origin and 1.0 units long on its longest axis.'''\n",
" assert len(rect) == 4, 'Rectangles must contain 4 coordinates'\n",
" llx, llyy, urx, ury = rect\n",
" llx, lly, urx, ury = rect\n",
" assert llx < urx, 'Invalid X coordinates'\n",
" assert lly < ury, 'Invalid Y coordinates'\n",
"\n",
......@@ -141,6 +141,27 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Post-conditions in lines 17, 18 check well formed output:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -244,20 +265,6 @@
"# Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -286,12 +293,17 @@
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
}
],
......
......@@ -504,6 +504,13 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -555,38 +562,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Oh wait... maybe I am creating to many variables... let's check:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes we can avoid the use of some variables:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -668,31 +643,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-info\">\n",
"<div class=\"panel-heading\">\n",
"<h3 class=\"panel-title\">\n",
"IPython Magic!\n",
"</h3>\n",
"</div>\n",
"<div class=\"panel-body\">\n",
"The % indicates an IPython magic function - a funciton that is valid only within the notebook environment.\n",
"</div>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -741,15 +691,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -884,7 +825,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.7.4"
}
},
"nbformat": 4,
......
......@@ -100,7 +100,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"What about Farh to Celsius? One of the main proposes of functions is reusability. So let's reuse one of them to build some \"neat\" code:"
"What about Fahr to Celsius? One of the main proposes of functions is reusability. So let's reuse one of them to build some \"neat\" code:"
]
},
{
......@@ -125,14 +125,16 @@
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {},
"source": [
"1) 7 3\n",
"2) 3 7\n",
"3) 3 3\n",
"4) 7 7\n",
"1. ) 7 3\n",
"2. ) 3 7\n",
"3. ) 3 3\n",
"4. ) 7 7\n",
"\n",
"\n",
"```\n",
"a = 3\n",
"b = 7\n",
"\n",
......@@ -143,7 +145,8 @@
"\n",
"swap(a, b)\n",
"\n",
"print(a, b)\n"
"print(a, b)\n",
"```"
]
},
{
......@@ -528,20 +531,23 @@
]
},
{
"cell_type": "raw",
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"def numbers(one, two=2, three, four=4):\n",
" n = str(one) + str(two) + str(three) + str(four)\n",
" return n\n",
"\n",
"print(numbers(1, three=3))\n",
"```\n",
"\n",
"what do you expect will be printed? What is actually printed? What rule do you think Python is following?\n",
"\n",
"1) 1234\n",
"2) one2three4\n",
"3) 1239\n",
"4) SyntaxError"
"1. ) 1234\n",
"2. ) one2three4\n",
"3. ) 1239\n",
"4. ) SyntaxError"
]
},
{
......@@ -593,10 +599,34 @@
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"def s(p):\n",
" a = 0\n",
" for v in p:\n",
" a += v\n",
" m = a / len(p)\n",
" d = 0\n",
" for v in p:\n",
" d += (v - m) * (v - m)\n",
" return numpy.sqrt(d / (len(p) - 1))\n",
" \n",
"#It is not very to say what it does...though it does something very well known. Check this one:\n",
"\n",
"def std_deviation(sample):\n",
" sample_sum = 0\n",
" for value in sample:\n",
" sample_sum += value\n",
" \n",
" sample_mean = sample_sum / len(sample)\n",
" sum_squared_devs = 0\n",
" for value in sample:\n",
" sum_squared_devs += (value - sample_mean) * (value - sample_mean)\n",
" return numpy.sqrt(sum_squared_devs / (len(sample) - 1))\n",
"\n",
"#Much more easy to read!"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
......@@ -607,7 +637,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises"
"## Additional Exercises"
]
},
{
......@@ -739,22 +769,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Readable code"
]
},
{
"cell_type": "code",
"execution_count": null,
......
......@@ -102,7 +102,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Example: Products in the shelves of a shop. We can print the results of the index operations in the images:"
"Example: Products in the shelves of a shop. "
]
},
{
......@@ -114,15 +114,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -309,7 +300,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.7.4"
}
},
"nbformat": 4,
......
......@@ -11,7 +11,7 @@
"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!"
"We found some suspicious features in our first dataset \"inflammation-01.csv\". It is possible that other datasets are also affected by this?? We would like to check... Fast!"
]
},
{
......@@ -133,22 +133,6 @@
"<li>etc.."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 3. This loop overrides the value of a external variable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......
......@@ -6,7 +6,7 @@
"source": [
"# Making choices\n",
"\n",
"In last section we discovered something suspicios in our inflammation data by drawing some plots. How can we use Python\n",
"In last section we discovered something suspicious in our inflammation data by drawing some plots. How can we use Python\n",
"to automatically detect the anomalies we found and take an action for each of them?"
]
},
......@@ -38,7 +38,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause:"
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause. If there are multiple choices we make use of the <code>elif</code>:"
]
},
{
......@@ -54,25 +54,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes there is no need to use a else, it depends on the logic of the problem:"
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes the flow of the decision is more complex. There are still multiple choices available \n",
"when the first condition is discarded. <br>\n",
"As an example imagine we want to know if a given number is positive negative or zero. We make use of the <code>elif</code> clause:"
"Sometimes there is no need to use a else, it depends on the logic of the problem:"
]
},
{
......@@ -84,13 +73,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -198,6 +180,20 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -213,15 +209,6 @@
"First two files max inflammation seems to rise linearly. So let's write a code that check this behavior and show warning:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -344,7 +331,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.7.4"
}
},
"nbformat": 4,
......
......@@ -287,10 +287,8 @@
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def test_range_overlap():\n",
......@@ -362,27 +360,6 @@
"# Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# a possible pre-condition:\n",
"assert len(input) > 0, #'List length must be non-zero'\n",
"# a possible post-condition:\n",
"assert numpy.min(input) <= average <= numpy.max(input), #'Average should be between min and max of input values'"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -394,10 +371,8 @@
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy\n",
......@@ -409,6 +384,7 @@
" return None\n",
" lowest, highest = ranges[0]\n",
" for (low, high) in ranges[1:]:\n",
" assert low <= high, 'first element in range should be the lower one'\n",
" lowest = max(lowest, low)\n",
" highest = min(highest, high)\n",
" if lowest >= highest: # no overlap\n",
......@@ -419,23 +395,33 @@
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"test_range_overlap()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre- and Post-Conditions\n",
"\n",
"Suppose you are writing a function called average that calculates the average of the numbers in a list. What pre-conditions and post-conditions would you write for it? Compare your answer to your neighbor’s: can you think of a function that will pass your tests but not his/hers or vice versa?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": []
"source": [
"# a possible pre-condition:\n",
"assert len(input) > 0, #'List length must be non-zero'\n",
"# a possible post-condition:\n",
"assert numpy.min(input) <= average <= numpy.max(input), #'Average should be between min and max of input values'"
]
}
],
"metadata": {
......
......@@ -165,7 +165,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<li>Remember: Both SyntaxError and IntentationError indicate a problem with the syntax of the program"
"<li>Remember: Both SyntaxError and IndentationError indicate a problem with the syntax of the program"
]
},
{
......
......@@ -331,9 +331,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"import numpy"
......@@ -568,7 +566,7 @@
}
],
"source": [
"print(data[5:10, 0:10]) # Selecting rows 0,1,2,3 and cols from 0 to 9. Important! notice upper limits are not \n",
"print(data[5:10, 0:10]) # Selecting rows 5,6,7,8 and cols from 0 to 9. Important! notice upper limits are not \n",
" # included in the counting\n",
" # You don't have to start slices at 0"
]
......@@ -591,7 +589,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 5,
"metadata": {},
"outputs": [
{
......@@ -599,9 +597,9 @@
"output_type": "stream",
"text": [
"small is: \n",
"[[ 2. 3. 0. 0.]\n",
" [ 1. 1. 0. 1.]\n",
" [ 2. 2. 1. 1.]]\n"
"[[2. 3. 0. 0.]\n",
" [1. 1. 0. 1.]\n",
" [2. 2. 1. 1.]]\n"
]
}
],
......@@ -650,7 +648,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 6,
"metadata": {},
"outputs": [
{
......@@ -658,22 +656,22 @@
"output_type": "stream",
"text": [
"original:\n",
"[[ 2. 3. 0. 0.]\n",
" [ 1. 1. 0. 1.]\n",
" [ 2. 2. 1. 1.]]\n",
"doubledata: \n",
"[[ 4. 6. 0. 0.]\n",
" [ 2. 2. 0. 2.]\n",
" [ 4. 4. 2. 2.]]\n"
"[[2. 3. 0. 0.]\n",
" [1. 1. 0. 1.]\n",
" [2. 2. 1. 1.]]\n",
"doublesmall: \n",
"[[4. 6. 0. 0.]\n",
" [2. 2. 0. 2.]\n",
" [4. 4. 2. 2.]]\n"
]
}
],
"source": [
"doubledata = 2 * data # The x2 operation is made in every element of the array\n",
"doublesmall = 2 * small # The x2 operation is made in every element of the array\n",
"print('original:')\n",
"print(data[:3,36:])\n",
"print('doubledata: ')\n",
"print(doubledata[:3,36:])"
"print(small)\n",
"print('doublesmall: ')\n",
"print(doublesmall)"
]
},
{
......@@ -685,7 +683,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 7,
"metadata": {},
"outputs": [
{
......@@ -693,16 +691,16 @@
"output_type": "stream",
"text": [
"tripledata:\n",
"[[ 6. 9. 0. 0.]\n",
" [ 3. 3. 0. 3.]\n",
" [ 6. 6. 3. 3.]]\n"
"[[6. 9. 0. 0.]\n",
" [3. 3. 0. 3.]\n",
" [6. 6. 3. 3.]]\n"
]
}
],
"source": [
"tripledata = doubledata + data #Array-Array operations are made on each corresponding element\n",
"triplesmall = doublesmall + small #Array-Array operations are made on each corresponding element\n",
"print('tripledata:')\n",
"print(tripledata[:3,36:])"
"print(triplesmall)"
]
},
{
......@@ -1001,33 +999,6 @@
" # You may get a warning here about rendering fonts. If we re-run it not htere anymore."
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"panel panel-info\">\n",
"<div class=\"panel-heading\">\n",
"<h3 class=\"panel-title\">\n",
"IPython Magic!\n",
"</h3>\n",
"</div>\n",
"<div class=\"panel-body\">\n",
"The % indicates an IPython magic function - a funciton that is valid only within the notebook environment.\n",
"</div>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
......
......@@ -19,7 +19,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [
{
......@@ -156,7 +156,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Example: Products in the shelves of a shop. We can print the results of the index operations in the images:"
"Example: Products in the shelves of a shop. "
]
},
{
......@@ -170,23 +170,6 @@
"x = [['salt', 'zuchini', 'onion'], ['cabbage', 'lettuce', 'garlic'],['apple','pear','banana']]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['salt', 'zuchini', 'onion']]\n"
]
}
],
"source": [
"print([x[0]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -263,14 +246,14 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3, 5, 7, 11]\n"
"[3, 5, 7]\n"
]
}
],
......
......@@ -11,7 +11,7 @@
"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!"
"We found some suspicious features in our first dataset \"inflammation-01.csv\". It is possible that other datasets are also affected by this?? We would like to check... Fast!"
]
},
{
......@@ -39,15 +39,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"l\n",
"e\n",
"t\n",
"a\n",
"d\n"
"n\n",
"k\n"
]
}
],
"source": [
"word = 'lead' # From wich number to wich number we had to go? Ask people\n",
"word = 'tank' # 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",
......@@ -71,7 +71,7 @@
"output_type": "stream",
"text": [
"t\n",
"i\n",
"a\n",
"n\n"
]
},
......@@ -82,13 +82,13 @@
"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;32m<ipython-input-2-be45257cba61>\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[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[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[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",
"word = 'tan' # 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",
......@@ -112,22 +112,22 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"l\n",
"e\n",
"t\n",
"a\n",
"d\n"
"n\n",
"k\n"
]
}
],
"source": [
"word = 'lead'\n",
"word = 'tank'\n",
"for char in word:\n",
" print(char)"
]
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Making choices\n",
"\n",
"In last section we discovered something suspicious in our inflammation data by drawing some plots. How can we use Python\n",
"to automatically detect the anomalies we found and take an action for each of them?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>By writing code that runs only certain conditions</strong>:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conditionals\n",
"\n",
"A conditional is a piece of code that takes different actions depending on a condition:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Imagine we want to determine if a certain number is lower or greater than 100. The flow of this conditional:\n",
"<img src=\"https://scw-ss.github.io/python-novice-inflammation-2016-06-27-cfmehu/fig/python-flowchart-conditional.png\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause. If there are multiple choices we make use of the <code>elif</code>:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes there is no need to use a else, it depends on the logic of the problem:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercises\n",
"#### What Is Truth?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"if '':\n",
" print('empty string is true')\n",
"if 'word':\n",
" print('word is true')\n",
"if []:\n",
" print('empty list is true')\n",
"if [1, 2, 3]:\n",
" print('non-empty list is true')\n",
"if 0:\n",
" print('zero is true')\n",
"if 1:\n",
" print('one is true')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### That’s Not Not What I Meant"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"if not '':\n",
" print('empty string is not true')\n",
"if not 'word':\n",
" print('word is not true')\n",
"if not not True:\n",
" print('not not True is true')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Composed conditions\n",
"We can write complex conditions in the if clause by using concatenation of simple conditions by means of <code>and</code> and <code>or</code>. <br>\n",
"As in human language an <code>and</code> condition is True if all its components are True:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Whereas an <code>or</code> condition is True if any of it components is True:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise\n",
"\n",
"#### Close Enough\n",
"\n",
"Write some conditions that print True if the variable a is within 10% of the variable b and False otherwise. Compare your implementation with your partner’s: do you get the same answer for all possible pairs of numbers?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Checking our Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we are in conditions to check our suspicious data in the inflammation tests.<br>\n",
"First two files max inflammation seems to rise linearly. So let's write a code that check this behavior and show warning:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We saw a different issue in file 3. All minima where 0. So we write a code to check that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can check now several files putting all toghether:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Additional Exercises\n",
"\n",
"### In-place operators"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Python (and most other languages in the C family) provides in-place operators that work like this:\n",
"\n",
"```x = 1 # original value\n",
"x += 1 # add one to x, assigning result back to x\n",
"x *= 3 # multiply x by 3\n",
"print(x)```\n",
"\n",
"\n",
"``6``\n",
"\n",
"Write some code that sums the positive and negative numbers in a list separately, using in-place operators. Do you think the result is more or less readable than writing the same without in-place operators?\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Counting Vowels "
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"* Write a loop that counts the number of vowels in a character string.\n",
"* Test it on a few individual words and full sentences.\n",
"* Once you are done, compare your solution to your neighbor’s. Did you make the same decisions about how to handle the letter ‘y’ (which some people think is a vowel, and some do not)?\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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
}
......@@ -6,7 +6,7 @@
"source": [
"# Making choices\n",
"\n",
"In last section we discovered something suspicios in our inflammation data by drawing some plots. How can we use Python\n",
"In last section we discovered something suspicious in our inflammation data by drawing some plots. How can we use Python\n",
"to automatically detect the anomalies we found and take an action for each of them?"
]
},
......@@ -38,30 +38,40 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause:"
"This scheme is achieved in Python by means of the <code>if</code>/<code>else</code> clause. If there are multiple choices we make use of the <code>elif</code>:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"not greater\n",
"done\n"
"-3 is negative\n"
]
}
],
"source": [
"num = 37\n",
"if num > 100:\n",
" print('greater')\n",
"\n",
"num = -3\n",
"\n",
"if num > 0:\n",
" print(num, 'is positive')\n",
"elif num == 0: # note the sign == instead of =. The reason is that \"==\" tests equality whereas \"=\" means assignment\n",
" print(num, 'is zero')\n",
"else:\n",
" print('not greater')\n",
"print('done')"
" print(num, 'is negative')\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
......@@ -93,48 +103,6 @@
"print('... after conditional')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes the flow of the decision is more complex. There are still multiple choices available \n",
"when the first condition is discarded. <br>\n",
"As an example imagine we want to know if a given number is positive negative or zero. We make use of the <code>elif</code> clause:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-3 is negative\n"
]
}
],
"source": [
"\n",
"num = -3\n",
"\n",
"if num > 0:\n",
" print(num, 'is positive')\n",
"elif num == 0: # note the sign == instead of =. The reason is that \"==\" tests equality whereas \"=\" means assignment\n",
" print(num, 'is zero')\n",
"else:\n",
" print(num, 'is negative')\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<strong>Note the \"==\"</strong>. This symbol checks equality rather than \"=\" wich means assingment"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -276,7 +244,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [
{
......@@ -298,7 +266,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
......@@ -315,34 +283,19 @@
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"multiplication=a*b\n",
"substraction=a-b"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variable Type Data/Info\n",
"-----------------------------------\n",
"a float 0.9\n",
"b int 1\n",
"multiplication float 0.9\n",
"substraction float -0.09999999999999998\n"
"0.9 1 -0.09999999999999998\n"
]
}
],
"source": [
"%whos"
"print(a,b,a-b)"
]
},
{
......@@ -360,31 +313,6 @@
"First two files max inflammation seems to rise linearly. So let's write a code that check this behavior and show warning:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Suspicious looking maxima\n"
]
}
],
"source": [
"import numpy\n",
"\n",
"data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')\n",
"if (data.max(axis=0)[0]==0) and (data.max(axis=0)[20]==20): # Anomalous linear behavior of the max in the data\n",
" print('Suspicious looking maxima')\n",
"elif (data.min(axis=0).sum() == 0):\n",
" print('Minima add up to zero')\n",
"else:\n",
" print('Everything seems OK')"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -394,19 +322,21 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Minima add up to zero\n"
"Suspicious looking maxima\n"
]
}
],
"source": [
"data = numpy.loadtxt(fname='inflammation-03.csv', delimiter=',')\n",
"import numpy\n",
"\n",
"data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')\n",
"if (data.max(axis=0)[0]==0) and (data.max(axis=0)[20]==20): # Anomalous linear behavior of the max in the data\n",
" print('Suspicious looking maxima')\n",
"elif (data.min(axis=0).sum() == 0):\n",
......
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