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"
]
},
{
"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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bad approach: tedious and if the string is larger it misses some characters, and if it shorter:"
]
},
{
"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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The method supports strings of any lenght:"
]
},
{
"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": "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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"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": [
"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": {},
"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": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"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": 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"
]
},
{
"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 diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
import sys
print('sys.argv is', sys.argv)
This diff is collapsed.
This diff is collapsed.
import sys
count = 0
for line in sys.stdin:
count += 1
print(count, 'lines in standard input')
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
def rectangle_area(coords):
x0, y0, x1, y1 = coords
return (x1 - x0) * (x1 - y0)
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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