Commit c8ae748f authored by Emilio Ambite's avatar Emilio Ambite

filled directory

parent be25570a
Pipeline #66 failed with stages
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Errors and Exceptions\n",
"\n",
"Two main questions to be answered\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<li>How Python report errors?\n",
"\n",
"<li>How I can handle errors in Python programs?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Every programmer makes Errors\n",
"\n",
"Errors in Python have a very specific form called <b>\"traceback\"</b>. Traceback are made of levels wich are marked with special symbol <b>--></b>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list 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-1-c34fcf537534>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mice_creams\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[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mfavorite_ice_cream\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-1-c34fcf537534>\u001b[0m in \u001b[0;36mfavorite_ice_cream\u001b[1;34m()\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;34m\"strawberry\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m ]\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mice_creams\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[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[0mfavorite_ice_cream\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"# This code has an intentional error. You can type it directly or\n",
"# use it for reference to understand the error message below.\n",
"def favorite_ice_cream():\n",
" ice_creams = [\n",
" \"chocolate\",\n",
" \"vanilla\",\n",
" \"strawberry\"\n",
" ]\n",
" print(ice_creams[3])\n",
"\n",
"favorite_ice_cream()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Traceback with two levels. Are read from bottom to top:\n",
"<li> 1) Last level is the line with the error actually is\n",
"\n",
"<li> 2) Upper level(s) show what function was executed to get the next level down"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tracebacks could be very long but important info is usually at the bottom"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Error categories"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All errors at the end of a traceback, are usually in the form:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<code>Category: Some more info</code>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Where Categories are:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Syntax Errors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You forget a colon, add too many spaces, forget a parenthesis etc... Makes Python not to know how to read your program. That gives you:\n",
"<code>SyntaxError</code>"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-2-dbf32ad5d3e8>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-2-dbf32ad5d3e8>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m def some_function()\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"def some_function()\n",
" msg = \"hello, world!\"\n",
" print(msg)\n",
" return msg"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "IndentationError",
"evalue": "unexpected indent (<ipython-input-3-e169556d667b>, line 4)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-3-e169556d667b>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m return msg\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
]
}
],
"source": [
"def some_function():\n",
" msg = \"hello, world!\"\n",
" print(msg)\n",
" return msg"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<li>Remember: Both SyntaxError and IntentationError indicate a problem with the syntax of the program"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variable Name Errors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Difficult to state a general case but common mistakes are:\n",
"<li> You meant to use a string but forgot quotes\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'hello' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-43a14fcd4265>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhello\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'hello' is not defined"
]
}
],
"source": [
"print(hello)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<li> You forgot to create a variable before using it\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'count' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-6-3cc942c225b7>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mcount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcount\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mnumber\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The count is:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'count' is not defined"
]
}
],
"source": [
"for number in range(10):\n",
" count = count + number\n",
"print(\"The count is:\", count)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<li> There is a typo in a variable name (maybe because Python's Case Sensitivity)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'count' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-7-79d2bb4fcc22>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mCount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mcount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcount\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mnumber\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The count is:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'count' is not defined"
]
}
],
"source": [
"Count = 0\n",
"for number in range(10):\n",
" count = count + number\n",
"print(\"The count is:\", count)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### IndexErrors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Typically you try to access something out of bounds:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Letter #1 is a\n",
"Letter #2 is b\n",
"Letter #3 is c\n"
]
},
{
"ename": "IndexError",
"evalue": "list 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-9-656a22fa6ec5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Letter #2 is\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mletters\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 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Letter #3 is\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mletters\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----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Letter #4 is\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mletters\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: list index out of range"
]
}
],
"source": [
"letters = ['a', 'b', 'c']\n",
"print(\"Letter #1 is\", letters[0])\n",
"print(\"Letter #2 is\", letters[1])\n",
"print(\"Letter #3 is\", letters[2])\n",
"print(\"Letter #4 is\", letters[3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### File Errors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Typically two:\n",
"<li> Trying to access a file that does not exist:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'myfile.txt'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-10-f6e1ac4aee96>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfile_handle\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'myfile.txt'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'r'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'myfile.txt'"
]
}
],
"source": [
"file_handle = open('myfile.txt', 'r')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<li> You try to read(write) a write-only(read-only) file:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"ename": "UnsupportedOperation",
"evalue": "not readable",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mUnsupportedOperation\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-12-02e2f546ed29>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mfile_handle\u001b[0m\u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'myfile.txt'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'w'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mfile_handle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mUnsupportedOperation\u001b[0m: not readable"
]
}
],
"source": [
"file_handle= open('myfile.txt', 'w')\n",
"file_handle.read()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises\n",
"\n",
"### Reading Error Messages "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read the python code and the resulting traceback below, and answer the following questions:\n",
"\n",
"* How many levels does the traceback have? **3 levels**\n",
"* What is the function name where the error occurred? **print_message**\n",
"* On which line number in this function did the error occurr? **11**\n",
"* What is the type of error? **Keyerror**\n",
"* What is the error message? **'Friday'**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"# This code has an intentional error. Do not type it directly;\n",
"# use it for reference to understand the error message below.\n",
"def print_message(day):\n",
" messages = {\n",
" \"monday\": \"Hello, world!\",\n",
" \"tuesday\": \"Today is tuesday!\",\n",
" \"wednesday\": \"It is the middle of the week.\",\n",
" \"thursday\": \"Today is Donnerstag in German!\",\n",
" \"friday\": \"Last day of the week!\",\n",
" \"saturday\": \"Hooray for the weekend!\",\n",
" \"sunday\": \"Aw, the weekend is almost over.\"\n",
" }\n",
" print(messages[day])\n",
"\n",
"def print_friday_message():\n",
" print_message(\"Friday\")\n",
"\n",
"print_friday_message()\n",
"---------------------------------------------------------------------------\n",
"KeyError Traceback (most recent call last)\n",
"<ipython-input-1-4be1945adbe2> in <module>()\n",
" 14 print_message(\"Friday\")\n",
" 15\n",
"---> 16 print_friday_message()\n",
"\n",
"<ipython-input-1-4be1945adbe2> in print_friday_message()\n",
" 12\n",
" 13 def print_friday_message():\n",
"---> 14 print_message(\"Friday\")\n",
" 15\n",
" 16 print_friday_message()\n",
"\n",
"<ipython-input-1-4be1945adbe2> in print_message(day)\n",
" 9 \"sunday\": \"Aw, the weekend is almost over.\"\n",
" 10 }\n",
"---> 11 print(messages[day])\n",
" 12\n",
" 13 def print_friday_message():\n",
"\n",
"KeyError: 'Friday'```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Identifying Syntax Errors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read the code below, and (without running it) try to identify what the errors are.\n",
"\n",
"Run the code, and read the error message. Is it a SyntaxError or an IndentationError?\n",
"Fix the error.\n",
"Repeat steps 2 and 3, until you have fixed all the errors.\n",
"```\n",
"def another_function # Falta ():\n",
" print(\"Syntax errors are annoying.\")\n",
" print(\"But at least python tells us about them!\") # Mala indentación\n",
" print(\"So they are usually not too hard to fix.\")```"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Syntax errors are annoying.\n",
"But at least python tells us about them!\n",
"So they are usually not too hard to fix.\n"
]
}
],
"source": [
"def another_function():\n",
" print(\"Syntax errors are annoying.\")\n",
" print(\"But at least python tells us about them!\")\n",
" print(\"So they are usually not too hard to fix.\")\n",
"another_function()"
]
}
],
"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
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Analyzing Patient Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are studying inflammation in patients who have been given a new treatment for arthritis, and need to analyze the first dozen data sets of their daily inflammation. The data sets are stored in comma-separated values (CSV) format: each row holds information for a single patient, and the columns represent successive days"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We want to:\n",
"\n",
" * load that data into memory,\n",
" * calculate the average inflammation per day across all patients, and\n",
" * plot the result.\n",
"\n",
"To do all that, we’ll have to learn a little bit about programming."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python as a calculator\n",
"\n",
"Any Python interpreter can be used as a calculator:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 + 5 * 4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here you have an overview of math operators:\n",
"\n",
"| Operators | Operations | Examples |\n",
"| :--------:| :---------------- | ---------- |\n",
"| + | Addition | 2 + 3 = 5 |\n",
"| - | Substraction | 3 - 2 = 1 |\n",
"| * | Multiplication | 2 * 3 = 6 |\n",
"| / | Division | 3 / 2 = 1.5|\n",
"| // | Integer division | 7 // 3 = 2 |\n",
"| % | remainder division| 7 % 2 = 1 |\n",
"| ** | Exponent | 2**3 = 8 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This only outputs a value. To use values in any manner, we have to access them in memory by using a variable.\n",
"\n",
"In Python, variable names:\n",
"\n",
"* can include letters, digits, and underscores\n",
"* cannot start with a digit\n",
"* are case sensitive.\n",
"\n",
"Let's assign a value 55 to variable named ``weight_kg``:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"weight_kg = 55 # we assign the value 55 to the variable weight_kg using ="
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We use ``print`` to output the value of a variable. Is a built-in function:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"55\n"
]
}
],
"source": [
"print(weight_kg) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can do some arithmetics with variables:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Weight in pounds: 121.00000000000001\n"
]
}
],
"source": [
"print(\"Weight in pounds: \", 2.2 * weight_kg) # Don't forget to explain about printing\n",
" # types, strings and comma separation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What the ...?? why is not simply giving 121.0??? <br>\n",
"(<strong>Pro tip:</strong> <em>Because 2.2 is a number with no exact float representation, so it multiplies by the closest number with a float representation. That produces a 1 at the last decimal position so Python realices that there is something non-zero at the end and plots it.</em>)<br><br>\n",
"Well it looks 121 enought to me.... Ok let's continue. \n",
"\n",
"We can change the value of a variable by re-assigning it:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Weight in kilograms is now: 57.5\n"
]
}
],
"source": [
"weight_kg = 57.5\n",
"print('Weight in kilograms is now:', weight_kg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Variables as sticky notes with the name put in a particular value.<br>\n",
"<img src=\"https://scw-ss.github.io/python-novice-inflammation-2016-06-27-cfmehu/fig/python-sticky-note-variables-01.svg\">\n",
"\n",
"We can create new variables from others:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"weight in kilograms: 57.5 and in pounds: 126.50000000000001\n"
]
}
],
"source": [
"weight_lb = 2.2 * weight_kg\n",
"\n",
"print( 'weight in kilograms: ', weight_kg, 'and in pounds: ', weight_lb) #concatenating complex expressions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://scw-ss.github.io/python-novice-inflammation-2016-06-27-cfmehu/fig/python-sticky-note-variables-02.svg\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What happens to ``weight_lb`` if you change ``weight_kg``?:"
]
},