# Tutorial Part I: Python language elements¶

Note

In the following subsections, most of the material will be presented using Jupyter Notebooks. In each case, there will be a first link to a notebook file that you can download and run, and a second link to a static web page with a rendering of that notebook, but with no ability to change anything. It is strongly recommended that whenever you can–that is, whenever you are working on a machine with Jupyter, IPython, numpy, and matplotlib–you download and run the notebook. Then you can experiment with the examples, which is a much more effective way to learn than merely reading. Don’t be afraid of messing up a notebook; you can always re-download it to get back to the original.

Note

• To download, you probably need to do something like right-click (or two-finger select on a Mac touchpad) and select “Save link as…”.

• Then you need to move the notebook file, which will have the extension “.ipynb”, into the directory in which you want to run:

jupyter notebook


This will bring up a list of available notebooks. Making a subdirectory dedicated to these notebooks is highly recommended.

Warning

Do not use Safari to download notebook files or data files. It is prone to adding unwanted extensions to filenames or otherwise interfering with the operation. If you are on OS X, install and use Firefox or Chrome instead. (Or be prepared to fix the problem that Safari creates.)

## Basic data types and containers¶

Python has several basic data types and containers. Becoming familiar with them is essential to effective use of Python, and is a good starting point for learning Python. We will introduce the most important ones here: strings (text), numbers (integers and floating point numbers), tuples (simple sequences), lists (more flexible sequences), and dictionaries.

## Variable names and assignment¶

Python variable names are case-sensitive and start with a letter or an underscore. A leading underscore may be used by convention to indicate a variable or function that is intended for local use only, but it has no special meaning to the Python interpreter. Names that start and end with double underscores, however, are used for special purposes, so to avoid confusion you should not use such names for ordinary variables or functions. There are some subtleties associated with Python objects, names, and variable assignment, so don’t skip the following short notebook:

## Control: branching and looping¶

Every computer language has mechanisms for branching (basing a decision about what to do next on some present condition) and looping (repeating a set of steps until some condition is satisfied). These basic control structures in Python are illustrated in:

## Functions¶

Computers are capable of doing calculations of staggering complexity—but when programs get too complex, they are likely to be full of bugs and hard to maintain. The most fundamental construct for keeping complexity under control is the function. It is a unit of code that takes well-defined inputs, performs well-defined operations, and yields well-defined outputs. It can be tested. It can be moved around. It can be used in multiple parts of a program, and often in many different programs.

Much of what we may think of as part of a given computer language is really a collection of functions. In Python there are built-in functions, such as list() which creates a list; there are functions in the huge standard library that comes with Python; there are functions in external libraries such as numpy and scipy; and there are one’s own functions.

Programming is largely a matter of writing one’s own functions, using the basic constructs of the language together with functions from all of the possible sources. The following notebook illustrates how to write your own functions. (Writing classes can also be an important part of programming in Python, but it is not very different from writing functions. We will introduce it later.)

## Exceptions and tracebacks¶

If you have been experimenting with Python code, you will almost certainly have encountered Exceptions and tracebacks. Exceptions are part of a program flow control mechanism that is specially designed for handling errors or other “exceptional conditions”, but that can be used more generally. A traceback is the error report from an uncaught exception, showing the chain of function calls leading to the exception.

## Classes¶

Classes are ubiquitous in Python but do not appear explicitly in older languages such as C and Fortran. (A crude implementation was added to Matlab several years ago, but it is rarely used.) Although the full description and discussion of classes in Python can trigger an avalanche of computer science jargon, they are really quite simple, intuitive, and easy to use, whether you realize you are using them or not. Regardless of whether you write your own classes—and as we will show, this can be done in as little as two lines of code—you will find Python easier to read and write once you see some examples of how classes work.