From 41c89abca9eff3059c4fac2bf5e3c7d3493aa7a1 Mon Sep 17 00:00:00 2001 From: Chris Hench Date: Mon, 8 Aug 2016 09:24:36 -0700 Subject: [PATCH 1/4] fixed typos --- Day_1/00_Intro.md | 15 ++++----- Day_1/01_Running-Python.md | 51 ++++++++++++++--------------- Day_1/02_Jupyter Notebooks.ipynb | 16 +++++---- Day_1/03_Variables_Assignment.ipynb | 10 +++--- Day_1/04_Types_Conversion.ipynb | 11 ++++++- Day_1/05_Strings.ipynb | 12 ++++--- Day_1/06_Built-ins.ipynb | 8 ++--- Day_1/madlib.py | 32 +++++++++--------- 8 files changed, 83 insertions(+), 72 deletions(-) diff --git a/Day_1/00_Intro.md b/Day_1/00_Intro.md index 2182588..f9ad976 100644 --- a/Day_1/00_Intro.md +++ b/Day_1/00_Intro.md @@ -13,7 +13,7 @@ This unit provides a basic introduction to Python. By the end of the week, you s > ## Learning Objectives > -> * Explain the difference between knowing a programming language knowing how to program. +> * Explain the difference between knowing a programming language and knowing how to program. > * Explain how programming languages can differ. > * Give useful debugging tips. > * Offer helpful resource websites. @@ -21,15 +21,15 @@ This unit provides a basic introduction to Python. By the end of the week, you s ### What it means to "know how to program" -Most programmers can program in more than one language. That's because they know *how to program* generally, as opposed to "knowing" Python, R, Ruby, or whatever. +Most programmers can program in more than one language. That's because they know *how to program* generally, as opposed to "knowing" Python, R, Ruby, or whatever. -In other words, programming is an extendible skill. Basic programming concepts -- conditionals, for loops, functions -- can be found in almost any programming language. +In other words, programming is an extendible skill. Basic programming concepts -- conditionals, for loops, functions -- can be found in almost any programming language. -That being said, programming languages differ from one anther in the following ways: +That being said, programming languages differ from one another in the following ways: 1. **Syntax**: whether to add a semicolon at the end of each line, etc. -2. **Usage**: javascript is for building websites, R is for statistics, Python is general purpose, etc. -3. **Level**: how close are you to the hardware. 'C' is often considered to be the lowest (or one of the lowest) level languages. +2. **Usage**: JavaScript is for building websites, R is for statistics, Python is general purpose, etc. +3. **Level**: how close you are to the hardware. 'C' is often considered to be the lowest (or one of the lowest) level languages. 4. **Object-oriented:** "objects" are data + functions. Some programming languages are object-oriented (e.g. Python) and some aren't (e.g. C). 5. **Many more**: Here's a [list](https://en.wikipedia.org/wiki/List_of_programming_languages_by_type) of all the types of programming languages out there. @@ -43,7 +43,7 @@ Thus "knowing how to program" means learning how to *think* like a programmer, n ![xkcd](http://sslimgs.xkcd.com/comics/wisdom_of_the_ancients.png) -Here's the sad reality: When you're programming, 80% or more of you time will be spent debugging, looking stuff up (like program-specific syntax, documentation for packages, useful functions, etc.), or testing. This does not just apply to beginner or intermediate programmers, although you will grow more "fluent" over time. +Here's the sad reality: When you're programming, 80% or more of your time will be spent debugging, looking stuff up (like program-specific syntax, documentation for packages, useful functions, etc.), or testing. This does not just apply to beginner or intermediate programmers, although you will grow more "fluent" over time. If you're a good programmer, you're a good detective! @@ -61,4 +61,3 @@ If you're a good programmer, you're a good detective! * google: name-of-program + text in error message * Remove user- and data-specific information first! * See if you can find examples that do and don’t produce the error - diff --git a/Day_1/01_Running-Python.md b/Day_1/01_Running-Python.md index 5707caf..2f3b250 100644 --- a/Day_1/01_Running-Python.md +++ b/Day_1/01_Running-Python.md @@ -2,7 +2,7 @@ > ## Questions > How can I run Python programs? -> +> > ## Learning Objectives > > * Explain how the shell relates to other programs like Python. @@ -10,29 +10,29 @@ > * Run a Python script from the shell. > * Understand the importance of IDE's and program-specific tools. > * Find which version of Python is used by the interpreter. -> * Start an ipython notebook server from the shell +> * Start an iPython notebook server from the shell ## Running Python in Shell: Interactive Mode If you took Programming FUN!damentals, you used the shell as the interface between us and the UNIX operating system. -But we can also use shell to interact with other programs. For example, we can run a Python interpreter right in shell. An **interpreter** is a program that reads and executes code. This includes source code and scripts. +But we can also use shell to interact with other programs. For example, we can run a Python interpreter right in shell. An **interpreter** is a program that reads and executes code. This includes source code and scripts. ~~~python $ python -Python 2.7.6 (default, Mar 22 2014, 22:59:56) +Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> ~~~ -The `>>>` is Python's way of telling you that you are in **interactive mode**. In interactive mode, what you type is immediately run. +The `>>>` is Python's way of telling you that you are in **interactive mode**. In interactive mode, what you type is immediately run. ~~~python >>> 5 5 ->>> print (5*7) +>>> print(5*7) 35 >>> "hello" * 4 'hellohellohellohello' @@ -40,7 +40,7 @@ The `>>>` is Python's way of telling you that you are in **interactive mode**. I ~~~ -To escape the Python interpreter and go back to Shell, type +To escape the Python interpreter and go back to Shell, type ~~~python >>> quit() @@ -50,22 +50,22 @@ $ Notice that the terminal window will go back to bash, giving you a `$` prompt. > #### Which Python? -> +> > Python, like other programs, come in versions. And with each version, the > features may change. That means that running code made for Python 2 might > not work on Python 3. This is one of the motivations behind BCE - > to standardize software versions for teaching, etc. -> -> To see which version of Python you have, enter the command `which python` in +> +> To see which version of Python you have, enter the command `which python` in > bash. You can use the `which` command with other programs, too. ## Running Python in Shell: Normal Mode Python has two basic modes: normal and interactive. Interactive mode allows you to test out and see what Python will do. If you ever feel the need to play with new Python statements, go into interactive mode and try them out. -If you quit from the Python interpreter and enter it again, the definitions you just made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. +If you quit from the Python interpreter and enter it again, the definitions you just made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. -This is known as creating a **script** (just like the shell scripts we created earlier). Python scripts have the ".py" extension to let everyone know (including the operating system) it is a Python program. +This is known as creating a **script** (just like the shell scripts we created in Programming FUN!damentals). Python scripts have the ".py" extension to let everyone know (including the operating system) it is a Python program. The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. To run a program, make sure you're in bash mode in your terminal (the default mode), `cd` into the directory in which the Python program (.py file) is located, and then enter the command `python [name of file]`. Here's an example: @@ -73,34 +73,32 @@ The normal mode is the mode where the scripted and finished .py files are run in $ python madlib.py ~~~ -Once the program ends, it will give a `$` prompt, returning to bash mode. +Once the program ends, it will give a `$` prompt, returning to bash mode. ## IDEs and other Tools -It's common to write python programs using either a text editor or an interactive/integrated development environment (IDE).An IDE is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools and a debugger. Some of them come with package managers and other features, too. +It's common to write python programs using either a text editor or an interactive/integrated development environment (IDE). An IDE is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools, and a debugger. Some of them come with package managers and other features, too. There are many Python IDE's. You can see a comparison [here](https://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments#Python) > #### Editors v. IDEs -> -> Notepad++, Vim, SublimeText etc are text editors. They is not specific -> to Python, or to any other language. They are not an IDE. That said, -> these editors are extensible, and through the use of plugins they allow the -> user to implement IDE like functionality for as broad a range of languages +> +> Notepad++, Vim, SublimeText etc are text editors. They are not specific +> to Python, or to any other language. They are not an IDE. That said, +> these editors are extensible, and through the use of plugins they allow the +> user to implement IDE-like functionality for as broad a range of languages > or syntaxes as plugin writers care to cover. -## Jupiter Notebooks +## Jupyter Notebooks This course will be using a Jupyter Notebook to interact with Python. The bit of extra setup is well worth it because the Notebook provides code completion and other helpful features. -Jupyter Notebooks are included in the Anaconda distribution. Notebook files have the extension ".ipynb" to distinguish them from plain-text Python programs. +Jupyter Notebooks are included in the Anaconda distribution. Notebook files have the extension ".ipynb" to distinguish them from plain-text Python programs. To start a notebook server, simply type -FIXME: ipython or jupyter in the code below? - ~~~bash -$ ipython notebook +$ jupyter notebook ~~~ This will start a Jupyter Notebook server and open your default web browser. @@ -114,10 +112,9 @@ This has several advantages: - You can easily type, edit, and copy and paste blocks of code. - Tab complete allows you to easily access the names of things you are using and learn more about them. -- It allows you to annotate your code with links, different sized text, bullets, etc to make it more accessible to you and your collaborators. +- It allows you to annotate your code with links, different sized text, bullets, etc., to make it more accessible to you and your collaborators. - It allows you to display figures next to the code that produces them to tell a complete story of the analysis. - The notebook is stored as JSON but can be saved as a .py file if you would like to run it from the bash shell or a python interpreter. -- Just like a webpage, the saved notebook looks different to what you see when it gets rendered by your browser. +- Just like a webpage, the saved notebook looks different than what you see when it gets rendered by your browser. Let's get programming! - diff --git a/Day_1/02_Jupyter Notebooks.ipynb b/Day_1/02_Jupyter Notebooks.ipynb index fc44107..038f44b 100644 --- a/Day_1/02_Jupyter Notebooks.ipynb +++ b/Day_1/02_Jupyter Notebooks.ipynb @@ -30,7 +30,7 @@ "\n", "**Don't panic!**\n", "\n", - "*You can't break anything!* You can always get another copy of any notebook from the workshop on the github repo **FIXME**" + "*You can't break anything!* You can always get another copy of any notebook from the workshop on the [github repo](https://github.com/dlab-berkeley/python-intensive)." ] }, { @@ -152,7 +152,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You can also split cells by puting your cursor on the line you want to split, and clicking on Edit --> Split cell. Split the cell below after the `c = 2` line" + "You can also split cells by putting your cursor on the line you want to split, and clicking on Edit --> Split cell. Split the cell below after the `c = 2` line" ] }, { @@ -199,6 +199,8 @@ "* to create\n", "* bullet lists.\n", "\n", + "but\n", + "\n", "1. Use numbers\n", "2. to create\n", "3. numbered lists.\n", @@ -222,7 +224,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "By default, cells in jupyter notebook are code cells, which means it's expecting you to enter Python code. But you can change the cell format by changing the dropdown item on the top from \"Code\" to \"Markdown\"\n", + "By default, cells in jupyter notebook are code cells, which means it's expecting you to enter Python code. But you can change the cell format by changing the dropdown item on the top from \"Code\" to \"Markdown\".\n", "\n", "Enter some markdown text in the cell below, and execute it. Be sure to change the cell from code to markdown!" ] @@ -285,7 +287,7 @@ "\n", "Jupyter remembers everything it executed, **even if it's not currently displayed in the notebook**.\n", "\n", - "To clear everything from Jupyter use Kernel->Restart in the menu." + "To clear everything from Jupyter use Kernel-->Restart in the menu." ] }, { @@ -313,7 +315,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now use Kernel->Restart in the menu!" + "Now use Kernel-->Restart in the menu!" ] }, { @@ -352,9 +354,9 @@ "source": [ "## Exiting Jupyter\n", "\n", - "When you cose your Jupyter notebook window, all of your values will be lost. But you can save your code for a later time.\n", + "When you close your Jupyter notebook window, all of your values will be lost. But you can save your code for a later time.\n", "\n", - "To end the Jupyter server, go back to your shell and type `CNTL+C`" + "To end the Jupyter server, go back to your shell and type `CTRL+C`" ] }, { diff --git a/Day_1/03_Variables_Assignment.ipynb b/Day_1/03_Variables_Assignment.ipynb index 652581f..f0e822e 100644 --- a/Day_1/03_Variables_Assignment.ipynb +++ b/Day_1/03_Variables_Assignment.ipynb @@ -13,11 +13,11 @@ "- Challenges: 5 min\n", "\n", "**Questions**\n", - "- How can I store data in programs?\n", + "- \"How can I store data in programs?\"\n", "\n", "**Learning Objectives**\n", - "- Write programs that assign scalar values to variables and perform calculations with those values.\n", - "- Correctly trace value changes in programs that use scalar assignment.\n", + "- \"Write programs that assign scalar values to variables and perform calculations with those values.\"\n", + "- \"Correctly trace value changes in programs that use scalar assignment.\"\n", "\n", "* * * * *" ] @@ -33,7 +33,7 @@ "* Variables are names for values.\n", "* In Python the `=` symbol assigns the value on the right to the name on the left.\n", "* The variable is created when a value is assigned to it.\n", - "* Here's Python that assigns an age to a variable `age`\n", + "* Here's Python code that assigns an age to a variable `age`\n", " and a name in quotation marks to a variable `first_name`.\n" ] }, @@ -134,7 +134,7 @@ "metadata": {}, "source": [ "* The last line of an error message is usually the most informative.\n", - "* We will look at error messages in detail [later]({{ site.github.url }}/05-error-messages/).\n", + "* We will look at error messages in detail [later](https://github.com/dlab-berkeley/python-intensive/blob/master/Day_3/15_Errors.ipynb).\n", "\n", "## Python is case-sensitive.\n", "\n", diff --git a/Day_1/04_Types_Conversion.ipynb b/Day_1/04_Types_Conversion.ipynb index 6b7eaae..cf1fea6 100644 --- a/Day_1/04_Types_Conversion.ipynb +++ b/Day_1/04_Types_Conversion.ipynb @@ -433,7 +433,7 @@ "source": [ "## Challenge 2: Division Types\n", "\n", - "The `//` operator calcultaes the whole-number result of division, while the '%' operator calculates the remainder from division:" + "The `//` operator calculates the whole-number result of division, while the '%' operator calculates the remainder from division:" ] }, { @@ -464,6 +464,15 @@ "If `num_subjects` is the number of subjects taking part in a study, and `num_per_survey` is the number that can take part in a single survey, write an expression that calculates the number of surveys needed to reach everyone once." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/Day_1/05_Strings.ipynb b/Day_1/05_Strings.ipynb index ee1d97e..180885f 100755 --- a/Day_1/05_Strings.ipynb +++ b/Day_1/05_Strings.ipynb @@ -7,14 +7,14 @@ "# Strings\n", "\n", "**Time**\n", - "-teaching: FIXME\n", - "-exercises: FIXME\n", + "- Teaching: 5 min\n", + "- Exercises: 10 min\n", "\n", "**Questions**:\n", - "FIXME\n", + "- \"How do I manipulate strings (text)?\"\n", "\n", "**Learning Objectives**:\n", - "FIXME\n", + "- Become familiar with the string type and its methods\n", "* * * * *\n" ] }, @@ -433,7 +433,9 @@ "source": [ "# Keypoints\n", "\n", - "FIXME" + "- Some mathematical operators can be used on strings\n", + "- Strings can be indexed, Python indexing always starts at 0!\n", + "- Strings, and other types, have their own methods, which are called using dots after the variable, and then the method name." ] }, { diff --git a/Day_1/06_Built-ins.ipynb b/Day_1/06_Built-ins.ipynb index 42f34d6..391c6e6 100644 --- a/Day_1/06_Built-ins.ipynb +++ b/Day_1/06_Built-ins.ipynb @@ -7,8 +7,8 @@ "# Built-in Functions and Help\n", "\n", "**Time**:\n", - "teaching: 10\n", - "exercises: 10\n", + "- Teaching: 10 min\n", + "- Exercises: 10 min\n", "\n", "**Questions**\n", "- \"How can I use built-in functions?\"\n", @@ -165,7 +165,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* We can specify the number of decimal places we ant.\n" + "* We can specify the number of decimal places we want.\n" ] }, { @@ -282,7 +282,7 @@ " \n", "Read more about objects, classes and methods [here](https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming)\n", "\n", - "Check out our Python glossary [here](A_Glossary.md)." + "Check out our Python glossary [here](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md)." ] }, { diff --git a/Day_1/madlib.py b/Day_1/madlib.py index ed122b1..f7aa741 100644 --- a/Day_1/madlib.py +++ b/Day_1/madlib.py @@ -1,9 +1,9 @@ """ String Substitution for a Mad Lib Adapted from code by Kirby Urner -""" - -story = """ +""" + +story = """ Once upon a time, deep in an ancient jungle, there lived a %(animal)s. This %(animal)s liked to eat %(food)s, but the jungle had @@ -17,18 +17,20 @@ leaving a large supply of %(food)s. The End -""" +""" + + +def tellStory(): + userPicks = dict() + addPick('animal', userPicks) + addPick('food', userPicks) + addPick('city', userPicks) + print(story % userPicks) + -def tellStory(): - userPicks = dict() - addPick('animal', userPicks) - addPick('food', userPicks) - addPick('city', userPicks) - print(story % userPicks) - def addPick(cue, dictionary): - prompt = "Enter a specific example for %s: " % cue - dictionary[cue] = input(prompt) + prompt = "Enter a specific example for %s: " % cue + dictionary[cue] = input(prompt) -tellStory() -input("Press Enter to end the program.") +tellStory() +input("Press Enter to end the program.") From d4c7c026ec0f88bf5b0782e8927659cfd4060cb5 Mon Sep 17 00:00:00 2001 From: Chris Hench Date: Mon, 8 Aug 2016 14:29:43 -0700 Subject: [PATCH 2/4] fixed typos, change some examples, added some comments, added schedule to readme, whitespace to glossary, fixed links --- Day_1/05_Strings.ipynb | 94 +++++--- Day_2/07_Lists.ipynb | 139 +++++++++--- Day_2/08_Loops.ipynb | 153 ++++++------- Day_2/09_Conditionals.ipynb | 169 +++++++------- Day_2/10_Functions.ipynb | 56 +++-- Day_2/11_Scope.ipynb | 57 ++--- Day_3/12_Dictionaries.ipynb | 92 ++++---- Day_3/13_Files.ipynb | 56 +++-- Day_3/14_Libraries.ipynb | 296 +++++++++++++++++++++++-- Day_3/15_Errors.ipynb | 113 +++++----- Day_3/16_Comprehensions.ipynb | 32 ++- Day_3/17_Beautiful-Code.ipynb | 63 ++++-- Day_3/capitals2.csv | 402 +++++++++++++++++----------------- {Day_2 => Day_3}/myfile.txt | 0 Glossary.md | 5 +- README.md | 10 +- 16 files changed, 1092 insertions(+), 645 deletions(-) rename {Day_2 => Day_3}/myfile.txt (100%) diff --git a/Day_1/05_Strings.ipynb b/Day_1/05_Strings.ipynb index 180885f..7b0c498 100755 --- a/Day_1/05_Strings.ipynb +++ b/Day_1/05_Strings.ipynb @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -88,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -99,7 +99,7 @@ "'o'" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -117,7 +117,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -128,7 +128,7 @@ "'J'" ] }, - "execution_count": 5, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -139,7 +139,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -150,7 +150,7 @@ "'n'" ] }, - "execution_count": 6, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -170,7 +170,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -181,7 +181,7 @@ "'Joha'" ] }, - "execution_count": 7, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -192,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -203,7 +203,7 @@ "'Johan'" ] }, - "execution_count": 8, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -221,7 +221,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -232,7 +232,7 @@ "'Johan'" ] }, - "execution_count": 9, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -243,7 +243,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -254,7 +254,7 @@ "' Gambolputty'" ] }, - "execution_count": 10, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -280,17 +280,17 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", + "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m str.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m str.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], @@ -307,7 +307,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 11, "metadata": { "collapsed": true }, @@ -325,7 +325,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -336,7 +336,7 @@ "'JOHAN GAMBOLPUTTY'" ] }, - "execution_count": 16, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -351,14 +351,58 @@ "source": [ "You have to use the parenthesis at the end because upper is a method of the string class.\n", "\n", - "\n", - "\n", + "Don't forget, simply calling the method does not change the original variable, you must reassign the variable:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Johan Gambolputty\n" + ] + } + ], + "source": [ + "print(fullName)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "JOHAN GAMBOLPUTTY\n" + ] + } + ], + "source": [ + "fullName = fullName.upper()\n", + "print(fullName)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "For what its worth, you don't need to have a variable to use the upper() method, you could use it on the string itself." ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 16, "metadata": { "collapsed": false }, @@ -369,7 +413,7 @@ "'JOHANN GAMBOLPUTTY'" ] }, - "execution_count": 17, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } diff --git a/Day_2/07_Lists.ipynb b/Day_2/07_Lists.ipynb index e418b68..5c88f3d 100644 --- a/Day_2/07_Lists.ipynb +++ b/Day_2/07_Lists.ipynb @@ -7,14 +7,17 @@ "# Lists\n", "\n", "**Time**\n", - "- teaching: FIXME\n", - "- exercises: FIXME\n", + "- teaching: 10 min\n", + "- exercises: 10 min\n", "\n", "**Questions**:\n", - "- FIXME\n", + "- \"How do I organize several data types in an ordered manner?\"\n", + "- \"How can I modify this collection of data?\"\n", "\n", "**Learning Objectives**:\n", - "- FIXME\n", + "- \"Understand how to create and modify a list\"\n", + "- \"Understand what a list can and can't do\"\n", + "- \"Become familiar with common list methods\"\n", "* * * * *\n", "\n", "A list is an ordered, indexable collection of data. Lets say you're doing a study on the following countries:\n", @@ -30,12 +33,12 @@ "You could put that data into a list \n", "\n", "* contain data in square brackets `[...]`, \n", - "* each values separated by commas `,`." + "* each value is separated by a comma `,`." ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -46,7 +49,7 @@ "list" ] }, - "execution_count": 19, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -65,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -76,7 +79,7 @@ "5" ] }, - "execution_count": 20, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -98,7 +101,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -126,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -157,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -183,7 +186,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -200,16 +203,35 @@ "print(country_list[:4])" ] }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Sierra Leone', 'Denmark', 'Japan']\n" + ] + } + ], + "source": [ + "print(country_list[2:])" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Lists’ values can be replaced by assigning to them." + "## Lists’ values can be replaced by assigning to specific indices." ] }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -239,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -251,7 +273,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmystring\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Donut\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmystring\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'C'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmystring\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Donut\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmystring\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'C'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } @@ -261,6 +283,37 @@ "mystring[0] = 'C'" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Mutable also means that any other variables pointing to a list will be changed accordingly:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "new_list: ['Iran', 'Canada', 'Sierra Leone', 'Denmark', 'Japan']\n", + "new_list: ['India', 'Canada', 'Sierra Leone', 'Denmark', 'Japan']\n" + ] + } + ], + "source": [ + "new_list = country_list\n", + "print(\"new_list: \", new_list)\n", + "\n", + "country_list[0] = \"India\"\n", + "print(\"new_list: \", new_list)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -275,17 +328,17 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", + "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m country_list.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m country_list.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], @@ -302,7 +355,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -311,7 +364,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "['Iran', 'Canada', 'Sierra Leone', 'Denmark', 'Japan', 'United States']\n" + "['India', 'Canada', 'Sierra Leone', 'Denmark', 'Japan', 'United States']\n" ] } ], @@ -332,7 +385,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 13, "metadata": { "collapsed": false }, @@ -341,8 +394,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "original list was: ['Iran', 'Canada', 'Sierra Leone', 'Denmark', 'Japan', 'United States']\n", - "the list is now: ['Iran', 'Canada', 'Sierra Leone', 'Japan', 'United States']\n" + "original list was: ['India', 'Canada', 'Sierra Leone', 'Denmark', 'Japan', 'United States']\n", + "the list is now: ['India', 'Canada', 'Sierra Leone', 'Japan', 'United States']\n" ] } ], @@ -363,7 +416,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 14, "metadata": { "collapsed": false }, @@ -390,7 +443,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 15, "metadata": { "collapsed": false }, @@ -422,14 +475,14 @@ "## Indexing beyond the end of the collection is an error.\n", "\n", "* Python reports an `IndexError` if we attempt to access a value that doesn't exist.\n", - " * This is a kind of [runtime error]({{ site.github.url }}/05-error-messages/).\n", + " * This is a kind of [runtime error](https://github.com/dlab-berkeley/python-intensive/blob/master/Day_3/15_Errors.ipynb).\n", " * Cannot be detected as the code is parsed\n", " because the index might be calculated based on data." ] }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 16, "metadata": { "collapsed": false }, @@ -441,7 +494,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcountry_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m99\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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcountry_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m99\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: list index out of range" ] } @@ -476,7 +529,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 17, "metadata": { "collapsed": false }, @@ -515,7 +568,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 18, "metadata": { "collapsed": false }, @@ -550,7 +603,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 19, "metadata": { "collapsed": true }, @@ -561,6 +614,15 @@ " \"Liz\", \"Olivia\", \"Will\", \"Ogi\", \"Melanie\", \"Jessica\"]" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -574,7 +636,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 20, "metadata": { "collapsed": true }, @@ -583,6 +645,15 @@ "letters = ['s', 'p', 'a', 'm']" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/Day_2/08_Loops.ipynb b/Day_2/08_Loops.ipynb index eba7285..8779cab 100644 --- a/Day_2/08_Loops.ipynb +++ b/Day_2/08_Loops.ipynb @@ -7,8 +7,8 @@ "# Loops\n", "\n", "**Time**\n", - "-teaching: 10 min\n", - "-exercises: 10\n", + "- Teaching: 10 min\n", + "- Exercises: 10 min\n", "\n", "**Questions**:\n", "- \"How can I make a program do many things?\"\n", @@ -36,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -65,7 +65,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -99,17 +99,17 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "ename": "IndentationError", - "evalue": "expected an indented block (, line 2)", + "evalue": "expected an indented block (, line 2)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print(number)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print(number)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" ] } ], @@ -127,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -156,16 +156,17 @@ "* The loop variable, `number`, is what changes for each *iteration* of the loop.\n", " * The \"current thing\".\n", "\n", - "## Loop variables can be called anything.\n", + "## Loop variables can be called anything!!!\n", "\n", "* As with all variables, loop variables are:\n", " * Created on demand.\n", - " * Meaningless: their names can be anything at all." + " * Meaningless: their names can be anything at all.\n", + " * Placeholders for the loop" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -197,7 +198,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -236,7 +237,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -266,12 +267,14 @@ "\n", "* A common pattern in programs is to:\n", " 1. Initialize an *accumulator* variable to zero, the empty string, or the empty list.\n", - " 2. Update the variable with values from a collection.\n" + " 2. Update the variable with values from a collection.\n", + " \n", + "If only one argument is given to `range`, the minimum will default to 0. But two arguments may also be given:\n" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -287,8 +290,8 @@ "source": [ "# Sum the first 10 integers.\n", "total = 0\n", - "for number in range(10):\n", - " total = total + (number + 1)\n", + "for number in range(1, 11): # start at one, end at 10\n", + " total = total + number\n", "print(total)" ] }, @@ -296,11 +299,34 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* Read `total = total + (number + 1)` as:\n", - " * Add 1 to the current value of the loop variable `number`.\n", - " * Add that to the current value of the accumulator variable `total`.\n", - " * Assign that to `total`, replacing the current value.\n", - "* We have to add `number + 1` because `range` produces 0..9, not 1..10." + "* Read `total = total + number` as:\n", + " * Add the current value of the loop variable `number` to the current value of the accumulator variable `total`.\n", + " * Assign this new value to to `total`, replacing the current value.\n", + " \n", + "Instead of writing `total = total + number`, this can be simplified to `total += number`. This will reassign total to the current value of total plus the current value of number:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n" + ] + } + ], + "source": [ + "# Sum the first 10 integers.\n", + "total = 0\n", + "for number in range(1, 11): # start at one, end at 10\n", + " total += number\n", + "print(total)" ] }, { @@ -312,7 +338,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -347,23 +373,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "NameError", - "evalue": "name '____' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0moriginal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"tin\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mchar\u001b[0m \u001b[0;32min\u001b[0m \u001b[0moriginal\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mNameError\u001b[0m: name '____' is not defined" - ] - } - ], + "outputs": [], "source": [ "original = \"tin\"\n", "result = ____\n", @@ -384,23 +398,11 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "NameError", - "evalue": "name '____' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mtotal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mword\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m\"red\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"green\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"blue\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0m____\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m____\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mword\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtotal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mNameError\u001b[0m: name '____' is not defined" - ] - } - ], + "outputs": [], "source": [ "# Total length of the strings in the list: [\"red\", \"green\", \"blue\"] => 12\n", "total = 0\n", @@ -411,23 +413,11 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "NameError", - "evalue": "name '____' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# List of word lengths: [\"red\", \"green\", \"blue\"] => [3, 5, 4]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mlengths\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mword\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m\"red\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"green\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"blue\"\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[0mlengths\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlengths\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m____\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m____\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlengths\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mNameError\u001b[0m: name '____' is not defined" - ] - } - ], + "outputs": [], "source": [ "# List of word lengths: [\"red\", \"green\", \"blue\"] => [3, 5, 4]\n", "lengths = ____\n", @@ -438,23 +428,11 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "ename": "NameError", - "evalue": "name '____' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Concatenate all words: [\"red\", \"green\", \"blue\"] => \"redgreenblue\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mwords\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m\"red\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"green\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"blue\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0m____\u001b[0m \u001b[0;32min\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mNameError\u001b[0m: name '____' is not defined" - ] - } - ], + "outputs": [], "source": [ "# Concatenate all words: [\"red\", \"green\", \"blue\"] => \"redgreenblue\"\n", "words = [\"red\", \"green\", \"blue\"]\n", @@ -466,7 +444,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": { "collapsed": true }, @@ -482,12 +460,14 @@ "source": [ "## Challenge 3: Multiple Operations\n", "\n", - "Below is a list of presidents. Create a new list that contains only the last name of each president." + "Below is a list of presidents. Create a new list that contains only the last name of each president.\n", + "\n", + "HINT: Look at string methods!" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "metadata": { "collapsed": false }, @@ -513,6 +493,15 @@ "Using `range` and a for loop, calculate the factorial of 42 (the product of all integers up to and including 42)." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/Day_2/09_Conditionals.ipynb b/Day_2/09_Conditionals.ipynb index bd4d80e..0fcff62 100644 --- a/Day_2/09_Conditionals.ipynb +++ b/Day_2/09_Conditionals.ipynb @@ -8,8 +8,8 @@ "\n", "**Time**\n", "\n", - "- teaching: 10 min\n", - "- exercises: 5\n", + "- Teaching: 10 min\n", + "- Exercises: 5 min\n", "\n", "**Questions**:\n", "\n", @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -52,18 +52,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "3.54 is large\n" + "84 is old\n" ] } ], "source": [ - "mass = 3.54\n", - "if mass > 3.0:\n", - " print(mass, 'is large')\n", + "age = 84\n", + "if age > 60:\n", + " print(age, 'is old')\n", "\n", - "mass = 2.07\n", - "if mass > 3.0:\n", - " print (mass, 'is large')" + "age = 20\n", + "if age > 60:\n", + " print (age, 'is large')" ] }, { @@ -79,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -88,16 +88,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "3.54 is large\n", - "9.22 is large\n" + "88 is old\n", + "67 is old\n" ] } ], "source": [ - "masses = [3.54, 2.07, 9.22, 1.86, 1.71]\n", - "for mass in masses:\n", - " if mass > 3.0:\n", - " print(mass, 'is large')" + "ages = [20, 43, 12, 88, 67]\n", + "for age in ages:\n", + " if age > 60:\n", + " print(age, 'is old')" ] }, { @@ -112,7 +112,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -121,21 +121,21 @@ "name": "stdout", "output_type": "stream", "text": [ - "3.54 is large\n", - "2.07 is small\n", - "9.22 is large\n", - "1.86 is small\n", - "1.71 is small\n" + "20 is not old\n", + "43 is not old\n", + "12 is not old\n", + "88 is old\n", + "67 is old\n" ] } ], "source": [ - "masses = [3.54, 2.07, 9.22, 1.86, 1.71]\n", - "for mass in masses:\n", - " if mass > 3.0:\n", - " print(mass, 'is large')\n", + "ages = [20, 43, 12, 88, 67]\n", + "for age in ages:\n", + " if age > 60:\n", + " print(age, 'is old')\n", " else:\n", - " print(mass, 'is small')" + " print(age, 'is not old')" ] }, { @@ -152,7 +152,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -161,23 +161,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "3.54 is large\n", - "2.07 is small\n", - "9.22 is HUGE\n", - "1.86 is small\n", - "1.71 is small\n" + "20 is young\n", + "43 is middle-aged\n", + "12 is young\n", + "88 is old\n", + "67 is old\n" ] } ], "source": [ - "masses = [3.54, 2.07, 9.22, 1.86, 1.71]\n", - "for mass in masses:\n", - " if mass > 9.0:\n", - " print(mass, 'is HUGE')\n", - " elif mass > 3.0:\n", - " print(mass, 'is large')\n", + "ages = [20, 43, 12, 88, 67]\n", + "for age in ages:\n", + " if age > 60:\n", + " print(age, 'is old')\n", + " elif age > 40:\n", + " print(age, 'is middle-aged')\n", " else:\n", - " print(mass, 'is small')" + " print(age, 'is young')" ] }, { @@ -192,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -201,21 +201,21 @@ "name": "stdout", "output_type": "stream", "text": [ - "3.54 is normal\n", - "2.07 is normal\n", - "9.22 is extreme\n", - "1.86 is extreme\n", - "1.71 is extreme\n" + "20 is in the labor force\n", + "43 is in the labor force\n", + "12 is outside the labor force\n", + "88 is outside the labor force\n", + "67 is outside the labor force\n" ] } ], "source": [ - "masses = [3.54, 2.07, 9.22, 1.86, 1.71]\n", - "for mass in masses:\n", - " if mass > 9.0 or mass < 2.0:\n", - " print(mass, 'is extreme')\n", + "ages = [20, 43, 12, 88, 67]\n", + "for age in ages:\n", + " if age > 65 or age < 16:\n", + " print(age, 'is outside the labor force')\n", " else:\n", - " print(mass, 'is normal')" + " print(age, 'is in the labor force')" ] }, { @@ -230,7 +230,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -257,12 +257,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* Does *not* automatically go back and re-evaluate if values change." + "* Conditionals do *not* automatically go back and re-evaluate if values change." ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -271,17 +271,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "adjusting velocity\n" + "small city\n" ] } ], "source": [ - "velocity = 10.0\n", - "if velocity > 20.0:\n", - " print('moving too fast')\n", + "population = 10000\n", + "if population > 200000:\n", + " print('large city')\n", "else:\n", - " print('adjusting velocity')\n", - " velocity = 50.0" + " print('small city')\n", + " population = 500000" ] }, { @@ -293,7 +293,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -302,31 +302,32 @@ "name": "stdout", "output_type": "stream", "text": [ - "0 : 10.0\n", - "moving too slow\n", - "1 : 20.0\n", - "moving too slow\n", - "2 : 30.0\n", - "moving too fast\n", - "3 : 25.0\n", - "moving too fast\n", - "4 : 20.0\n", - "moving too slow\n", - "final velocity: 30.0\n" + "0 : 10000\n", + "small city\n", + "1 : 85000\n", + "small city\n", + "2 : 160000\n", + "small city\n", + "3 : 235000\n", + "large city\n", + "4 : 230000\n", + "large city\n", + "final population: 225000\n" ] } ], "source": [ - "velocity = 10.0\n", + "population = 10000\n", "for i in range(5): # execute the loop 5 times\n", - " print(i, ':', velocity)\n", - " if velocity > 20.0:\n", - " print('moving too fast')\n", - " velocity = velocity - 5.0\n", + " print(i, ':', population)\n", + " if population > 200000:\n", + " print('large city')\n", + " population -= 5000\n", " else:\n", - " print('moving too slow')\n", - " velocity = velocity + 10.0\n", - "print('final velocity:', velocity)" + " print('small city')\n", + " population += 75000\n", + "\n", + "print('final population:', population)" ] }, { @@ -342,7 +343,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -354,7 +355,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0moriginal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1.5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1.3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;32min\u001b[0m \u001b[0moriginal\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0moriginal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1.5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1.3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;32min\u001b[0m \u001b[0moriginal\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name '____' is not defined" ] } @@ -381,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 10, "metadata": { "collapsed": true }, diff --git a/Day_2/10_Functions.ipynb b/Day_2/10_Functions.ipynb index b110e65..4a29f20 100644 --- a/Day_2/10_Functions.ipynb +++ b/Day_2/10_Functions.ipynb @@ -8,23 +8,19 @@ "\n", "**Time**\n", "\n", - "- teaching: 10 min\n", - "- exercises: 15 min\n", + "- Teaching: 10 min\n", + "- Exercises: 15 min\n", "\n", "**Questions**:\n", "\n", - "- \"How can programs do different things for different data?\"\n", + "- \"How can I avoid rewriting code that I will use again?\"\n", "\n", "**Learning Objectives**:\n", "\n", - "- \"Correctly write programs that use if and else statements and simple Boolean expressions (without logical operators).\"\n", - "- \"Trace the execution of unnested conditionals and conditionals inside loops.\"\n", - "keypoints:\n", - "- \"Use `if` statements to control whether or not a block of code is executed.\"\n", - "- \"Conditionals are often used inside loops.\"\n", - "- \"Use `else` to execute a block of code when an `if` condition is *not* true.\"\n", - "- \"Use `elif` to specify additional tests.\"\n", - "- \"Conditions are tested once, in order.\"\n", + "- \"Understand what a function is and why it's helpful.\"\n", + "- \"Understand how to define a function and its arguments.\"\n", + "- \"Understand what `return` does.\"\n", + "- \"Write a basic function.\"\n", "* * * * *" ] }, @@ -47,7 +43,7 @@ "* Begin the definition of a new function with `def`.\n", "* Followed by the name of the function.\n", " * Must obey the same rules as variable names.\n", - "* Then *parameters* in parentheses.\n", + "* The *parameters* are defined in parentheses.\n", " * Empty parentheses if the function doesn't take any inputs.\n", " * We will discuss this in detail in a moment.\n", "* Then a colon.\n", @@ -56,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": { "collapsed": true }, @@ -70,7 +66,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Defining a function does not run it.\n", + "## Defining a function does not run it!!!\n", "\n", "* Defining a function does not run it.\n", " * Like assigning a value to a variable.\n", @@ -79,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -110,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -150,7 +146,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "metadata": { "collapsed": true }, @@ -164,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -184,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -205,13 +201,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "* Remember: [every function returns something]({{ site.github.url }}/04-built-in/).\n", + "* Remember: [every function returns something](https://github.com/dlab-berkeley/python-intensive/blob/master/Day_1/06_Built-ins.ipynb).\n", "* A function that doesn't explicitly `return` a value automatically returns `None`.\n" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -241,7 +237,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -250,7 +246,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "calling 22.5\n" + "calling 22.5\n" ] } ], @@ -272,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 9, "metadata": { "collapsed": true }, @@ -298,7 +294,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -335,7 +331,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 11, "metadata": { "collapsed": false }, @@ -374,7 +370,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": { "collapsed": true }, @@ -397,17 +393,17 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", + "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def get_country:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def get_country:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], diff --git a/Day_2/11_Scope.ipynb b/Day_2/11_Scope.ipynb index c87409a..08aac8d 100755 --- a/Day_2/11_Scope.ipynb +++ b/Day_2/11_Scope.ipynb @@ -41,9 +41,9 @@ }, "outputs": [], "source": [ - "def alt_reality(temp):\n", - " value = temp + 1\n", - " return(value)" + "def increment(num):\n", + " incremented_num = num + 1\n", + " return(incremented_num)" ] }, { @@ -65,7 +65,7 @@ } ], "source": [ - "alt_reality(3)" + "increment(3)" ] }, { @@ -83,17 +83,20 @@ }, "outputs": [ { - "ename": "SyntaxError", - "evalue": "Missing parentheses in call to 'print' (, line 2)", + "ename": "NameError", + "evalue": "name 'num' is not defined", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print temp\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'\n" + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# temp is no longer defined because simple_function returned (i.e., finished)!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'num' is not defined" ] } ], "source": [ "# temp is no longer defined because simple_function returned (i.e., finished)!\n", - "print temp" + "print(num)" ] }, { @@ -104,17 +107,20 @@ }, "outputs": [ { - "ename": "SyntaxError", - "evalue": "Missing parentheses in call to 'print' (, line 2)", + "ename": "NameError", + "evalue": "name 'incremented_num' is not defined", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print value\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'\n" + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# and neither is value!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mincremented_num\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'incremented_num' is not defined" ] } ], "source": [ "# and neither is value!\n", - "print value" + "print(incremented_num)" ] }, { @@ -128,7 +134,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -143,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -162,7 +168,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"pressure is\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpressure\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"temperature is\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtemperature\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"pressure is\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpressure\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"temperature is\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtemperature\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'temperature' is not defined" ] } @@ -197,7 +203,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -208,7 +214,7 @@ "[4, 2, 3, 5]" ] }, - "execution_count": 11, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -226,7 +232,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -264,20 +270,19 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def print_hello():\n", - " print(\"hello\")\n", - " " + " print(\"hello\")" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -305,7 +310,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 11, "metadata": { "collapsed": false }, @@ -338,7 +343,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -371,7 +376,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 13, "metadata": { "collapsed": false }, @@ -406,7 +411,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 14, "metadata": { "collapsed": false }, diff --git a/Day_3/12_Dictionaries.ipynb b/Day_3/12_Dictionaries.ipynb index a12a357..7bdfe19 100644 --- a/Day_3/12_Dictionaries.ipynb +++ b/Day_3/12_Dictionaries.ipynb @@ -7,14 +7,17 @@ "# Dictionaries\n", "\n", "**Time**\n", - "- teaching: FIXME\n", - "- exercises: FIXME\n", + "- Teaching: 10 min\n", + "- Exercises: 10 min\n", "\n", "**Questions**:\n", - "- FIXME\n", + "- \"How can I store more complicated data?\"\n", + "- \"How can I retrieve complicated data efficiently?\"\n", "\n", "**Learning Objectives**:\n", - "- FIXME\n", + "- \"Understand the fundamentals of a dictionary.\"\n", + "- \"Understand advantages and disadvantages of a dictionary compared to a list.\"\n", + "- \"Iterate through dictionaries.\"\n", "* * * * *" ] }, @@ -30,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -47,13 +50,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "- The keys have to be **unique** and **immutible**. The usual suspects are strings and integers.\n", + "- The keys have to be **unique** and **immutable**. The usual suspects are strings and integers.\n", "- The values can be anything, including lists, and even other dictionaries" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -76,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -85,7 +88,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'year of death': 1967, 'place of birth': 'Iran', 'language': 'Persian', 'works': ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough'], 'name': 'Forough Farrokhzad', 'year of birth': 1935}\n" + "{'year of birth': 1935, 'place of birth': 'Iran', 'works': ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough'], 'name': 'Forough Farrokhzad', 'language': 'Persian', 'year of death': 1967}\n" ] } ], @@ -104,7 +107,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -113,8 +116,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "dict_keys(['year of death', 'place of birth', 'language', 'works', 'name', 'year of birth'])\n", - "dict_values([1967, 'Iran', 'Persian', ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough'], 'Forough Farrokhzad', 1935])\n" + "dict_keys(['year of birth', 'place of birth', 'works', 'name', 'language', 'year of death'])\n", + "dict_values([1935, 'Iran', ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough'], 'Forough Farrokhzad', 'Persian', 1967])\n" ] } ], @@ -132,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -143,7 +146,7 @@ "'Forough Farrokhzad'" ] }, - "execution_count": 15, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -161,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -172,7 +175,7 @@ "'Portrait of Forough'" ] }, - "execution_count": 16, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -192,7 +195,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -201,7 +204,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'year of death': 1967, 'place of birth': 'Iran', 'language': 'Farsi', 'works': ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough'], 'name': 'Forough Farrokhzad', 'year of birth': 1935}\n" + "{'year of birth': 1935, 'place of birth': 'Iran', 'works': ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough'], 'name': 'Forough Farrokhzad', 'language': 'Farsi', 'year of death': 1967}\n" ] } ], @@ -221,7 +224,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -230,7 +233,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'year of death': 1967, 'place of birth': 'Iran', 'language': 'Farsi', 'works': ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough'], 'name': 'Forough Farrokhzad', 'gender': 'Female', 'year of birth': 1935}\n" + "{'year of birth': 1935, 'place of birth': 'Iran', 'works': ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough'], 'gender': 'Female', 'name': 'Forough Farrokhzad', 'language': 'Farsi', 'year of death': 1967}\n" ] } ], @@ -251,7 +254,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -259,10 +262,10 @@ { "data": { "text/plain": [ - "dict_items([('year of death', 1967), ('place of birth', 'Iran'), ('language', 'Farsi'), ('works', ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough']), ('name', 'Forough Farrokhzad'), ('gender', 'Female'), ('year of birth', 1935)])" + "dict_items([('year of birth', 1935), ('place of birth', 'Iran'), ('works', ['Remembrance of a Day', 'Unison', 'The Shower of Your Hair', 'Portrait of Forough']), ('gender', 'Female'), ('name', 'Forough Farrokhzad'), ('language', 'Farsi'), ('year of death', 1967)])" ] }, - "execution_count": 22, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -283,7 +286,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -292,10 +295,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "bananas 0.32\n", - "apples 0.49\n", "pears 1.49\n", - "oranges 0.99\n" + "oranges 0.99\n", + "bananas 0.32\n", + "apples 0.49\n" ] } ], @@ -315,7 +318,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 11, "metadata": { "collapsed": false }, @@ -324,7 +327,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'bananas': 0.34, 'apples': 0.51, 'pears': 1.56, 'oranges': 1.04}\n" + "{'pears': 1.56, 'oranges': 1.04, 'bananas': 0.34, 'apples': 0.51}\n" ] } ], @@ -338,6 +341,22 @@ "print(d)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dictionaries vs. Lists" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In general, if you need data to be ordered or you have only simple data not needing to be subsetted, use a list.\n", + "\n", + "If the data is complex and hierarchical, the dictionary's `key` / `value` strcuture is very helpful. If you are only concerned about membership in a collection, dictionaries will always be much faster to reference, as the computer doesn't have to keep track of order. And of course, you can put a list (or even another dictionary!) inside a dictionary as the `value`." + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -382,20 +401,11 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "False\n" - ] - } - ], + "outputs": [], "source": [ "l = [\"Afghanistan\", \"Canada\", \"Sierra Leone\", \"Denmark\", \"Japan\"]\n", "d = {'apples': 0.49, 'oranges': 0.99, 'pears': 1.49, 'bananas': 0.32}\n", @@ -417,7 +427,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "metadata": { "collapsed": true }, diff --git a/Day_3/13_Files.ipynb b/Day_3/13_Files.ipynb index 3369030..7bf0b31 100755 --- a/Day_3/13_Files.ipynb +++ b/Day_3/13_Files.ipynb @@ -7,14 +7,16 @@ "# Files \n", "\n", "**Time**\n", - "- teaching: FIXME\n", - "- exercises: FIXME\n", + "- Teaching: 5 min\n", + "- Exercises: 5 min\n", "\n", "**Questions**:\n", - "- FIXME\n", + "- \"How do a open a file and read its contents?\"\n", + "- \"How do I write a file with the variables I generated?\"\n", "\n", "**Learning Objectives**:\n", - "- FIXME\n", + "- \"Learn the Pythonic way of reading in files.\"\n", + "- \"Understand how to read/write text files and csv files.\"\n", "* * * * *\n", "\n", "In this lesson we will cover how to read and write files." @@ -35,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -70,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -96,6 +98,13 @@ "print(text)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "`with` will keep the file open as long as the program is still in the indented block, once outside, the file is no longer open, and you can't access the contents, only what you have saved to a variable." + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -108,7 +117,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -122,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -137,7 +146,7 @@ " 'This is line 5.\\n']" ] }, - "execution_count": 6, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -155,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "metadata": { "collapsed": true }, @@ -169,7 +178,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -184,7 +193,7 @@ " 'This is line 5.']" ] }, - "execution_count": 8, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -204,7 +213,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": { "collapsed": true }, @@ -220,14 +229,13 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# but this is better...\n", - "new_file = open(\"example2.txt\", \"w\")\n", "bees = ['bears', 'beets', 'Battlestar Galactica']\n", "with open('example2.txt', 'w') as new_file:\n", " for i in bees:\n", @@ -236,7 +244,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -268,7 +276,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 10, "metadata": { "collapsed": true }, @@ -279,7 +287,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 11, "metadata": { "collapsed": false }, @@ -295,7 +303,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -325,7 +333,7 @@ " 'Longitude': \"01¡32'E\"}]" ] }, - "execution_count": 18, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -343,7 +351,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 13, "metadata": { "collapsed": false }, @@ -351,10 +359,10 @@ { "data": { "text/plain": [ - "dict_keys(['Longitude', 'Capital', 'Country', 'Latitude'])" + "dict_keys(['Capital', 'Longitude', 'Country', 'Latitude'])" ] }, - "execution_count": 19, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -367,7 +375,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 14, "metadata": { "collapsed": false }, diff --git a/Day_3/14_Libraries.ipynb b/Day_3/14_Libraries.ipynb index 9cc686d..a5f529c 100644 --- a/Day_3/14_Libraries.ipynb +++ b/Day_3/14_Libraries.ipynb @@ -7,12 +7,13 @@ "# Libraries\n", "\n", "**Time**\n", - "- teaching: 5\n", - "- exercises: 5\n", + "- Teaching: 5 min\n", + "- Exercises: 5 min\n", "\n", "**Questions**:\n", "- \"How can I use software that other people have written?\"\n", "- \"How can I find out what that software does?\"\n", + "\n", "**Learning Objectives**:\n", "- \"Explain what software libraries are and why programmers create and use them.\"\n", "- \"Write programs that import and use libraries from Python's standard library.\"\n", @@ -32,8 +33,8 @@ "* A *library* is a collection of functions that can be used by other programs.\n", " * May also contain data values (e.g., numerical constants).\n", " * Library's contents are supposed to be related, but there's no way to enforce that.\n", - "* Python's [standard library][stdlib] is installed with it.\n", - "* Many additional libraries are available from [PyPI][pypi] (the Python Package Index).\n", + "* Python's [standard library](https://docs.python.org/3/library/) is installed with it.\n", + "* Many additional libraries are available from [PyPI](https://pypi.python.org/pypi) (the Python Package Index).\n", "* We will see later how to write new libraries.\n", "\n", "## A program must import a library in order to use it.\n", @@ -80,11 +81,267 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { - "collapsed": true + "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on module math:\n", + "\n", + "NAME\n", + " math\n", + "\n", + "MODULE REFERENCE\n", + " http://docs.python.org/3.4/library/math\n", + " \n", + " The following documentation is automatically generated from the Python\n", + " source files. It may be incomplete, incorrect or include features that\n", + " are considered implementation detail and may vary between Python\n", + " implementations. When in doubt, consult the module reference at the\n", + " location listed above.\n", + "\n", + "DESCRIPTION\n", + " This module is always available. It provides access to the\n", + " mathematical functions defined by the C standard.\n", + "\n", + "FUNCTIONS\n", + " acos(...)\n", + " acos(x)\n", + " \n", + " Return the arc cosine (measured in radians) of x.\n", + " \n", + " acosh(...)\n", + " acosh(x)\n", + " \n", + " Return the inverse hyperbolic cosine of x.\n", + " \n", + " asin(...)\n", + " asin(x)\n", + " \n", + " Return the arc sine (measured in radians) of x.\n", + " \n", + " asinh(...)\n", + " asinh(x)\n", + " \n", + " Return the inverse hyperbolic sine of x.\n", + " \n", + " atan(...)\n", + " atan(x)\n", + " \n", + " Return the arc tangent (measured in radians) of x.\n", + " \n", + " atan2(...)\n", + " atan2(y, x)\n", + " \n", + " Return the arc tangent (measured in radians) of y/x.\n", + " Unlike atan(y/x), the signs of both x and y are considered.\n", + " \n", + " atanh(...)\n", + " atanh(x)\n", + " \n", + " Return the inverse hyperbolic tangent of x.\n", + " \n", + " ceil(...)\n", + " ceil(x)\n", + " \n", + " Return the ceiling of x as an int.\n", + " This is the smallest integral value >= x.\n", + " \n", + " copysign(...)\n", + " copysign(x, y)\n", + " \n", + " Return a float with the magnitude (absolute value) of x but the sign \n", + " of y. On platforms that support signed zeros, copysign(1.0, -0.0) \n", + " returns -1.0.\n", + " \n", + " cos(...)\n", + " cos(x)\n", + " \n", + " Return the cosine of x (measured in radians).\n", + " \n", + " cosh(...)\n", + " cosh(x)\n", + " \n", + " Return the hyperbolic cosine of x.\n", + " \n", + " degrees(...)\n", + " degrees(x)\n", + " \n", + " Convert angle x from radians to degrees.\n", + " \n", + " erf(...)\n", + " erf(x)\n", + " \n", + " Error function at x.\n", + " \n", + " erfc(...)\n", + " erfc(x)\n", + " \n", + " Complementary error function at x.\n", + " \n", + " exp(...)\n", + " exp(x)\n", + " \n", + " Return e raised to the power of x.\n", + " \n", + " expm1(...)\n", + " expm1(x)\n", + " \n", + " Return exp(x)-1.\n", + " This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.\n", + " \n", + " fabs(...)\n", + " fabs(x)\n", + " \n", + " Return the absolute value of the float x.\n", + " \n", + " factorial(...)\n", + " factorial(x) -> Integral\n", + " \n", + " Find x!. Raise a ValueError if x is negative or non-integral.\n", + " \n", + " floor(...)\n", + " floor(x)\n", + " \n", + " Return the floor of x as an int.\n", + " This is the largest integral value <= x.\n", + " \n", + " fmod(...)\n", + " fmod(x, y)\n", + " \n", + " Return fmod(x, y), according to platform C. x % y may differ.\n", + " \n", + " frexp(...)\n", + " frexp(x)\n", + " \n", + " Return the mantissa and exponent of x, as pair (m, e).\n", + " m is a float and e is an int, such that x = m * 2.**e.\n", + " If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.\n", + " \n", + " fsum(...)\n", + " fsum(iterable)\n", + " \n", + " Return an accurate floating point sum of values in the iterable.\n", + " Assumes IEEE-754 floating point arithmetic.\n", + " \n", + " gamma(...)\n", + " gamma(x)\n", + " \n", + " Gamma function at x.\n", + " \n", + " hypot(...)\n", + " hypot(x, y)\n", + " \n", + " Return the Euclidean distance, sqrt(x*x + y*y).\n", + " \n", + " isfinite(...)\n", + " isfinite(x) -> bool\n", + " \n", + " Return True if x is neither an infinity nor a NaN, and False otherwise.\n", + " \n", + " isinf(...)\n", + " isinf(x) -> bool\n", + " \n", + " Return True if x is a positive or negative infinity, and False otherwise.\n", + " \n", + " isnan(...)\n", + " isnan(x) -> bool\n", + " \n", + " Return True if x is a NaN (not a number), and False otherwise.\n", + " \n", + " ldexp(...)\n", + " ldexp(x, i)\n", + " \n", + " Return x * (2**i).\n", + " \n", + " lgamma(...)\n", + " lgamma(x)\n", + " \n", + " Natural logarithm of absolute value of Gamma function at x.\n", + " \n", + " log(...)\n", + " log(x[, base])\n", + " \n", + " Return the logarithm of x to the given base.\n", + " If the base not specified, returns the natural logarithm (base e) of x.\n", + " \n", + " log10(...)\n", + " log10(x)\n", + " \n", + " Return the base 10 logarithm of x.\n", + " \n", + " log1p(...)\n", + " log1p(x)\n", + " \n", + " Return the natural logarithm of 1+x (base e).\n", + " The result is computed in a way which is accurate for x near zero.\n", + " \n", + " log2(...)\n", + " log2(x)\n", + " \n", + " Return the base 2 logarithm of x.\n", + " \n", + " modf(...)\n", + " modf(x)\n", + " \n", + " Return the fractional and integer parts of x. Both results carry the sign\n", + " of x and are floats.\n", + " \n", + " pow(...)\n", + " pow(x, y)\n", + " \n", + " Return x**y (x to the power of y).\n", + " \n", + " radians(...)\n", + " radians(x)\n", + " \n", + " Convert angle x from degrees to radians.\n", + " \n", + " sin(...)\n", + " sin(x)\n", + " \n", + " Return the sine of x (measured in radians).\n", + " \n", + " sinh(...)\n", + " sinh(x)\n", + " \n", + " Return the hyperbolic sine of x.\n", + " \n", + " sqrt(...)\n", + " sqrt(x)\n", + " \n", + " Return the square root of x.\n", + " \n", + " tan(...)\n", + " tan(x)\n", + " \n", + " Return the tangent of x (measured in radians).\n", + " \n", + " tanh(...)\n", + " tanh(x)\n", + " \n", + " Return the hyperbolic tangent of x.\n", + " \n", + " trunc(...)\n", + " trunc(x:Real) -> Integral\n", + " \n", + " Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.\n", + "\n", + "DATA\n", + " e = 2.718281828459045\n", + " pi = 3.141592653589793\n", + "\n", + "FILE\n", + " /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload/math.so\n", + "\n", + "\n" + ] + } + ], "source": [ "help(math)" ] @@ -96,7 +353,7 @@ "## Import specific items from a library to shorten programs.\n", "\n", "* Use `from...import...` to load only specific items from a library.\n", - "* Then refer to them directly without library name as prefix.\n" + "* Then refer to them directly without the library name as prefix.\n" ] }, { @@ -132,7 +389,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -172,7 +429,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": { "collapsed": true }, @@ -232,7 +489,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -244,7 +501,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mmath\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mangle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdegrees\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m____\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpi\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m____\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mmath\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mangle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m____\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdegrees\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m____\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpi\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m____\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name '____' is not defined" ] } @@ -268,17 +525,17 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", + "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m ____ math import ____, ____\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m ____ math import ____, ____\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], @@ -287,6 +544,15 @@ "angle = degrees(pi / 2)\n", "print(angle)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/Day_3/15_Errors.ipynb b/Day_3/15_Errors.ipynb index c72f08b..96a8d8c 100644 --- a/Day_3/15_Errors.ipynb +++ b/Day_3/15_Errors.ipynb @@ -7,11 +7,14 @@ "# Errors and Exceptions\n", "\n", "**Time**\n", - "- teaching: FIXME\n", - "- exercises: FIXME\n", + "- teaching: 10 min\n", + "- exercises: 10 min\n", "\n", "**Questions**:\n", - "- FIXME\n", + "- \"How do a read an error message?\"\n", + "- \"What do the error messages mean?\"\n", + "- \"How do a fix an error?\"\n", + "- \"What if I still can't figure it out?\"\n", "\n", "**Learning Objectives**:\n", "\n", @@ -36,14 +39,14 @@ "* Once you know *why* you get certain types of errors,\n", "they become much easier to fix.\n", "\n", - "## Errors in Python come in specific form, called a [traceback](A1_Glossary.md#traceback).\n", + "## Errors in Python come in specific form, called a [traceback](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#traceback).\n", "\n", "Let's examine one:" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 1, "metadata": { "attributes": { "classes": [ @@ -61,8 +64,8 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0merrors_01\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0merrors_01\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfavorite_ice_cream\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/Users/rterman/Dropbox/berkeley/Git-Repos/Python-DLAB/Day_2/errors_01.py\u001b[0m in \u001b[0;36mfavorite_ice_cream\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\"strawberry\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m ]\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mice_creams\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[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0merrors_01\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0merrors_01\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfavorite_ice_cream\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/Users/chench/Documents/Git Repositories/python-intensive/Day_3/errors_01.py\u001b[0m in \u001b[0;36mfavorite_ice_cream\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\"strawberry\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m ]\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mice_creams\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[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } @@ -83,7 +86,7 @@ "\n", "So, in this case, the program:\n", "\n", - "1. first performed a [function call](A1_Glossary.md#function-call) to the function `favorite_ice_cream`.\n", + "1. first performed a [function call](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#function-call) to the function `favorite_ice_cream`.\n", "\n", "2. Inside this function, the program encountered an error on Line 7, when it tried to run the code `print ice_creams[3]`.\n", "\n", @@ -118,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 2, "metadata": { "attributes": { "classes": [ @@ -131,10 +134,10 @@ "outputs": [ { "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", + "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def some_function()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def some_function()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], @@ -161,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 3, "metadata": { "attributes": { "classes": [ @@ -174,10 +177,10 @@ "outputs": [ { "ename": "IndentationError", - "evalue": "unexpected indent (, line 4)", + "evalue": "unexpected indent (, line 4)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m return msg\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m return msg\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" ] } ], @@ -201,7 +204,7 @@ "> A quick note on indentation errors:\n", "> they can sometimes be insidious,\n", "> especially if you are mixing spaces and tabs.\n", - "> Because they are both [whitespace](reference.html#whitespace),\n", + "> Because they are both [whitespace](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#whitespace),\n", "> it is difficult to visually tell the difference.\n", "> The IPython notebook actually gives us a bit of a hint,\n", "> but not all Python editors will do that.\n", @@ -218,11 +221,11 @@ }, "outputs": [ { - "ename": "IndentationError", - "evalue": "unexpected indent (, line 4)", + "ename": "TabError", + "evalue": "inconsistent use of tabs and spaces in indentation (, line 4)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m return msg\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m return msg\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mTabError\u001b[0m\u001b[0;31m:\u001b[0m inconsistent use of tabs and spaces in indentation\n" ] } ], @@ -244,7 +247,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 5, "metadata": { "attributes": { "classes": [ @@ -262,7 +265,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'a' is not defined" ] } @@ -277,12 +280,12 @@ "source": [ "Why did you get this error?\n", "\n", - "- you might have meant to use a [string](A1_Glossary.md#string), but forgot to put quotes around it:" + "- you might have meant to use a [string](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string), but forgot to put quotes around it:" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 6, "metadata": { "attributes": { "classes": [ @@ -294,10 +297,14 @@ }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" + "ename": "NameError", + "evalue": "name 'hello' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhello\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'hello' is not defined" ] } ], @@ -316,7 +323,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 7, "metadata": { "attributes": { "classes": [ @@ -334,7 +341,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcount\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcount\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"The count is: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcount\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcount\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"The count is: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'count' is not defined" ] } @@ -354,7 +361,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 8, "metadata": { "attributes": { "classes": [ @@ -372,7 +379,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mCount\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mcount\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcount\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"The count is: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mCount\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mcount\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcount\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"The count is: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'count' is not defined" ] } @@ -397,7 +404,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 9, "metadata": { "attributes": { "classes": [ @@ -424,7 +431,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Letter #2 is \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mletters\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[0;34m\"Letter #3 is \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mletters\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[0;34m\"Letter #4 is \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mletters\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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Letter #2 is \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mletters\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[0;34m\"Letter #3 is \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mletters\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[0;34m\"Letter #4 is \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mletters\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: list index out of range" ] } @@ -448,7 +455,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -460,7 +467,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0ma_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0ma_dictionary\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0ma_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'name'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0ma_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0ma_dictionary\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0ma_list\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'name'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: list indices must be integers, not str" ] } @@ -486,7 +493,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 11, "metadata": { "attributes": { "classes": [ @@ -504,7 +511,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfile_handle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'myfile.txt'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfile_handle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'myfile.txt'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'myfile.txt'" ] } @@ -523,7 +530,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 12, "metadata": { "attributes": { "classes": [ @@ -541,7 +548,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mUnsupportedOperation\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mfile_handle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'myfile.txt'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfile_handle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mfile_handle\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'myfile.txt'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'w'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfile_handle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mUnsupportedOperation\u001b[0m: not readable" ] } @@ -569,7 +576,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 13, "metadata": { "collapsed": false }, @@ -581,9 +588,9 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0merrors_02\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0merrors_02\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprint_friday_message\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/Users/rterman/Dropbox/berkeley/Git-Repos/Python-DLAB/Day_2/errors_02.py\u001b[0m in \u001b[0;36mprint_friday_message\u001b[0;34m()\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mprint_friday_message\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---> 15\u001b[0;31m \u001b[0mprint_message\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Friday\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m/Users/rterman/Dropbox/berkeley/Git-Repos/Python-DLAB/Day_2/errors_02.py\u001b[0m in \u001b[0;36mprint_message\u001b[0;34m(day)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\"sunday\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m\"Aw, the weekend is almost over.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m }\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessages\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mday\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0merrors_02\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0merrors_02\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprint_friday_message\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/Users/chench/Documents/Git Repositories/python-intensive/Day_3/errors_02.py\u001b[0m in \u001b[0;36mprint_friday_message\u001b[0;34m()\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mprint_friday_message\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---> 15\u001b[0;31m \u001b[0mprint_message\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Friday\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/Users/chench/Documents/Git Repositories/python-intensive/Day_3/errors_02.py\u001b[0m in \u001b[0;36mprint_message\u001b[0;34m(day)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\"sunday\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m\"Aw, the weekend is almost over.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m }\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessages\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mday\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mKeyError\u001b[0m: 'Friday'" ] } @@ -607,17 +614,17 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", - "evalue": "invalid syntax (, line 1)", + "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def another_function\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def another_function\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], @@ -642,7 +649,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 15, "metadata": { "collapsed": false }, @@ -654,7 +661,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# use a if the number is a multiple of 3, otherwise use b\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mNumber\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mmessage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmessage\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnumber\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# use a if the number is a multiple of 3, otherwise use b\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mNumber\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mmessage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmessage\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'Number' is not defined" ] } @@ -682,7 +689,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 16, "metadata": { "collapsed": false }, @@ -694,7 +701,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mseasons\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'Spring'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Summer'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Fall'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Winter'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'My favorite season is '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseasons\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mseasons\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'Spring'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Summer'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Fall'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Winter'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'My favorite season is '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mseasons\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m4\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: list index out of range" ] } @@ -726,9 +733,13 @@ "\n", "### Change One Thing at a Time, For a Reason\n", "\n", - "Replacing random chunks of code is unlikely to do much good. (After all, if you got it wrong the first time, you'll probably get it wrong the second and third as well.) Good programmers therefore *change one thing at a time, for a reason* They are either trying to gather more information (\"is the bug still there if we change the order of the loops?\") or test a fix (\"can we make the bug go away by sorting our data before processing it?\").\n", + "Replacing random chunks of code is unlikely to do much good. (After all, if you got it wrong the first time, you'll probably get it wrong the second and third as well.) Good programmers therefore *change one thing at a time, for a reason*. They are either trying to gather more information (\"is the bug still there if we change the order of the loops?\") or test a fix (\"can we make the bug go away by sorting our data before processing it?\").\n", + "\n", + "Every time we make a change, however small, we should re-run our tests immediately, because the more things we change at once, the harder it is to know what's responsible for what.\n", + "\n", + "### Outside Resources\n", "\n", - "Every time we make a change, however small, we should re-run our tests immediately, because the more things we change at once, the harder it is to know what's responsible for what" + "If you've tried everything you can think of to logically fix the error and still don't understand what Python is trying to tell you, now the real searching begins. Go to Google and copy/paste the error, you're probably not the only one who has run into it!" ] }, { diff --git a/Day_3/16_Comprehensions.ipynb b/Day_3/16_Comprehensions.ipynb index 357f74e..b1829a4 100755 --- a/Day_3/16_Comprehensions.ipynb +++ b/Day_3/16_Comprehensions.ipynb @@ -11,10 +11,11 @@ "- exercises: 5 min\n", "\n", "**Questions**:\n", - "- FIXME\n", + "- \"Is there a faster way to generate a new list by changing all the elements in an old one?\"\n", + "- \"How can I write cleaner, more compact code?\"\n", "\n", "**Learning Objectives**:\n", - "- FIXME\n", + "- \"Understand and write a `list comprehension`.\"\n", "* * * * *\n", "\n", "****" @@ -31,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -63,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -94,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -120,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -140,6 +141,13 @@ "print(nums2)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There are several advantages to list comprehensions, most obvious being cleaner, more readable code. List comprehensions also save variable name space if you are looking to modify elements in a list. Less obvious, is that list comprehensions are actually calculated faster than `for` loops!" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -151,7 +159,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -167,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 6, "metadata": { "collapsed": true }, @@ -178,7 +186,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 7, "metadata": { "collapsed": true }, @@ -192,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -204,7 +212,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -221,7 +229,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "collapsed": true }, diff --git a/Day_3/17_Beautiful-Code.ipynb b/Day_3/17_Beautiful-Code.ipynb index fc4625e..f4fb791 100644 --- a/Day_3/17_Beautiful-Code.ipynb +++ b/Day_3/17_Beautiful-Code.ipynb @@ -7,23 +7,26 @@ "# Programming Style\n", "\n", "**Time**\n", - "- teaching: 10\n", - "- exercises: FIXME\n", + "- teaching: 10 min\n", + "- exercises: 5 min (exploratory)\n", "\n", "**Questions**:\n", "- \"How can I make my programs more readable?\"\n", "- \"How do most programmers format their code?\"\n", "\n", "**Learning Objectives**:\n", - "FIX ME\n", + "- \"Learn how to write code for others.\"\n", + "- \"Explore the PEP 8 style guide.\"\n", "\n", "**keypoints**\n", - "FIX ME\n", + "- \"Be clear with your code.\"\n", + "- \"Write comments, even if it seems obvious to you (it won't tomorrow!).\"\n", + "- \"Code with other users in mind.\"\n", "* * * * *\n", "\n", - "## Good Python Code is Readible\n", + "## Good Python Code is Readable\n", "\n", - "- If you ask Python programmers what they like most in Python, they will often say its high readability. I\n", + "- If you ask Python programmers what they like most in Python, they will often say its high readability.\n", "- Indeed, a high level of readability is at the heart of the design of the Python language, following the recognised fact that code is read much more often than it is written.\n", "- How do you help readers of your code? " ] @@ -182,11 +185,23 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 5, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "No module named 'some'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m )\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m from some.deep.module.inside.a.module import (\n\u001b[0m\u001b[1;32m 8\u001b[0m a_nice_function, another_nice_function, yet_another_nice_function)\n", + "\u001b[0;31mImportError\u001b[0m: No module named 'some'" + ] + } + ], "source": [ "my_very_big_string = (\n", " \"For a long time I used to go to bed early. Sometimes, \"\n", @@ -202,16 +217,28 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Or a backslah, although this is not as recommended:" + "Or a backslash, although this is not as recommended:" ] }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 6, "metadata": { - "collapsed": true + "collapsed": false }, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "No module named 'some'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmy_very_big_string\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"\"\"For a long time I used to go to bed early. Sometimes, when I had put out my candle, my eyes would close so quickly that I had not even time to say “I’m going to sleep.”\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0msome\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdeep\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minside\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodule\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0ma_nice_function\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0manother_nice_function\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0myet_another_nice_function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mImportError\u001b[0m: No module named 'some'" + ] + } + ], "source": [ "my_very_big_string = \"\"\"For a long time I used to go to bed early. Sometimes, \\\n", " when I had put out my candle, my eyes would close so quickly that I had not even \\\n", @@ -236,7 +263,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 7, "metadata": { "collapsed": true }, @@ -264,7 +291,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 8, "metadata": { "collapsed": true }, @@ -314,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -327,7 +354,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 10, "metadata": { "collapsed": false }, @@ -339,8 +366,8 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcalc_GDP\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m40000000\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mcalc_GDP\u001b[0;34m(income, population)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcalc_GDP\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mincome\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpopulation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mpopulation\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;34m,\u001b[0m \u001b[0;34m'Population cannot be 0 or negative'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mincome\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mpopulation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcalc_GDP\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m40000000\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mcalc_GDP\u001b[0;34m(income, population)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcalc_GDP\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mincome\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpopulation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mpopulation\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;34m,\u001b[0m \u001b[0;34m'Population cannot be 0 or negative'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mincome\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mpopulation\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: Population cannot be 0 or negative" ] } diff --git a/Day_3/capitals2.csv b/Day_3/capitals2.csv index f1eef4b..17e1f82 100644 --- a/Day_3/capitals2.csv +++ b/Day_3/capitals2.csv @@ -1,201 +1,201 @@ -Longitude,Capital,Country,Latitude -69¡11'E,Kabul,Afghanistan,34¡28'N -19¡49'E,Tirane,Albania,41¡18'N -03¡08'E,Algiers,Algeria,36¡42'N -170¡43'W,Pago Pago,American Samoa,14¡16'S -01¡32'E,Andorra la Vella,Andorra,42¡31'N -13¡15'E,Luanda,Angola,08¡50'S -61¡48'W,W. Indies,Antigua and Barbuda,17¡20'N -60¡00'W,Buenos Aires,Argentina,36¡30'S -44¡31'E,Yerevan,Armenia,40¡10'N -70¡02'W,Oranjestad,Aruba,12¡32'N -149¡08'E,Canberra,Australia,35¡15'S -16¡22'E,Vienna,Austria,48¡12'N -49¡56'E,Baku,Azerbaijan,40¡29'N -77¡20'W,Nassau,Bahamas,25¡05'N -50¡30'E,Manama,Bahrain,26¡10'N -90¡26'E,Dhaka,Bangladesh,23¡43'N -59¡30'W,Bridgetown,Barbados,13¡05'N -27¡30'E,Minsk,Belarus,53¡52'N -04¡21'E,Brussels,Belgium,50¡51'N -88¡30'W,Belmopan,Belize,17¡18'N -02¡42'E,Porto-Novo (constitutional cotonou) (seat of gvnt),Benin,06¡23'N -89¡45'E,Thimphu,Bhutan,27¡31'N -68¡10'W,La Paz (adm.)/sucre (legislative),Bolivia,16¡20'S -18¡26'E,Sarajevo,Bosnia and Herzegovina,43¡52'N -25¡57'E,Gaborone,Botswana,24¡45'S -47¡55'W,Brasilia,Brazil,15¡47'S -64¡37'W,Road Town,British Virgin Islands,18¡27'N -115¡00'E,Bandar Seri Begawan,Brunei Darussalam,04¡52'N -23¡20'E,Sofia,Bulgaria,42¡45'N -01¡30'W,Ouagadougou,Burkina Faso,12¡15'N -29¡18'E,Bujumbura,Burundi,03¡16'S -104¡55'E,Phnom Penh,Cambodia,11¡33'N -11¡35'E,Yaounde,Cameroon,03¡50'N -75¡42'W,Ottawa,Canada,45¡27'N -23¡34'W,Praia,Cape Verde,15¡02'N -81¡24'W,George Town,Cayman Islands,19¡20'N -18¡35'E,Bangui,Central African Republic,04¡23'N -14¡59'E,N'Djamena,Chad,12¡10'N -70¡40'W,Santiago,Chile,33¡24'S -116¡20'E,Beijing,China,39¡55'N -74¡00'W,Bogota,Colombia,04¡34'N -43¡16'E,Moroni,Comros,11¡40'S -15¡12'E,Brazzaville,Congo,04¡09'S -84¡02'W,San Jose,Costa Rica,09¡55'N -05¡17'W,Yamoussoukro,Cote d'Ivoire,06¡49'N -15¡58'E,Zagreb,Croatia,45¡50'N -82¡22'W,Havana,Cuba,23¡08'N -33¡25'E,Nicosia,Cyprus,35¡10'N -14¡22'E,Prague,Czech Republic,50¡05'N -125¡30'E,P'yongyang,Democratic People's Republic of,39¡09'N -15¡15'E,Kinshasa,Democratic Republic of the Congo,04¡20'S -12¡34'E,Copenhagen,Denmark,55¡41'N -42¡20'E,Djibouti,Djibouti,11¡08'N -61¡24'W,Roseau,Dominica,15¡20'N -69¡59'W,Santo Domingo,Dominica Republic,18¡30'N -125¡34'E,Dili,East Timor,08¡29'S -78¡35'W,Quito,Ecuador,00¡15'S -31¡14'E,Cairo,Egypt,30¡01'N -89¡10'W,San Salvador,El Salvador,13¡40'N -08¡50'E,Malabo,Equatorial Guinea,03¡45'N -38¡55'E,Asmara,Eritrea,15¡19'N -24¡48'E,Tallinn,Estonia,59¡22'N -38¡42'E,Addis Ababa,Ethiopia,09¡02'N -59¡51'W,Stanley,Falkland Islands (Malvinas),51¡40'S -06¡56'W,Torshavn,Faroe Islands,62¡05'N -178¡30'E,Suva,Fiji,18¡06'S -25¡03'E,Helsinki,Finland,60¡15'N -02¡20'E,Paris,France,48¡50'N -52¡18'W,Cayenne,French Guiana,05¡05'N -149¡34'W,Papeete,French Polynesia,17¡32'S -09¡26'E,Libreville,Gabon,00¡25'N -16¡40'W,Banjul,Gambia,13¡28'N -44¡50'E,T'bilisi,Georgia,41¡43'N -13¡25'E,Berlin,Germany,52¡30'N -00¡06'W,Accra,Ghana,05¡35'N -23¡46'E,Athens,Greece,37¡58'N -51¡35'W,Nuuk,Greenland,64¡10'N -61¡44'W,Basse-Terre,Guadeloupe,16¡00'N -90¡22'W,Guatemala,Guatemala,14¡40'N -02¡33'W,St. Peter Port,Guernsey,49¡26'N -13¡49'W,Conakry,Guinea,09¡29'N -15¡45'W,Bissau,Guinea-Bissau,11¡45'N -58¡12'W,Georgetown,Guyana,06¡50'N -72¡20'W,Port-au-Prince,Haiti,18¡40'N -74¡00'E,,Heard Island and McDonald Islands,53¡00'S -87¡14'W,Tegucigalpa,Honduras,14¡05'N -19¡05'E,Budapest,Hungary,47¡29'N -21¡57'W,Reykjavik,Iceland,64¡10'N -77¡13'E,New Delhi,India,28¡37'N -106¡49'E,Jakarta,Indonesia,06¡09'S -51¡30'E,Tehran,Iran (Islamic Republic of),35¡44'N -44¡30'E,Baghdad,Iraq,33¡20'N -06¡15'W,Dublin,Ireland,53¡21'N -35¡12'E,Jerusalem,Israel,31¡47'N -12¡29'E,Rome,Italy,41¡54'N -76¡50'W,Kingston,Jamaica,18¡00'N -35¡52'E,Amman,Jordan,31¡57'N -71¡30'E,Astana,Kazakhstan,51¡10'N -36¡48'E,Nairobi,Kenya,01¡17'S -173¡00'E,Tarawa,Kiribati,01¡30'N -48¡00'E,Kuwait,Kuwait,29¡30'N -74¡46'E,Bishkek,Kyrgyzstan,42¡54'N -102¡36'E,Vientiane,Lao People's Democratic Republic,17¡58'N -24¡08'E,Riga,Latvia,56¡53'N -35¡31'E,Beirut,Lebanon,33¡53'N -27¡30'E,Maseru,Lesotho,29¡18'S -10¡47'W,Monrovia,Liberia,06¡18'N -13¡07'E,Tripoli,Libyan Arab Jamahiriya,32¡49'N -09¡31'E,Vaduz,Liechtenstein,47¡08'N -25¡19'E,Vilnius,Lithuania,54¡38'N -06¡09'E,Luxembourg,Luxembourg,49¡37'N -113¡33'E,Macau,"Macao, China",22¡12'N -47¡31'E,Antananarivo,Madagascar,18¡55'S -33¡48'E,Lilongwe,Malawi,14¡00'S -101¡41'E,Kuala Lumpur,Malaysia,03¡09'N -73¡28'E,Male,Maldives,04¡00'N -07¡55'W,Bamako,Mali,12¡34'N -14¡31'E,Valletta,Malta,35¡54'N -61¡02'W,Fort-de-France,Martinique,14¡36'N -57¡30'E,Nouakchott,Mauritania,20¡10'S -45¡14'E,Mamoudzou,Mayotte,12¡48'S -99¡10'W,Mexico,Mexico,19¡20'N -158¡09'E,Palikir,Micronesia (Federated States of),06¡55'N -28¡50'E,Chisinau,"Moldova, Republic of",47¡02'N -32¡32'E,Maputo,Mozambique,25¡58'S -96¡20'E,Yangon,Myanmar,16¡45'N -17¡04'E,Windhoek,Namibia,22¡35'S -85¡20'E,Kathmandu,Nepal,27¡45'N -04¡54'E,Amsterdam/The Hague (seat of Gvnt),Netherlands,52¡23'N -69¡00'W,Willemstad,Netherlands Antilles,12¡05'N -166¡30'E,Noumea,New Caledonia,22¡17'S -174¡46'E,Wellington,New Zealand,41¡19'S -86¡20'W,Managua,Nicaragua,12¡06'N -02¡06'E,Niamey,Niger,13¡27'N -07¡32'E,Abuja,Nigeria,09¡05'N -168¡43'E,Kingston,Norfolk Island,45¡20'S -145¡45'E,Saipan,Northern Mariana Islands,15¡12'N -10¡45'E,Oslo,Norway,59¡55'N -58¡36'E,Masqat,Oman,23¡37'N -73¡10'E,Islamabad,Pakistan,33¡40'N -134¡28'E,Koror,Palau,07¡20'N -79¡25'W,Panama,Panama,09¡00'N -147¡08'E,Port Moresby,Papua New Guinea,09¡24'S -57¡30'W,Asuncion,Paraguay,25¡10'S -77¡00'W,Lima,Peru,12¡00'S -121¡03'E,Manila,Philippines,14¡40'N -21¡00'E,Warsaw,Poland,52¡13'N -09¡10'W,Lisbon,Portugal,38¡42'N -66¡07'W,San Juan,Puerto Rico,18¡28'N -51¡35'E,Doha,Qatar,25¡15'N -126¡58'E,Seoul,Republic of Korea,37¡31'N -26¡10'E,Bucuresti,Romania,44¡27'N -37¡35'E,Moskva,Russian Federation,55¡45'N -30¡04'E,Kigali,Rawanda,01¡59'S -62¡43'W,Basseterre,Saint Kitts and Nevis,17¡17'N -60¡58'W,Castries,Saint Lucia,14¡02'N -56¡12'W,Saint-Pierre,Saint Pierre and Miquelon,46¡46'N -61¡10'W,Kingstown,Saint vincent and the Grenadines,13¡10'N -171¡50'W,Apia,Samoa,13¡50'S -12¡30'E,San Marino,San Marino,43¡55'N -06¡39'E,Sao Tome,Sao Tome and Principe,00¡10'N -46¡42'E,Riyadh,Saudi Arabia,24¡41'N -17¡29'W,Dakar,Senegal,14¡34'N -13¡17'W,Freetown,Sierra Leone,08¡30'N -17¡07'E,Bratislava,Slovakia,48¡10'N -14¡33'E,Ljubljana,Slovenia,46¡04'N -159¡57'E,Honiara,Solomon Islands,09¡27'S -45¡25'E,Mogadishu,Somalia,02¡02'N -28¡12'E,Pretoria (adm.) / Cap Town (Legislative) / Bloemfontein (Judicial),South Africa,25¡44'S -03¡45'W,Madrid,Spain,40¡25'N -32¡35'E,Khartoum,Sudan,15¡31'N -55¡10'W,Paramaribo,Suriname,05¡50'N -31¡06'E,Mbabane (Adm.),Swaziland,26¡18'S -18¡03'E,Stockholm,Sweden,59¡20'N -07¡28'E,Bern,Switzerland,46¡57'N -36¡18'E,Damascus,Syrian Arab Republic,33¡30'N -68¡48'E,Dushanbe,Tajikistan,38¡33'N -100¡35'E,Bangkok,Thailand,13¡45'N -21¡26'E,Skopje,The Former Yugoslav Republic of Macedonia,42¡01'N -01¡20'E,Lome,Togo,06¡09'N -174¡00'W,Nuku'alofa,Tonga,21¡10'S -10¡11'E,Tunis,Tunisia,36¡50'N -32¡54'E,Ankara,Turkey,39¡57'N -57¡50'E,Ashgabat,Turkmenistan,38¡00'N -179¡13'E,Funafuti,Tuvalu,08¡31'S -32¡30'E,Kampala,Uganda,00¡20'N -30¡28'E,Kiev (Rus),Ukraine,50¡30'N -54¡22'E,Abu Dhabi,United Arab Emirates,24¡28'N -00¡05'W,London,United Kingdom of Great Britain and Northern Ireland,51¡36'N -35¡45'E,Dodoma,United Republic of Tanzania,06¡08'S -77¡02'W,Washington DC,United States of America,39¡91'N -64¡56'W,Charlotte Amalie,United States of Virgin Islands,18¡21'N -56¡11'W,Montevideo,Uruguay,34¡50'S -69¡10'E,Tashkent,Uzbekistan,41¡20'N -168¡18'E,Port-Vila,Vanuatu,17¡45'S -66¡55'W,Caracas,Venezuela,10¡30'N -105¡55'E,Hanoi,Viet Nam,21¡05'N -20¡37'E,Belgrade,Yugoslavia,44¡50'N -28¡16'E,Lusaka,Zambia,15¡28'S -31¡02'E,Harare,Zimbabwe,17¡43'S +Capital,Longitude,Country,Latitude +Kabul,69¡11'E,Afghanistan,34¡28'N +Tirane,19¡49'E,Albania,41¡18'N +Algiers,03¡08'E,Algeria,36¡42'N +Pago Pago,170¡43'W,American Samoa,14¡16'S +Andorra la Vella,01¡32'E,Andorra,42¡31'N +Luanda,13¡15'E,Angola,08¡50'S +W. Indies,61¡48'W,Antigua and Barbuda,17¡20'N +Buenos Aires,60¡00'W,Argentina,36¡30'S +Yerevan,44¡31'E,Armenia,40¡10'N +Oranjestad,70¡02'W,Aruba,12¡32'N +Canberra,149¡08'E,Australia,35¡15'S +Vienna,16¡22'E,Austria,48¡12'N +Baku,49¡56'E,Azerbaijan,40¡29'N +Nassau,77¡20'W,Bahamas,25¡05'N +Manama,50¡30'E,Bahrain,26¡10'N +Dhaka,90¡26'E,Bangladesh,23¡43'N +Bridgetown,59¡30'W,Barbados,13¡05'N +Minsk,27¡30'E,Belarus,53¡52'N +Brussels,04¡21'E,Belgium,50¡51'N +Belmopan,88¡30'W,Belize,17¡18'N +Porto-Novo (constitutional cotonou) (seat of gvnt),02¡42'E,Benin,06¡23'N +Thimphu,89¡45'E,Bhutan,27¡31'N +La Paz (adm.)/sucre (legislative),68¡10'W,Bolivia,16¡20'S +Sarajevo,18¡26'E,Bosnia and Herzegovina,43¡52'N +Gaborone,25¡57'E,Botswana,24¡45'S +Brasilia,47¡55'W,Brazil,15¡47'S +Road Town,64¡37'W,British Virgin Islands,18¡27'N +Bandar Seri Begawan,115¡00'E,Brunei Darussalam,04¡52'N +Sofia,23¡20'E,Bulgaria,42¡45'N +Ouagadougou,01¡30'W,Burkina Faso,12¡15'N +Bujumbura,29¡18'E,Burundi,03¡16'S +Phnom Penh,104¡55'E,Cambodia,11¡33'N +Yaounde,11¡35'E,Cameroon,03¡50'N +Ottawa,75¡42'W,Canada,45¡27'N +Praia,23¡34'W,Cape Verde,15¡02'N +George Town,81¡24'W,Cayman Islands,19¡20'N +Bangui,18¡35'E,Central African Republic,04¡23'N +N'Djamena,14¡59'E,Chad,12¡10'N +Santiago,70¡40'W,Chile,33¡24'S +Beijing,116¡20'E,China,39¡55'N +Bogota,74¡00'W,Colombia,04¡34'N +Moroni,43¡16'E,Comros,11¡40'S +Brazzaville,15¡12'E,Congo,04¡09'S +San Jose,84¡02'W,Costa Rica,09¡55'N +Yamoussoukro,05¡17'W,Cote d'Ivoire,06¡49'N +Zagreb,15¡58'E,Croatia,45¡50'N +Havana,82¡22'W,Cuba,23¡08'N +Nicosia,33¡25'E,Cyprus,35¡10'N +Prague,14¡22'E,Czech Republic,50¡05'N +P'yongyang,125¡30'E,Democratic People's Republic of,39¡09'N +Kinshasa,15¡15'E,Democratic Republic of the Congo,04¡20'S +Copenhagen,12¡34'E,Denmark,55¡41'N +Djibouti,42¡20'E,Djibouti,11¡08'N +Roseau,61¡24'W,Dominica,15¡20'N +Santo Domingo,69¡59'W,Dominica Republic,18¡30'N +Dili,125¡34'E,East Timor,08¡29'S +Quito,78¡35'W,Ecuador,00¡15'S +Cairo,31¡14'E,Egypt,30¡01'N +San Salvador,89¡10'W,El Salvador,13¡40'N +Malabo,08¡50'E,Equatorial Guinea,03¡45'N +Asmara,38¡55'E,Eritrea,15¡19'N +Tallinn,24¡48'E,Estonia,59¡22'N +Addis Ababa,38¡42'E,Ethiopia,09¡02'N +Stanley,59¡51'W,Falkland Islands (Malvinas),51¡40'S +Torshavn,06¡56'W,Faroe Islands,62¡05'N +Suva,178¡30'E,Fiji,18¡06'S +Helsinki,25¡03'E,Finland,60¡15'N +Paris,02¡20'E,France,48¡50'N +Cayenne,52¡18'W,French Guiana,05¡05'N +Papeete,149¡34'W,French Polynesia,17¡32'S +Libreville,09¡26'E,Gabon,00¡25'N +Banjul,16¡40'W,Gambia,13¡28'N +T'bilisi,44¡50'E,Georgia,41¡43'N +Berlin,13¡25'E,Germany,52¡30'N +Accra,00¡06'W,Ghana,05¡35'N +Athens,23¡46'E,Greece,37¡58'N +Nuuk,51¡35'W,Greenland,64¡10'N +Basse-Terre,61¡44'W,Guadeloupe,16¡00'N +Guatemala,90¡22'W,Guatemala,14¡40'N +St. Peter Port,02¡33'W,Guernsey,49¡26'N +Conakry,13¡49'W,Guinea,09¡29'N +Bissau,15¡45'W,Guinea-Bissau,11¡45'N +Georgetown,58¡12'W,Guyana,06¡50'N +Port-au-Prince,72¡20'W,Haiti,18¡40'N +,74¡00'E,Heard Island and McDonald Islands,53¡00'S +Tegucigalpa,87¡14'W,Honduras,14¡05'N +Budapest,19¡05'E,Hungary,47¡29'N +Reykjavik,21¡57'W,Iceland,64¡10'N +New Delhi,77¡13'E,India,28¡37'N +Jakarta,106¡49'E,Indonesia,06¡09'S +Tehran,51¡30'E,Iran (Islamic Republic of),35¡44'N +Baghdad,44¡30'E,Iraq,33¡20'N +Dublin,06¡15'W,Ireland,53¡21'N +Jerusalem,35¡12'E,Israel,31¡47'N +Rome,12¡29'E,Italy,41¡54'N +Kingston,76¡50'W,Jamaica,18¡00'N +Amman,35¡52'E,Jordan,31¡57'N +Astana,71¡30'E,Kazakhstan,51¡10'N +Nairobi,36¡48'E,Kenya,01¡17'S +Tarawa,173¡00'E,Kiribati,01¡30'N +Kuwait,48¡00'E,Kuwait,29¡30'N +Bishkek,74¡46'E,Kyrgyzstan,42¡54'N +Vientiane,102¡36'E,Lao People's Democratic Republic,17¡58'N +Riga,24¡08'E,Latvia,56¡53'N +Beirut,35¡31'E,Lebanon,33¡53'N +Maseru,27¡30'E,Lesotho,29¡18'S +Monrovia,10¡47'W,Liberia,06¡18'N +Tripoli,13¡07'E,Libyan Arab Jamahiriya,32¡49'N +Vaduz,09¡31'E,Liechtenstein,47¡08'N +Vilnius,25¡19'E,Lithuania,54¡38'N +Luxembourg,06¡09'E,Luxembourg,49¡37'N +Macau,113¡33'E,"Macao, China",22¡12'N +Antananarivo,47¡31'E,Madagascar,18¡55'S +Lilongwe,33¡48'E,Malawi,14¡00'S +Kuala Lumpur,101¡41'E,Malaysia,03¡09'N +Male,73¡28'E,Maldives,04¡00'N +Bamako,07¡55'W,Mali,12¡34'N +Valletta,14¡31'E,Malta,35¡54'N +Fort-de-France,61¡02'W,Martinique,14¡36'N +Nouakchott,57¡30'E,Mauritania,20¡10'S +Mamoudzou,45¡14'E,Mayotte,12¡48'S +Mexico,99¡10'W,Mexico,19¡20'N +Palikir,158¡09'E,Micronesia (Federated States of),06¡55'N +Chisinau,28¡50'E,"Moldova, Republic of",47¡02'N +Maputo,32¡32'E,Mozambique,25¡58'S +Yangon,96¡20'E,Myanmar,16¡45'N +Windhoek,17¡04'E,Namibia,22¡35'S +Kathmandu,85¡20'E,Nepal,27¡45'N +Amsterdam/The Hague (seat of Gvnt),04¡54'E,Netherlands,52¡23'N +Willemstad,69¡00'W,Netherlands Antilles,12¡05'N +Noumea,166¡30'E,New Caledonia,22¡17'S +Wellington,174¡46'E,New Zealand,41¡19'S +Managua,86¡20'W,Nicaragua,12¡06'N +Niamey,02¡06'E,Niger,13¡27'N +Abuja,07¡32'E,Nigeria,09¡05'N +Kingston,168¡43'E,Norfolk Island,45¡20'S +Saipan,145¡45'E,Northern Mariana Islands,15¡12'N +Oslo,10¡45'E,Norway,59¡55'N +Masqat,58¡36'E,Oman,23¡37'N +Islamabad,73¡10'E,Pakistan,33¡40'N +Koror,134¡28'E,Palau,07¡20'N +Panama,79¡25'W,Panama,09¡00'N +Port Moresby,147¡08'E,Papua New Guinea,09¡24'S +Asuncion,57¡30'W,Paraguay,25¡10'S +Lima,77¡00'W,Peru,12¡00'S +Manila,121¡03'E,Philippines,14¡40'N +Warsaw,21¡00'E,Poland,52¡13'N +Lisbon,09¡10'W,Portugal,38¡42'N +San Juan,66¡07'W,Puerto Rico,18¡28'N +Doha,51¡35'E,Qatar,25¡15'N +Seoul,126¡58'E,Republic of Korea,37¡31'N +Bucuresti,26¡10'E,Romania,44¡27'N +Moskva,37¡35'E,Russian Federation,55¡45'N +Kigali,30¡04'E,Rawanda,01¡59'S +Basseterre,62¡43'W,Saint Kitts and Nevis,17¡17'N +Castries,60¡58'W,Saint Lucia,14¡02'N +Saint-Pierre,56¡12'W,Saint Pierre and Miquelon,46¡46'N +Kingstown,61¡10'W,Saint vincent and the Grenadines,13¡10'N +Apia,171¡50'W,Samoa,13¡50'S +San Marino,12¡30'E,San Marino,43¡55'N +Sao Tome,06¡39'E,Sao Tome and Principe,00¡10'N +Riyadh,46¡42'E,Saudi Arabia,24¡41'N +Dakar,17¡29'W,Senegal,14¡34'N +Freetown,13¡17'W,Sierra Leone,08¡30'N +Bratislava,17¡07'E,Slovakia,48¡10'N +Ljubljana,14¡33'E,Slovenia,46¡04'N +Honiara,159¡57'E,Solomon Islands,09¡27'S +Mogadishu,45¡25'E,Somalia,02¡02'N +Pretoria (adm.) / Cap Town (Legislative) / Bloemfontein (Judicial),28¡12'E,South Africa,25¡44'S +Madrid,03¡45'W,Spain,40¡25'N +Khartoum,32¡35'E,Sudan,15¡31'N +Paramaribo,55¡10'W,Suriname,05¡50'N +Mbabane (Adm.),31¡06'E,Swaziland,26¡18'S +Stockholm,18¡03'E,Sweden,59¡20'N +Bern,07¡28'E,Switzerland,46¡57'N +Damascus,36¡18'E,Syrian Arab Republic,33¡30'N +Dushanbe,68¡48'E,Tajikistan,38¡33'N +Bangkok,100¡35'E,Thailand,13¡45'N +Skopje,21¡26'E,The Former Yugoslav Republic of Macedonia,42¡01'N +Lome,01¡20'E,Togo,06¡09'N +Nuku'alofa,174¡00'W,Tonga,21¡10'S +Tunis,10¡11'E,Tunisia,36¡50'N +Ankara,32¡54'E,Turkey,39¡57'N +Ashgabat,57¡50'E,Turkmenistan,38¡00'N +Funafuti,179¡13'E,Tuvalu,08¡31'S +Kampala,32¡30'E,Uganda,00¡20'N +Kiev (Rus),30¡28'E,Ukraine,50¡30'N +Abu Dhabi,54¡22'E,United Arab Emirates,24¡28'N +London,00¡05'W,United Kingdom of Great Britain and Northern Ireland,51¡36'N +Dodoma,35¡45'E,United Republic of Tanzania,06¡08'S +Washington DC,77¡02'W,United States of America,39¡91'N +Charlotte Amalie,64¡56'W,United States of Virgin Islands,18¡21'N +Montevideo,56¡11'W,Uruguay,34¡50'S +Tashkent,69¡10'E,Uzbekistan,41¡20'N +Port-Vila,168¡18'E,Vanuatu,17¡45'S +Caracas,66¡55'W,Venezuela,10¡30'N +Hanoi,105¡55'E,Viet Nam,21¡05'N +Belgrade,20¡37'E,Yugoslavia,44¡50'N +Lusaka,28¡16'E,Zambia,15¡28'S +Harare,31¡02'E,Zimbabwe,17¡43'S diff --git a/Day_2/myfile.txt b/Day_3/myfile.txt similarity index 100% rename from Day_2/myfile.txt rename to Day_3/myfile.txt diff --git a/Glossary.md b/Glossary.md index 059ff44..05867b8 100644 --- a/Glossary.md +++ b/Glossary.md @@ -178,4 +178,7 @@ The classification of something in a program (for example, the contents of a var Indicates the nature of an error in a program. For example, in Python, an `IOError` to problems with file input/output. See also: [syntax error](#syntax-error). #### while loop -A loop that keeps executing as long as some condition is true. See also: [for loop](#for-loop). \ No newline at end of file +A loop that keeps executing as long as some condition is true. See also: [for loop](#for-loop). + +#### whitespace +Any character or series of characters that represent horizontal or vertical space. Generally, space-bar, tab, return/enter. Some whitespace in python is represented differently than what you see. A return is "\n" and tab "\t". \ No newline at end of file diff --git a/README.md b/README.md index 6e6a105..cb872bc 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,14 @@ # python-intensive +### Schedule: + +[Day 1](https://github.com/dlab-berkeley/python-intensive/tree/master/Day_1): Tue, August 16, 2016 - 9:30 AM to 12:30 PM, Barrows 356: D-Lab Convening Room [(Register)](http://dlab.berkeley.edu/training/python-everything-part-0-0) + +[Day 2](https://github.com/dlab-berkeley/python-intensive/tree/master/Day_2): Wed, August 17, 2016 - 9:30 AM to 12:30 PM, Barrows 356: D-Lab Convening Room [(Register)](http://dlab.berkeley.edu/training/python-everything-part-1-1) + +[Day 3](https://github.com/dlab-berkeley/python-intensive/tree/master/Day_3): Thu, August 16, 2016 - 9:30 AM to 12:30 PM, Barrows 356: D-Lab Convening Room [(Register)](http://dlab.berkeley.edu/training/python-everything-part-2-1) + +[Day 4](https://github.com/dlab-berkeley/python-intensive/tree/master/Day_4): Fri, August 16, 2016 - 9:30 AM to 12:30 PM, Barrows 356: D-Lab Convening Room [(Register)](http://dlab.berkeley.edu/training/python-everything-part-3-1) ### Credits: @@ -37,4 +46,3 @@ In other words, we want to turn this: into this: - From 4bf87b785313ead6cf2c428fbe58d77d886ce4d0 Mon Sep 17 00:00:00 2001 From: Chris Hench Date: Mon, 8 Aug 2016 14:31:05 -0700 Subject: [PATCH 3/4] fixed schedule --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cb872bc..9e87d42 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ [Day 2](https://github.com/dlab-berkeley/python-intensive/tree/master/Day_2): Wed, August 17, 2016 - 9:30 AM to 12:30 PM, Barrows 356: D-Lab Convening Room [(Register)](http://dlab.berkeley.edu/training/python-everything-part-1-1) -[Day 3](https://github.com/dlab-berkeley/python-intensive/tree/master/Day_3): Thu, August 16, 2016 - 9:30 AM to 12:30 PM, Barrows 356: D-Lab Convening Room [(Register)](http://dlab.berkeley.edu/training/python-everything-part-2-1) +[Day 3](https://github.com/dlab-berkeley/python-intensive/tree/master/Day_3): Thu, August 18, 2016 - 9:30 AM to 12:30 PM, Barrows 356: D-Lab Convening Room [(Register)](http://dlab.berkeley.edu/training/python-everything-part-2-1) -[Day 4](https://github.com/dlab-berkeley/python-intensive/tree/master/Day_4): Fri, August 16, 2016 - 9:30 AM to 12:30 PM, Barrows 356: D-Lab Convening Room [(Register)](http://dlab.berkeley.edu/training/python-everything-part-3-1) +[Day 4](https://github.com/dlab-berkeley/python-intensive/tree/master/Day_4): Fri, August 19, 2016 - 9:30 AM to 12:30 PM, Barrows 356: D-Lab Convening Room [(Register)](http://dlab.berkeley.edu/training/python-everything-part-3-1) ### Credits: From dba56a55f5cb10b2a62452312d95e75650032c07 Mon Sep 17 00:00:00 2001 From: Chris Hench Date: Mon, 8 Aug 2016 14:32:19 -0700 Subject: [PATCH 4/4] fixed whitespace --- Glossary.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Glossary.md b/Glossary.md index 05867b8..dbf2095 100644 --- a/Glossary.md +++ b/Glossary.md @@ -181,4 +181,4 @@ Indicates the nature of an error in a program. For example, in Python, an `IOErr A loop that keeps executing as long as some condition is true. See also: [for loop](#for-loop). #### whitespace -Any character or series of characters that represent horizontal or vertical space. Generally, space-bar, tab, return/enter. Some whitespace in python is represented differently than what you see. A return is "\n" and tab "\t". \ No newline at end of file +Any character or series of characters that represent horizontal or vertical space. Generally, space-bar, tab, return/enter. Some whitespace in python is represented differently than what you see. A return is `\n` and tab `\t`. \ No newline at end of file