Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Crash Course in Python for Scientists
This file has been truncated, but you can view the full file.
{
"metadata": {
"name": "",
"signature": "sha256:a04c38d9604adb7eb9ca89860dfa1ef72db66037cc2c07c391ef8e67a31f9254"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# A Crash Course in Python for Scientists\n",
"[Rick Muller](http://www.cs.sandia.gov/~rmuller/), Sandia National Laboratories\n",
"\n",
"version 0.6\n",
"\n",
"This work is licensed under a [Creative Commons Attribution-ShareAlike 3.0 Unported License](http://creativecommons.org/licenses/by-sa/3.0/deed.en_US)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Why Python?\n",
"Python is the programming language of choice for many scientists to a large degree because it offers a great deal of power to analyze and model scientific data with relatively little overhead in terms of learning, installation or development time. It is a language you can pick up in a weekend, and use for the rest of one's life.\n",
"\n",
"The [Python Tutorial](http://docs.python.org/2/tutorial/) is a great place to start getting a feel for the language. To complement this material, I taught a [Python Short Course](http://www.wag.caltech.edu/home/rpm/python_course/) years ago to a group of computational chemists during a time that I was worried the field was moving too much in the direction of using canned software rather than developing one's own methods. I wanted to focus on what working scientists needed to be more productive: parsing output of other programs, building simple models, experimenting with object oriented programming, extending the language with C, and simple GUIs. \n",
"\n",
"I'm trying to do something very similar here, to cut to the chase and focus on what scientists need. In the last year or so, the [IPython Project](http://ipython.org) has put together a notebook interface that I have found incredibly valuable. A large number of people have released very good IPython Notebooks that I have taken a huge amount of pleasure reading through. Some ones that I particularly like include:\n",
"\n",
"* Rob Johansson's [excellent notebooks](http://jrjohansson.github.io/), including [Scientific Computing with Python](https://github.com/jrjohansson/scientific-python-lectures) and [Computational Quantum Physics with QuTiP](https://github.com/jrjohansson/qutip-lectures) lectures;\n",
"* [XKCD style graphs in matplotlib](http://nbviewer.ipython.org/url/jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb);\n",
"* [A collection of Notebooks for using IPython effectively](https://github.com/ipython/ipython/tree/master/examples/notebooks#a-collection-of-notebooks-for-using-ipython-effectively)\n",
"* [A gallery of interesting IPython Notebooks](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks)\n",
"\n",
"I find IPython notebooks an easy way both to get important work done in my everyday job, as well as to communicate what I've done, how I've done it, and why it matters to my coworkers. I find myself endlessly sweeping the [IPython subreddit](http://ipython.reddit.com) hoping someone will post a new notebook. In the interest of putting more notebooks out into the wild for other people to use and enjoy, I thought I would try to recreate some of what I was trying to get across in the original Python Short Course, updated by 15 years of Python, Numpy, Scipy, Matplotlib, and IPython development, as well as my own experience in using Python almost every day of this time."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What You Need to Install\n",
"\n",
"There are two branches of current releases in Python: the older-syntax Python 2, and the newer-syntax Python 3. This schizophrenia is largely intentional: when it became clear that some non-backwards-compatible changes to the language were necessary, the Python dev-team decided to go through a five-year (or so) transition, during which the new language features would be introduced and the old language was still actively maintained, to make such a transition as easy as possible. We're now (2013) past the halfway point, and, IMHO, at the first time when I'm considering making the change to Python 3.\n",
"\n",
"Nonetheless, I'm going to write these notes with Python 2 in mind, since this is the version of the language that I use in my day-to-day job, and am most comfortable with. If these notes are important and are valuable to people, I'll be happy to rewrite the notes using Python 3.\n",
"\n",
"With this in mind, these notes assume you have a Python distribution that includes:\n",
"\n",
"* [Python](http://www.python.org) version 2.7;\n",
"* [Numpy](http://www.numpy.org), the core numerical extensions for linear algebra and multidimensional arrays;\n",
"* [Scipy](http://www.scipy.org), additional libraries for scientific programming;\n",
"* [Matplotlib](http://matplotlib.sf.net), excellent plotting and graphing libraries;\n",
"* [IPython](http://ipython.org), with the additional libraries required for the notebook interface.\n",
"\n",
"A good, easy to install option that supports Mac, Windows, and Linux, and that has all of these packages (and much more) is the [Entought Python Distribution](https://www.enthought.com/products/epd), also known as EPD, which appears to be changing its name to Enthought Canopy. Enthought is a commercial company that supports a lot of very good work in scientific Python development and application. You can either purchase a license to use EPD, or there is also a [free version](https://www.enthought.com/products/epd/free/) that you can download and install.\n",
"\n",
"Here are some other alternatives, should you not want to use EPD:\n",
"\n",
"**Linux** Most distributions have an installation manager. Redhat has yum, Ubuntu has apt-get. To my knowledge, all of these packages should be available through those installers.\n",
"\n",
"**Mac** I use [Macports](http://www.macports.org/), which has up-to-date versions of all of these packages.\n",
"\n",
"**Windows** The [PythonXY](https://code.google.com/p/pythonxy/) package has everything you need: install the package, then go to Start > PythonXY > Command Prompts > IPython notebook server.\n",
"\n",
"**Cloud** This notebook is currently not running on the [IPython notebook viewer](http://nbviewer.ipython.org/), but will be shortly, which will allow the notebook to be viewed but not interactively. I'm keeping an eye on [Wakari](http://www.wakari.io), from [Continuum Analytics](http://continuum.io/), which is a cloud-based IPython notebook. Wakari appears to support free accounts as well. Continuum is a company started by some of the core Enthought Numpy/Scipy people focusing on big data. \n",
"\n",
"Continuum also supports a bundled, multiplatform Python package called [Anaconda](https://store.continuum.io/) that I'll also keep an eye on."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# I. Python Overview\n",
"This is a quick introduction to Python. There are lots of other places to learn the language more thoroughly. I have collected a list of useful links, including ones to other learning resources, at the end of this notebook. If you want a little more depth, [Python Tutorial](http://docs.python.org/2/tutorial/) is a great place to start, as is Zed Shaw's [Learn Python the Hard Way](http://learnpythonthehardway.org/book/).\n",
"\n",
"The lessons that follow make use of the IPython notebooks. There's a good introduction to notebooks [in the IPython notebook documentation](http://ipython.org/notebook.html) that even has a [nice video](http://www.youtube.com/watch?v=H6dLGQw9yFQ#!) on how to use the notebooks. You should probably also flip through the [IPython tutorial](http://ipython.org/ipython-doc/dev/interactive/tutorial.html) in your copious free time.\n",
"\n",
"Briefly, notebooks have code cells (that are generally followed by result cells) and text cells. The text cells are the stuff that you're reading now. The code cells start with \"In []:\" with some number generally in the brackets. If you put your cursor in the code cell and hit Shift-Enter, the code will run in the Python interpreter and the result will print out in the output cell. You can then change things around and see whether you understand what's going on. If you need to know more, see the [IPython notebook documentation](http://ipython.org/notebook.html) or the [IPython tutorial](http://ipython.org/ipython-doc/dev/interactive/tutorial.html)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using Python as a Calculator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Many of the things I used to use a calculator for, I now use Python for:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"2+2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"4"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"(50-5*6)/4"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"5"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(If you're typing this into an IPython notebook, or otherwise using notebook file, you hit shift-Enter to evaluate a cell.)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are some gotchas compared to using a normal calculator."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"7/3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"2"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python integer division, like C or Fortran integer division, truncates the remainder and returns an integer. At least it does in version 2. In version 3, Python returns a floating point number. You can get a sneak preview of this feature in Python 2 by importing the module from the future features:\n",
"\n",
" from __future__ import division"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, you can convert one of the integers to a floating point number, in which case the division function returns another floating point number."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"7/3."
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"2.3333333333333335"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"7/float(3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"2.3333333333333335"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the last few lines, we have sped by a lot of things that we should stop for a moment and explore a little more fully. We've seen, however briefly, two different data types: **integers**, also known as *whole numbers* to the non-programming world, and **floating point numbers**, also known (incorrectly) as *decimal numbers* to the rest of the world.\n",
"\n",
"We've also seen the first instance of an **import** statement. Python has a huge number of libraries included with the distribution. To keep things simple, most of these variables and functions are not accessible from a normal Python interactive session. Instead, you have to import the name. For example, there is a **math** module containing many useful functions. To access, say, the square root function, you can either first\n",
"\n",
" from math import sqrt\n",
"\n",
"and then"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sqrt(81)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"9.0"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or you can simply import the math library itself"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import math\n",
"math.sqrt(81)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"9.0"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can define variables using the equals (=) sign:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"width = 20\n",
"length = 30\n",
"area = length*width\n",
"area"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"600"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you try to access a variable that you haven't yet defined, you get an error:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"volume"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'volume' is not defined",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-0c7fc58f9268>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mvolume\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'volume' is not defined"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and you need to define it:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"depth = 10\n",
"volume = area*depth\n",
"volume"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can name a variable *almost* anything you want. It needs to start with an alphabetical character or \"\\_\", can contain alphanumeric charcters plus underscores (\"\\_\"). Certain words, however, are reserved for the language:\n",
"\n",
" and, as, assert, break, class, continue, def, del, elif, else, except, \n",
" exec, finally, for, from, global, if, import, in, is, lambda, not, or,\n",
" pass, print, raise, return, try, while, with, yield\n",
"\n",
"Trying to define a variable using one of these will result in a syntax error:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"return = 0"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-10-2b99136d4ec6>, line 1)",
"output_type": "pyerr",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-10-2b99136d4ec6>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m return = 0\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The [Python Tutorial](http://docs.python.org/2/tutorial/introduction.html#using-python-as-a-calculator) has more on using Python as an interactive shell. The [IPython tutorial](http://ipython.org/ipython-doc/dev/interactive/tutorial.html) makes a nice complement to this, since IPython has a much more sophisticated iteractive shell."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Strings\n",
"Strings are lists of printable characters, and can be defined using either single quotes"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'Hello, World!'"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"'Hello, World!'"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or double quotes"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"Hello, World!\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"'Hello, World!'"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But not both at the same time, unless you want one of the symbols to be part of the string."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"He's a Rebel\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"\"He's a Rebel\""
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'She asked, \"How are you today?\"'"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
"'She asked, \"How are you today?\"'"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just like the other two data objects we're familiar with (ints and floats), you can assign a string to a variable"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"greeting = \"Hello, World!\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The **print** statement is often used for printing character strings:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print greeting"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Hello, World!\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But it can also print data types other than strings:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"The area is \",area"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The area is 600\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the above snipped, the number 600 (stored in the variable \"area\") is converted into a string before being printed out."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can use the + operator to concatenate strings together:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"statement = \"Hello,\" + \"World!\"\n",
"print statement"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Hello,World!\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Don't forget the space between the strings, if you want one there. "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"statement = \"Hello, \" + \"World!\"\n",
"print statement"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Hello, World!\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can use + to concatenate multiple strings in a single statement:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"This \" + \"is \" + \"a \" + \"longer \" + \"statement.\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"This is a longer statement.\n"
]
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you have a lot of words to concatenate together, there are other, more efficient ways to do this. But this is fine for linking a few strings together."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lists\n",
"Very often in a programming language, one wants to keep a group of similar items together. Python does this using a data type called **lists**."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"days_of_the_week = [\"Sunday\",\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\"]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can access members of the list using the **index** of that item:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"days_of_the_week[2]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
"'Tuesday'"
]
}
],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python lists, like C, but unlike Fortran, use 0 as the index of the first element of a list. Thus, in this example, the 0 element is \"Sunday\", 1 is \"Monday\", and so on. If you need to access the *n*th element from the end of the list, you can use a negative index. For example, the -1 element of a list is the last element:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"days_of_the_week[-1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
"'Saturday'"
]
}
],
"prompt_number": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can add additional items to the list using the .append() command:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"languages = [\"Fortran\",\"C\",\"C++\"]\n",
"languages.append(\"Python\")\n",
"print languages"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['Fortran', 'C', 'C++', 'Python']\n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The **range()** command is a convenient way to make sequential lists of numbers:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"range(10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 25,
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
}
],
"prompt_number": 25
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that range(n) starts at 0 and gives the sequential list of integers less than n. If you want to start at a different number, use range(start,stop)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"range(2,8)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 26,
"text": [
"[2, 3, 4, 5, 6, 7]"
]
}
],
"prompt_number": 26
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The lists created above with range have a *step* of 1 between elements. You can also give a fixed step size via a third command:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"evens = range(0,20,2)\n",
"evens"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 27,
"text": [
"[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]"
]
}
],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"evens[3]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 28,
"text": [
"6"
]
}
],
"prompt_number": 28
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lists do not have to hold the same data type. For example,"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[\"Today\",7,99.3,\"\"]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 29,
"text": [
"['Today', 7, 99.3, '']"
]
}
],
"prompt_number": 29
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, it's good (but not essential) to use lists for similar objects that are somehow logically connected. If you want to group different data types together into a composite data object, it's best to use **tuples**, which we will learn about below.\n",
"\n",
"You can find out how long a list is using the **len()** command:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"help(len)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Help on built-in function len in module __builtin__:\n",
"\n",
"len(...)\n",
" len(object) -> integer\n",
" \n",
" Return the number of items of a sequence or collection.\n",
"\n"
]
}
],
"prompt_number": 30
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(evens)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 31,
"text": [
"10"
]
}
],
"prompt_number": 31
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iteration, Indentation, and Blocks\n",
"One of the most useful things you can do with lists is to *iterate* through them, i.e. to go through each element one at a time. To do this in Python, we use the **for** statement:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for day in days_of_the_week:\n",
" print day"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sunday\n",
"Monday\n",
"Tuesday\n",
"Wednesday\n",
"Thursday\n",
"Friday\n",
"Saturday\n"
]
}
],
"prompt_number": 32
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This code snippet goes through each element of the list called **days_of_the_week** and assigns it to the variable **day**. It then executes everything in the indented block (in this case only one line of code, the print statement) using those variable assignments. When the program has gone through every element of the list, it exists the block.\n",
"\n",
"(Almost) every programming language defines blocks of code in some way. In Fortran, one uses END statements (ENDDO, ENDIF, etc.) to define code blocks. In C, C++, and Perl, one uses curly braces {} to define these blocks.\n",
"\n",
"Python uses a colon (\":\"), followed by indentation level to define code blocks. Everything at a higher level of indentation is taken to be in the same block. In the above example the block was only a single line, but we could have had longer blocks as well:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for day in days_of_the_week:\n",
" statement = \"Today is \" + day\n",
" print statement"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Today is Sunday\n",
"Today is Monday\n",
"Today is Tuesday\n",
"Today is Wednesday\n",
"Today is Thursday\n",
"Today is Friday\n",
"Today is Saturday\n"
]
}
],
"prompt_number": 33
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The **range()** command is particularly useful with the **for** statement to execute loops of a specified length:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for i in range(20):\n",
" print \"The square of \",i,\" is \",i*i"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The square of 0 is 0\n",
"The square of 1 is 1\n",
"The square of 2 is 4\n",
"The square of 3 is 9\n",
"The square of 4 is 16\n",
"The square of 5 is 25\n",
"The square of 6 is 36\n",
"The square of 7 is 49\n",
"The square of 8 is 64\n",
"The square of 9 is 81\n",
"The square of 10 is 100\n",
"The square of 11 is 121\n",
"The square of 12 is 144\n",
"The square of 13 is 169\n",
"The square of 14 is 196\n",
"The square of 15 is 225\n",
"The square of 16 is 256\n",
"The square of 17 is 289\n",
"The square of 18 is 324\n",
"The square of 19 is 361\n"
]
}
],
"prompt_number": 34
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Slicing\n",
"Lists and strings have something in common that you might not suspect: they can both be treated as sequences. You already know that you can iterate through the elements of a list. You can also iterate through the letters in a string:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for letter in \"Sunday\":\n",
" print letter"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"S\n",
"u\n",
"n\n",
"d\n",
"a\n",
"y\n"
]
}
],
"prompt_number": 35
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is only occasionally useful. Slightly more useful is the *slicing* operation, which you can also use on any sequence. We already know that we can use *indexing* to get the first element of a list:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"days_of_the_week[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 36,
"text": [
"'Sunday'"
]
}
],
"prompt_number": 36
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we want the list containing the first two elements of a list, we can do this via"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"days_of_the_week[0:2]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 37,
"text": [
"['Sunday', 'Monday']"
]
}
],
"prompt_number": 37
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or simply"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"days_of_the_week[:2]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 38,
"text": [
"['Sunday', 'Monday']"
]
}
],
"prompt_number": 38
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we want the last items of the list, we can do this with negative slicing:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"days_of_the_week[-2:]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 39,
"text": [
"['Friday', 'Saturday']"
]
}
],
"prompt_number": 39
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"which is somewhat logically consistent with negative indices accessing the last elements of the list.\n",
"\n",
"You can do:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"workdays = days_of_the_week[1:6]\n",
"print workdays"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n"
]
}
],
"prompt_number": 40
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since strings are sequences, you can also do this to them:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"day = \"Sunday\"\n",
"abbreviation = day[:3]\n",
"print abbreviation"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sun\n"
]
}
],
"prompt_number": 41
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we really want to get fancy, we can pass a third element into the slice, which specifies a step length (just like a third argument to the **range()** function specifies the step):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"numbers = range(0,40)\n",
"evens = numbers[2::2]\n",
"evens"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 42,
"text": [
"[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]"
]
}
],
"prompt_number": 42
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that in this example I was even able to omit the second argument, so that the slice started at 2, went to the end of the list, and took every second element, to generate the list of even numbers less that 40."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Booleans and Truth Testing\n",
"We have now learned a few data types. We have integers and floating point numbers, strings, and lists to contain them. We have also learned about lists, a container that can hold any data type. We have learned to print things out, and to iterate over items in lists. We will now learn about **boolean** variables that can be either True or False.\n",
"\n",
"We invariably need some concept of *conditions* in programming to control branching behavior, to allow a program to react differently to different situations. If it's Monday, I'll go to work, but if it's Sunday, I'll sleep in. To do this in Python, we use a combination of **boolean** variables, which evaluate to either True or False, and **if** statements, that control branching based on boolean values."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"if day == \"Sunday\":\n",
" print \"Sleep in\"\n",
"else:\n",
" print \"Go to work\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sleep in\n"
]
}
],
"prompt_number": 43
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(Quick quiz: why did the snippet print \"Go to work\" here? What is the variable \"day\" set to?)\n",
"\n",
"Let's take the snippet apart to see what happened. First, note the statement"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"day == \"Sunday\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 44,
"text": [
"True"
]
}
],
"prompt_number": 44
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we evaluate it by itself, as we just did, we see that it returns a boolean value, False. The \"==\" operator performs *equality testing*. If the two items are equal, it returns True, otherwise it returns False. In this case, it is comparing two variables, the string \"Sunday\", and whatever is stored in the variable \"day\", which, in this case, is the other string \"Saturday\". Since the two strings are not equal to each other, the truth test has the false value."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The if statement that contains the truth test is followed by a code block (a colon followed by an indented block of code). If the boolean is true, it executes the code in that block. Since it is false in the above example, we don't see that code executed.\n",
"\n",
"The first block of code is followed by an **else** statement, which is executed if nothing else in the above if statement is true. Since the value was false, this code is executed, which is why we see \"Go to work\".\n",
"\n",
"You can compare any data types in Python:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"1 == 2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 45,
"text": [
"False"
]
}
],
"prompt_number": 45
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"50 == 2*25"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 46,
"text": [
"True"
]
}
],
"prompt_number": 46
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"3 < 3.14159"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 47,
"text": [
"True"
]
}
],
"prompt_number": 47
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"1 == 1.0"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 48,
"text": [
"True"
]
}
],
"prompt_number": 48
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"1 != 0"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 49,
"text": [
"True"
]
}
],
"prompt_number": 49
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"1 <= 2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 50,
"text": [
"True"
]
}
],
"prompt_number": 50
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"1 >= 1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 51,
"text": [
"True"
]
}
],
"prompt_number": 51
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We see a few other boolean operators here, all of which which should be self-explanatory. Less than, equality, non-equality, and so on.\n",
"\n",
"Particularly interesting is the 1 == 1.0 test, which is true, since even though the two objects are different data types (integer and floating point number), they have the same *value*. There is another boolean operator **is**, that tests whether two objects are the same object:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"1 is 1.0"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 52,
"text": [
"False"
]
}
],
"prompt_number": 52
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can do boolean tests on lists as well:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[1,2,3] == [1,2,4]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 53,
"text": [
"False"
]
}
],
"prompt_number": 53
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[1,2,3] < [1,2,4]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 54,
"text": [
"True"
]
}
],
"prompt_number": 54
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, note that you can also string multiple comparisons together, which can result in very intuitive tests:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"hours = 5\n",
"0 < hours < 24"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 55,
"text": [
"True"
]
}
],
"prompt_number": 55
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If statements can have **elif** parts (\"else if\"), in addition to if/else parts. For example:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"if day == \"Sunday\":\n",
" print \"Sleep in\"\n",
"elif day == \"Saturday\":\n",
" print \"Do chores\"\n",
"else:\n",
" print \"Go to work\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sleep in\n"
]
}
],
"prompt_number": 56
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Of course we can combine if statements with for loops, to make a snippet that is almost interesting:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for day in days_of_the_week:\n",
" statement = \"Today is \" + day\n",
" print statement\n",
" if day == \"Sunday\":\n",
" print \" Sleep in\"\n",
" elif day == \"Saturday\":\n",
" print \" Do chores\"\n",
" else:\n",
" print \" Go to work\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Today is Sunday\n",
" Sleep in\n",
"Today is Monday\n",
" Go to work\n",
"Today is Tuesday\n",
" Go to work\n",
"Today is Wednesday\n",
" Go to work\n",
"Today is Thursday\n",
" Go to work\n",
"Today is Friday\n",
" Go to work\n",
"Today is Saturday\n",
" Do chores\n"
]
}
],
"prompt_number": 57
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is something of an advanced topic, but ordinary data types have boolean values associated with them, and, indeed, in early versions of Python there was not a separate boolean object. Essentially, anything that was a 0 value (the integer or floating point 0, an empty string \"\", or an empty list []) was False, and everything else was true. You can see the boolean value of any data object using the **bool()** function."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bool(1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 58,
"text": [
"True"
]
}
],
"prompt_number": 58
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bool(0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 59,
"text": [
"False"
]
}
],
"prompt_number": 59
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"bool([\"This \",\" is \",\" a \",\" list\"])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 60,
"text": [
"True"
]
}
],
"prompt_number": 60
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code Example: The Fibonacci Sequence\n",
"The [Fibonacci sequence](http://en.wikipedia.org/wiki/Fibonacci_number) is a sequence in math that starts with 0 and 1, and then each successive entry is the sum of the previous two. Thus, the sequence goes 0,1,1,2,3,5,8,13,21,34,55,89,...\n",
"\n",
"A very common exercise in programming books is to compute the Fibonacci sequence up to some number **n**. First I'll show the code, then I'll discuss what it is doing."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"n = 10\n",
"sequence = [0,1]\n",
"for i in range(2,n): # This is going to be a problem if we ever set n <= 2!\n",
" sequence.append(sequence[i-1]+sequence[i-2])\n",
"print sequence"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n"
]
}
],
"prompt_number": 61
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's go through this line by line. First, we define the variable **n**, and set it to the integer 20. **n** is the length of the sequence we're going to form, and should probably have a better variable name. We then create a variable called **sequence**, and initialize it to the list with the integers 0 and 1 in it, the first two elements of the Fibonacci sequence. We have to create these elements \"by hand\", since the iterative part of the sequence requires two previous elements.\n",
"\n",
"We then have a for loop over the list of integers from 2 (the next element of the list) to **n** (the length of the sequence). After the colon, we see a hash tag \"#\", and then a **comment** that if we had set **n** to some number less than 2 we would have a problem. Comments in Python start with #, and are good ways to make notes to yourself or to a user of your code explaining why you did what you did. Better than the comment here would be to test to make sure the value of **n** is valid, and to complain if it isn't; we'll try this later.\n",
"\n",
"In the body of the loop, we append to the list an integer equal to the sum of the two previous elements of the list.\n",
"\n",
"After exiting the loop (ending the indentation) we then print out the whole list. That's it!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions\n",
"We might want to use the Fibonacci snippet with different sequence lengths. We could cut an paste the code into another cell, changing the value of **n**, but it's easier and more useful to make a function out of the code. We do this with the **def** statement in Python:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def fibonacci(sequence_length):\n",
" \"Return the Fibonacci sequence of length *sequence_length*\"\n",
" sequence = [0,1]\n",
" if sequence_length < 1:\n",
" print \"Fibonacci sequence only defined for length 1 or greater\"\n",
" return\n",
" if 0 < sequence_length < 3:\n",
" return sequence[:sequence_length]\n",
" for i in range(2,sequence_length): \n",
" sequence.append(sequence[i-1]+sequence[i-2])\n",
" return sequence"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 62
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now call **fibonacci()** for different sequence_lengths:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fibonacci(2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 63,
"text": [
"[0, 1]"
]
}
],
"prompt_number": 63
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fibonacci(12)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 64,
"text": [
"[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]"
]
}
],
"prompt_number": 64
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We've introduced a several new features here. First, note that the function itself is defined as a code block (a colon followed by an indented block). This is the standard way that Python delimits things. Next, note that the first line of the function is a single string. This is called a **docstring**, and is a special kind of comment that is often available to people using the function through the python command line:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"help(fibonacci)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Help on function fibonacci in module __main__:\n",
"\n",
"fibonacci(sequence_length)\n",
" Return the Fibonacci sequence of length *sequence_length*\n",
"\n"
]
}
],
"prompt_number": 65
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you define a docstring for all of your functions, it makes it easier for other people to use them, since they can get help on the arguments and return values of the function.\n",
"\n",
"Next, note that rather than putting a comment in about what input values lead to errors, we have some testing of these values, followed by a warning if the value is invalid, and some conditional code to handle special cases."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Recursion and Factorials\n",
"Functions can also call themselves, something that is often called *recursion*. We're going to experiment with recursion by computing the factorial function. The factorial is defined for a positive integer **n** as\n",
" \n",
"$$ n! = n(n-1)(n-2)\\cdots 1 $$\n",
"\n",
"First, note that we don't need to write a function at all, since this is a function built into the standard math library. Let's use the help function to find out about it:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from math import factorial\n",
"help(factorial)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Help on built-in function factorial in module math:\n",
"\n",
"factorial(...)\n",
" factorial(x) -> Integral\n",
" \n",
" Find x!. Raise a ValueError if x is negative or non-integral.\n",
"\n"
]
}
],
"prompt_number": 66
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is clearly what we want."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"factorial(20)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 67,
"text": [
"2432902008176640000"
]
}
],
"prompt_number": 67
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, if we did want to write a function ourselves, we could do recursively by noting that\n",
"\n",
"$$ n! = n(n-1)!$$\n",
"\n",
"The program then looks something like:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def fact(n):\n",
" if n <= 0:\n",
" return 1\n",
" return n*fact(n-1)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 68
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fact(20)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 69,
"text": [
"2432902008176640000"
]
}
],
"prompt_number": 69
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recursion can be very elegant, and can lead to very simple programs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Two More Data Structures: Tuples and Dictionaries\n",
"Before we end the Python overview, I wanted to touch on two more data structures that are very useful (and thus very common) in Python programs.\n",
"\n",
"A **tuple** is a sequence object like a list or a string. It's constructed by grouping a sequence of objects together with commas, either without brackets, or with parentheses:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t = (1,2,'hi',9.0)\n",
"t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 70,
"text": [
"(1, 2, 'hi', 9.0)"
]
}
],
"prompt_number": 70
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tuples are like lists, in that you can access the elements using indices:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t[1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 71,
"text": [
"2"
]
}
],
"prompt_number": 71
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, tuples are *immutable*, you can't append to them or change the elements of them:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t.append(7)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'tuple' object has no attribute 'append'",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-72-50c7062b1d5f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'"
]
}
],
"prompt_number": 72
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"t[1]=77"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-73-03cc8ba9c07d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m77\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"prompt_number": 73
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tuples are useful anytime you want to group different pieces of data together in an object, but don't want to create a full-fledged class (see below) for them. For example, let's say you want the Cartesian coordinates of some objects in your program. Tuples are a good way to do this:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"('Bob',0.0,21.0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 74,
"text": [
"('Bob', 0.0, 21.0)"
]
}
],
"prompt_number": 74
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Again, it's not a necessary distinction, but one way to distinguish tuples and lists is that tuples are a collection of different things, here a name, and x and y coordinates, whereas a list is a collection of similar things, like if we wanted a list of those coordinates:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"positions = [\n",
" ('Bob',0.0,21.0),\n",
" ('Cat',2.5,13.1),\n",
" ('Dog',33.0,1.2)\n",
" ]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 75
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tuples can be used when functions return more than one value. Say we wanted to compute the smallest x- and y-coordinates of the above list of objects. We could write:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def minmax(objects):\n",
" minx = 1e20 # These are set to really big numbers\n",
" miny = 1e20\n",
" for obj in objects:\n",
" name,x,y = obj\n",
" if x < minx: \n",
" minx = x\n",
" if y < miny:\n",
" miny = y\n",
" return minx,miny\n",
"\n",
"x,y = minmax(positions)\n",
"print x,y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0.0 1.2\n"
]
}
],
"prompt_number": 76
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we did two things with tuples you haven't seen before. First, we unpacked an object into a set of named variables using *tuple assignment*:\n",
"\n",
" >>> name,x,y = obj\n",
"\n",
"We also returned multiple values (minx,miny), which were then assigned to two other variables (x,y), again by tuple assignment. This makes what would have been complicated code in C++ rather simple.\n",
"\n",
"Tuple assignment is also a convenient way to swap variables:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x,y = 1,2\n",
"y,x = x,y\n",
"x,y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 77,
"text": [
"(2, 1)"
]
}
],
"prompt_number": 77
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Dictionaries** are an object called \"mappings\" or \"associative arrays\" in other languages. Whereas a list associates an integer index with a set of objects:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mylist = [1,2,9,21]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 78
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The index in a dictionary is called the *key*, and the corresponding dictionary entry is the *value*. A dictionary can use (almost) anything as the key. Whereas lists are formed with square brackets [], dictionaries use curly brackets {}:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ages = {\"Rick\": 46, \"Bob\": 86, \"Fred\": 21}\n",
"print \"Rick's age is \",ages[\"Rick\"]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Rick's age is 46\n"
]
}
],
"prompt_number": 79
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There's also a convenient way to create dictionaries without having to quote the keys."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dict(Rick=46,Bob=86,Fred=20)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 80,
"text": [
"{'Bob': 86, 'Fred': 20, 'Rick': 46}"
]
}
],
"prompt_number": 80
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The **len()** command works on both tuples and dictionaries:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(t)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 81,
"text": [
"4"
]
}
],
"prompt_number": 81
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(ages)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 82,
"text": [
"3"
]
}
],
"prompt_number": 82
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotting with Matplotlib\n",
"We can generally understand trends in data by using a plotting program to chart it. Python has a wonderful plotting library called [Matplotlib](http://matplotlib.sf.net). The IPython notebook interface we are using for these notes has that functionality built in.\n",
"\n",
"As an example, we have looked at two different functions, the Fibonacci function, and the factorial function, both of which grow faster than polynomially. Which one grows the fastest? Let's plot them. First, let's generate the Fibonacci sequence of length 20:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fibs = fibonacci(10)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 83
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next lets generate the factorials."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"facts = []\n",
"for i in range(10):\n",
" facts.append(factorial(i))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 84
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we use the Matplotlib function **plot** to compare the two."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"figsize(8,6)\n",
"plot(facts,label=\"factorial\")\n",
"plot(fibs,label=\"Fibonacci\")\n",
"xlabel(\"n\")\n",
"legend()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 85,
"text": [
"<matplotlib.legend.Legend at 0x10d1b4890>"
]
},
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAfgAAAF/CAYAAAClndsfAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xm0XVWZ9/vvkz5AuhMghJBGJSiRJuAFlJLBURRCH71v\nmRRUAVW59SoRAa3yCtQVwkDfIQpFoRYZ9SpKI8kF5SoCEQHxMESBAJJGAiaICWlIICckECRFQp77\nx16Jm5Bmn3Y3+X7G2OOsPddca8+Vk+S351xzrRWZiSRJaiw9qt0ASZLU+Qx4SZIakAEvSVIDMuAl\nSWpABrwkSQ3IgJckqQFVFPAR0TMino6Iu4v3TRHxQEQsjIj7I2JwWd1LI2JRRDwXESeWlX8oIuYX\n664vK+8bEbcX5Y9FxOiydecWn7EwIs7pnEOWJKnxVdqDvwhYAGy5aP4S4IHMPAj4VfGeiBgHTALG\nAROAGyIiim2mA1MycywwNiImFOVTgNai/Drg6mJfTcDlwNHF64ryLxKSJGnHdhnwEXEAcArwfWBL\nWJ8B3Fws3wxMLJbPBGZm5sbMXAw8DxwTEcOBAZk5u6h3S9k25fu6EzihWD4JuD8z12bmWuABSl8a\nJEnSLlTSg78O+DKwuaxsWGauKpZXAcOK5f2BZWX1lgEjtlO+vCin+LkUIDM3AesiYuhO9iVJknZh\npwEfEacBL2fm0/y19/4OWbrXrfe7lSSphvTaxfpjgTMi4hSgHzAwIm4FVkXEfpm5shh+f7movxwY\nWbb9AZR63suL5W3Lt2wzClgREb2AQZnZGhHLgeaybUYCD23bwIjwy4UkabeTmdvteG+x0x58Zl6W\nmSMz8z3AZOChzPwH4OfAuUW1c4GfFcs/ByZHRJ+IeA8wFpidmSuB1yLimGLS3T8Ad5Vts2Vf/4PS\npD2A+4ETI2JwRAwBPgn8cgftrPvXFVdcUfU2eAweR629GuEYGuU4GuEYGuk4KrGrHvy7srT4+Q3g\njoiYAiwGPlME7YKIuIPSjPtNwNT8a0umAjcB/YFZmXlfUX4jcGtELAJaKX2RIDPXRMRVwBNFvSuz\nNNlOkiTtQsUBn5kPAw8Xy2uAT+yg3v8C/td2yp8CDt1O+X9TfEHYzrofAj+stI2SJKnEO9nViObm\n5mo3ocMa4RjA46gljXAM0BjH0QjHAI1zHJWISsfya1VEZL0fgyRJbRER5C4m2bX1HLwkaTfx1xuR\nqpra24k14CVJO+QIaXV15EuW5+AlSWpABrwkSQ3IgJckqQEZ8JKkuvPHP/6R8ePHM3DgQL773e9W\nrR3nn38+X/va1yqq29zczI033tjFLforJ9lJkurON7/5TU444QTmzJnT7n2cd955jBw5kquuuqrd\n+5g+fXrFdSOiW69MsAcvSao7S5YsYdy4cVVtw+bNm3ddqYoMeElSXfn4xz9OS0sLF1xwAQMGDODb\n3/42RxxxBIMGDWLUqFFceeWV76j/yCOPcOyxxzJkyBBGjRrFzTffzPe+9z1mzJjBN7/5TQYMGMCZ\nZ54JwLPPPktzczNDhgzhkEMO4e677966n/POO4/zzz+fU045hb322otf//rXnHfeeXz1q18F4NVX\nX+W0005j3333pampidNPP53ly5d33x/MNgx4SVJdeeihhzjuuOP4z//8T15//XUOP/xwfvSjH7Fu\n3Truvfdepk+fzl13lR5YumTJEk455RQuuugiVq9ezZw5cxg/fjz//M//zNlnn81XvvIVXn/9de66\n6y42btzI6aefzoQJE3jllVf4zne+w9lnn83ChQu3fvbMmTP56le/yvr16/noRz/6jmH3zGTKlCm8\n+OKLvPjii/Tv358LLrigKn9GYMBLkurUlpvwHH/88Xzwgx8E4NBDD2Xy5Mk8/PDDAMyYMYNPfvKT\nTJo0iZ49e9LU1MThhx/+rn0APPbYY7zxxhtccskl9OrVi4997GOcdtppzJw5c2udiRMn8pGPfASA\nvn37vmMfTU1NfOpTn6Jfv37stddeXHbZZVvbUQ0GvCSpXSI659X+zy9t/Pjjj/Oxj32Mfffd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l8PCJuqXKb2iUzXyp+vgL8lNJpuXqyDFhW\nNgr0E0qBX69OBp4qfh/16BPAnzOzNTM3Af8fpX8vdSUzf5CZ/0dmHg+spTQXbbvqOeCfBMZGxJji\nm+Uk4OdVbtNuKSICuBFYkJn/Ue32tFdE7B0Rg4vl/sAnqbMHG2XmZZk5MjPfQ2k49aHMPKfa7Wqr\niNgjIgYUy3sCJwJ1daVJZq4ElkbEQUXRJ4Bnqtikjvo7Sl8a69US4MMR0b/4P+sTwIIqt6nNImLf\n4uco4FPs5JRJTd/oZmca5SY4ETETOB4YGhFLgcsz84dVblZb/Q3w98C8iHi6KLs0M++rYpvaYzhw\nc3GFRg/g9sycVeU2dVS9nsoaBvy09P8wvYDbMvP+6japXb4A3FZ0Qv4E/GOV29MuxZesTwD1OBcC\ngMycHRE/AX5P6a7+vwf+d3Vb1S4/iYihlGbRT83M13ZUsW4vk5MkSTtWz0P0kiRpBwx4SZIakAEv\nSVIDMuAlSWpABrwkSQ3IgJckqQEZ8JIkNSADXpKkBmTAS2qT4vbQz0bE/46IP0TELyOiX7XbJemd\nDHhJ7XEg8N3MPITSAy/+zyq3R9I2DHhJ7fHnzJxXLD9F6RGckmqIAS+pPf67bPlt6vjBVVKjMuAl\nSWpABryk9tj2MZQ+llKqMT4uVpKkBmQPXpKkBmTAS5LUgAx4SZIakAEvSVIDMuAlSWpABrwkSQ3I\ngJckqQEZ8JIkNaD/H0SXosygsHLrAAAAAElFTkSuQmCC\n",
"text": [
"<matplotlib.figure.Figure at 0x10d1047d0>"
]
}
],
"prompt_number": 85
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The factorial function grows much faster. In fact, you can't even see the Fibonacci sequence. It's not entirely surprising: a function where we multiply by n each iteration is bound to grow faster than one where we add (roughly) n each iteration.\n",
"\n",
"Let's plot these on a semilog plot so we can see them both a little more clearly:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"semilogy(facts,label=\"factorial\")\n",
"semilogy(fibs,label=\"Fibonacci\")\n",
"xlabel(\"n\")\n",
"legend()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 86,
"text": [
"<matplotlib.legend.Legend at 0x10d2bee90>"
]
},
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAeYAAAGBCAYAAACzYhItAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xd4VGXexvHvkwDSSyjSEiIqKEWaCISwRAWlKYpKRCwg\ni2UX14ZrBcLa1sJacOV1UVBXwSAWmlgQBmlSpEkHXUgo0omEFpI87x8njAEFJskk58zk/lwXl5mT\nmTO/QeDO0421FhEREfGGCLcLEBERkd8omEVERDxEwSwiIuIhCmYREREPUTCLiIh4iIJZRETEQxTM\nIiIiHqJgFhER8ZASwb6hMcYAzwAVgCXW2veD/R4iIiLhqjBazNcBdYAMYGsh3F9ERCRsBRTMxpgx\nxpidxpgfT7nexRizzhiz0RjzaM7lBsA8a+1g4N4g1ysiIhLWAm0xjwW65L5gjIkE3si53gjoY4y5\nGKeVfCDnadlBqlNERKRYCCiYrbVzgP2nXL4M2GSt3WytPQ58BPQEPgWuNsa8DviCWKuIiEjYK8jk\nrzpAaq7HW4E21tojwJ/P9mJjjI61EhGRYsVaa872nIJM/ipwsFprQ/7XsGHDXK9BnyF8Pkc4fAZ9\nDm/9CofPEC6fI1AFCeZtQHSux9FoFraIiEiBFCSYlwAXGmNijTGlgERgcl5ukJSUhM/nK0AJIiIi\n3ubz+UhKSgr4+YEulxoPzAcaGGNSjTH9rbWZwCDgK2ANkGytXZuXYpOSkkhISMjLSzwn1OuH8PgM\nEB6fIxw+A+hzeEk4fAYI7c+RkJCQp2A2een3DiZjjB02bBgJCQkh/RsuIiJyJj6fD5/Px/Dhw7EB\nTP5yNZjdem8RkXDm7IwsbvqjfDPGBBTMQd8rW0RE3KeGj3sK+oORTpcSERHxEFeDWbOyRUQk3OV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rFixAoBzzz2Xn3/+2f/ctm3bUrZsWV588UWOHz+Oz+dj6tSp3HzzzcAf7xue\nO3jT09MpU6YMlSpVYt++fb+bhHa6exQWBbOIiLjizTffZOjQoVSsWJGnn36axMRE//diYmL44osv\nGDFiBFWrVqVFixasXLkSgAEDBrBmzRqqVKlCr169KFmyJFOmTGH69OlUr16dQYMG8d///pcGDRoA\nf7wOOfe1Bx54gCNHjlCtWjXi4uLo2rXrHz6/qGhLThGRMKMtOd0V0ltyavKXiIiEOx1iISJSzKnF\n7K6QbjGLiIgUB5MnB/5cBbOIiEghmjUL/vznwJ+vYBYRESkkS5ZAYiIkJwf+GgWziIhIIVizBnr0\ngLffhssvD/x1CmYREZEg27wZrr4aXnoJrr02b6/VcikREZEg69wZ/v53uO02LZcSESn2QmW5lLWW\nihUr8uOPPxIbG0u/fv2Ijo7m6aefdru0s7r33nupU6cOTz311O++Z4xh+HDL0KG/vx7IcqkSQatS\nRETkDGJjY9m1axeRkZGAE1QbN26kZs2a/sdFufVlQYwaNeqM3x8yJP/31hiziIgUCWMMU6dO5eDB\ngxw8eJBff/3VH8onhEJLPxAF+flCwSwiIq6JiIg46aSoPXv2cNVVV1GxYkUSEhJISUnxf2/+/Pm0\nbt2aypUrc9lll7FgwQL/9xISEhg6dCjx8fFUrFiRq6++2n+2M8BNN91ErVq1qFy5Mh07dmTNmjX+\n7x05coSHH36Y2NhYKleuTIcOHTh69CgAc+fOJS4ujipVqhATE8P7778PQL9+/RhSkGbxmX5PCuWu\nIiIif+BMLWJrLR9++CFDhw5lz549NG/enL59+wKwb98+unfvzgMPPMC+fft46KGH6N69O/v37/e/\nfvz48bz77rvs2rWLjIwMXn75Zf/3unfvzqZNm9i9ezctW7b03xdg8ODBLFu2jAULFrBv3z5eeukl\nIiIi2LJlC926deP+++9nz549LF++nGbNmgGF2+2uYBYRkSJhreW6666jSpUqVKlSheuvv/53z+nR\nowfx8fGUKlWKZ599lgULFrB161amTZtGw4YN6du3LxEREdx8881cdNFF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2s/drqcgcrobZwFd3K4lH+KAa3PGZ8cDVxhj3j/LazzJWrsj57+7gc9whrBCyVZga66e\nl4k4QR2qugI/5Pz/CDWdgP9Za/fmTDj+FOfvSsix1o6x1l5qre0IHMCZb/WHijqYtcTKQ4yz7+Y7\nwBpr7atu15MfxphqxpjKOV+XATrjjJWHFGvtE9baaGvteTjdjjOttbe7XVdeGWPKGmMq5HxdDrgK\nCKnVC9baX4BUY0yDnEudgNUullRQfXB+2AtFW4C2xpgyOf9edcJZBRRyjDE1cv4bA1zPGYYWivTg\nQWttpjHmxBKrSOCdUJsBDP7lYx2BqsaYVGCotXasy2XlR3vgVmClMWZZzrXHrbVfulhTXtUC3suZ\n8R+Bs2zvC5drCoZQHfY5F/gsZ6/1EsCH1tqv3S0pX+4DPsxpQPwE9He5nnzJ+eGoExCKY/1YaxcZ\nYyYCS4HMnP/+x92q8m2iMaYqzqzsv1hrfz3dE4t0uZSIiIicWdgfyygiIhJKFMwiIiIeomAWERHx\nEAWziIiIhyiYRUREPETBLCIi4iEKZhEREQ9RMIuIiHiIglmkmMjZCnetMeY/OYe1f2WMKe12XSJy\nMgWzSPFyAfCGtbYJzkb6N7hcj4icQsEsUrz8z1q7MufrH3CO0xMRD1EwixQvx3J9nUURH2QjImen\nYBYREfEQBbNI8XLqcXI6Xk7EY3Tso4iIiIeoxSwiIuIhCmYREREPUTCLiIh4iIJZRETEQxTMIiIi\nHqJgFhER8RAFs4iIiIcomEVERDzk/wEzcL5PbDD7RgAAAABJRU5ErkJggg==\n",
"text": [
"<matplotlib.figure.Figure at 0x10d11c2d0>"
]
}
],
"prompt_number": 86
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are many more things you can do with Matplotlib. We'll be looking at some of them in the sections to come. In the meant