{ "metadata": { "name": "", "signature": "sha256:90c61266902e3bc09f695fd57cfc65c21479a3907957f5cc84723eefb38e9677" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Assimilation and Visualization Short Course\n", "# Visualization practical\n", "\n", "## Wednesday 17th September 2014\n", "## Jon Blower\n", "\n", "# Lesson 1: Brief introduction to Python\n", "\n", "## Objectives\n", "1. Learn some of the advantages (and disadvantages) to programming in Python\n", "1. Learn how to run Python code interactively\n", "1. Learn how to edit and run Python scripts\n", "\n", "(If you are already comfortable with using Python you can probably skip this lesson.)\n", "\n", "## What is Python?\n", "[Python](http://www.python.org) is a *free*, easy-to-use programming language that can be used for all kinds of things, from writing simple five-line scripts to creating very large pieces of software. It runs on Windows, Mac, Linux and many other systems besides.\n", "\n", "Unlike other languages such as Matlab and IDL, Python is not specifically a scientific programming language. Most of the useful scientific functionality comes from software *libraries*. This is both a good and a bad thing: it is good because the Python community (which is large and growing) is constantly releasing new and useful libraries that we can all use; but the disadvantage is that it can be difficult to find and install the libraries we need, and sometimes scientific users are surprised that common functionality (e.g. data plotting) isn't supported \"out of the box\".\n", "\n", "All the libraries you will need to complete this practical are already installed on the computers we will use. See later in this document for instructions about how you can set up your own Python environment on your own computer.\n", "\n", "## Running Python interactively\n", "There are two ways in which you can use Python. In *interactive mode*, you run the Python interpreter and type commands directly into it. The commands are executed as soon as you press Return. Interactive mode is very useful when you are trying new things out.\n", "\n", "Run the Python interpreter by going to the Start Menu (bottom left-hand corner of the screen) and selecting `All Programs \u2192 Python(x,y) \u2192 Command Prompts \u2192 Python interpreter`.\n", "\n", "You should see a prompt that looks a bit like this (it will look a bit different on your system):\n", "\n", "```\n", "Python 2.7.6 (default, Mar 7 2014, 20:25:50) \n", "[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin\n", "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", ">>>\n", "```\n", "\n", "Write a line of Python code, for example \"`print 2+2`\" and press Return. You should see the unsurprising result:\n", "```\n", ">>> print 2+2\n", "4\n", "```\n", "\n", "### Getting help in interactive mode\n", "You can get documentation on Python commands and objects with `help()` and `dir()`. For example, let's say you wanted to know more about Python's mathematical functions. You can do this:\n", "\n", "```\n", ">>> import math\n", ">>> help(math)\n", "```\n", "The first line imports the `math` library, which gives access to common functions such as `sin` and `cos`. The second line prints out some documentation for the library (press `q` to go back to the interactive prompt). You can get documentation on a particular function:\n", "```\n", ">>> help(math.sin)\n", "```\n", "and you can get a simple list of functions that are part of the `math` library with:\n", "```\n", ">>> dir(math)\n", "```\n", "\n", "\n", "## Running Python scripts\n", "As you would expect, you can also write your Python commands in a text file (or *script*) and execute them as a program. You can use any text editor (even Windows' own Notepad) but it's convenient to use a program that is Python-aware.\n", "\n", "### Using the SciTE text editor\n", "Your machine should already have the [SciTE](http://www.scintilla.org/SciTE.html) text editor installed. Run this from the Start Menu by selecting `All Programs \u2192 Python(x,y) \u2192 SciTE`. Use the File menu to load the script `example.py` and run it by pressing F5. You should see the output of the script below.\n", "\n", "SciTE is probably the easiest way to run simple Python programs and I will assume you will run most scripts in this practical in this way. But there are other methods too.\n", "\n", "### Running scripts from the command line (optional)\n", "*This section is optional - it assumes you know the basics of changing directories and running programs from the command prompt. Don't worry if you don't know this, just skip this section.*\n", "\n", "You can run Python scripts from the Windows command prompt (the same method works on the Mac or Linux command prompt if you happen to be working on one of these systems). Open the Command Prompt from the Start Menu by selecting `Accessories \u2192 Command Prompt`. Change into a directory containing a Python script using the `cd` command, then run:\n", "\n", "```\n", "python .py\n", "```\n", "\n", "### Spyder: a more sophisticated development environment (optional)\n", "Many programmers use nothing more than a text editor and a command prompt. However, some programmers prefer to use a more sophisticated development environment. [Spyder](https://code.google.com/p/spyderlib/) is a Matlab-like development environment that allows you to load and run scripts, get access to documentation and program interactively using a built-in command prompt.\n", "\n", "Spyder is provided on the lab PCs, but you don't need to use it unless you prefer this approach (I am not too familiar with it myself so cannot provide much help).\n", "\n", "## **IMPORTANT:** Watch out for WHITESPACE!!\n", "Most newcomers to Python are surprised to find that blocks of code are not separated by braces (i.e. \"`{`\" and \"`}`\" as they are in C, C++, Java and JavaScript), nor are there special commands like \"`end do`\" or \"`end if`\" as there are in languages like Fortran and Pascal. Instead, Python code blocks are recognised by *indentation*. A line of code is recognised as being part of the same code block as the previous line if it is indented by the same set of whitespace characters (spaces, tabs etc).\n", "\n", "So the following function in Python is fine:\n", "```\n", "def double(x):\n", " x = x * 2\n", " print \"the new value of x is\", x\n", " return x\n", "```\n", "because each line within the function is indented by the same number of spaces (two in this example). But this function would not work:\n", "```\n", "def double(x):\n", " x = x * 2\n", " print \"the new value of x is\", x\n", " return x\n", "```\n", "This may appear strange at first, but it is part of the Python philosophy to make code as human-readable as possible and avoid extraneous characters in scripts.\n", "\n", "You can nest indentation too:\n", "```\n", "def printNumbersDoubled(n):\n", " for i in range(n):\n", " i = i * 2\n", " print i\n", "```\n", "See the example script `example.py` for some more examples." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Further reading\n", "The rest of this section contains links and information if you want to find out more. You don't need it for the rest of this practical.\n", "\n", "### Where to learn more about Python\n", "* Introduction to Python for Science - free eBook: https://github.com/djpine/pyman\n", "* A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences (Johnny Lin): http://www.johnny-lin.com/pyintro/\n", "* Scientific Computing in Python (Philip Lewis Qingling Wu and Jos\u001d", "\u00e9 Gomez-Dans, University College London): http://proflewis.github.io/geogg122/\n", "* Introduction to Python, designed for those who know IDL (by Andy Heaps): http://climate.ncas.ac.uk/~andy/python.html\n", "\n", "### Using Python as a lab notebook\n", "These notes were created using a great system called [IPython Notebook](http://ipython.org/notebook.html). Although you may be currently looking at a read-only version of these notes, it's possible to run your own notebook server so that you can edit and run the code *within the notes themselves*. You can do this if you wish. Go to a command prompt and run:\n", "\n", "```\n", "cd /d n:\n", "ipython notebook --no-browser\n", "```\n", "\n", "(The first line changes to the location of the drive containing your files. You may need to replace `n:` with the name of the drive where your files are stored.) Wait a few seconds and the server will start. Open a web browser (use Chrome or Firefox, *not* Internet Explorer) at http://localhost:8888 and you will see a directory tree. Navigate to the location of these lecture notes (look for files with an `.ipynb` extension) and load a notebook. You can then run it \"live\" - see instructions here: http://ipython.org/notebook.html.\n", "\n", "Note that this is ***not*** necessary for this practical. But this can be a great way to experiment with Python and the techniques you're learning in this class." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }