Commit ad54ac4c authored by Emilio Ambite's avatar Emilio Ambite

Repositorio de notebooks de python para SCW 2019

parent e7783177
This source diff could not be displayed because it is too large. You can view the blob instead.
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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"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"
]
},
{
"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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"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",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<li> You forgot to create a variable before using it\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<li> You try to read(write) a write-only(read-only) file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises\n",
"\n",
"### Reading Error Messages "
]
},
{
"cell_type": "raw",
"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?\n",
"What is the function name where the error occurred?\n",
"On which line number in this function did the error occurr?\n",
"What is the type of error?\n",
"What is the error message?\n",
"How to fix it?\n",
"\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": "raw",
"metadata": {},
"source": [
"Read the code below, and (without running it) try to identify what the errors are.\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 \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.\")"
]
},
{
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lists\n",
"They are used to store many values. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Difference with array?\n",
"<li>They are very similar but they do not belong to numpy, and do not have some of the attributes of numpy arrays, such as mean sd max min etc... Beside this they are pretty similar in concept\n",
"<li>We create a list the same "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Selecting elements of a list:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can loop over a list:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List are also pretty similar to strings. There is an <strong>IMPORTANT</strong> difference between strings and lists: Elements in a list can be modified but not ones in a string:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### List of Lists or nested lists\n",
"Making a list whose elements are also lists."
]
},
{
"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:"
]
},
{
"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": [
"We can print the first element of the list <code>x</code>:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Printing first element of <code>x[0]</code> list:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Modifying list contents\n",
"Many ways to change a string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"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": [
"<strong>Caution here!</strong>: List are treated slightly conterintuitive:<br>\n",
"<em>Pro-tip: Technically it is said that lists are copied \"by reference\" instead of \"by value\"</em>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise\n",
"\n",
"#### Turn string into a list\n",
"\n",
"Use a for-loop to convert the string “hello” into a list of letters:\n",
"\n",
"<code>\n",
"[\"h\", \"e\", \"l\", \"l\", \"o\"]\n",
"<code/>\n",
"\n",
"Hint: You can create an empty list like this:\n",
"\n",
"``my_list = []``"
]
},
{
"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": [
"We can make a single copy of the original list using list built-in"
]
},
{
"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 panel-heading\">\n",
"<h3>Reminder: When not to use \"list\" method?</h3>\n",
"</div>\n",
"<div class=\"panel panel-body\">\n",
"<li>Reference copy uses less resources \n",
"<li>Is the default when relating lists through assignment\n",
"<li>But whether you need to do a reference or a copy is your choice!!\n",
"</div>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise\n",
"\n",
"#### Overloading\n",
"\n",
"`+` usually means addition, but when used on strings or lists, it means “concatenate”. Given that, what do you think the multiplication operator * does on lists? In particular, what will be the output of the following code?\n",
"\n",
"<code>\n",
"counts = [2, 4, 6, 8, 10]\n",
"repeats = counts * 2\n",
"<code/>"
]
},
{
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Repeating Actions with Loops"
]
},