Computational Methods for Complex Systems

Physics 7682


Instructor: Chris Myers

http://www.physics.cornell.edu/~myers/teaching/ComputationalMethods/

Getting Started With Python

Table of Contents

  1. Python installations: in the computer lab or on your laptop
  2. Development Environments, Workflows and Interpreters
  3. Suggested Workflow for the Course
  4. Some Alternative Workflows for the Course
  5. Additional information on IPython
NOTE: There is a lot of detail here. If something is confusing or you don't know how to proceed, ask for help. Getting the basics down on how to work with files, software and course exercises early will save you trouble later on.


1. Python Installation

Python is a programming language, augmented by a rich set of third-party libraries that build upon the core functionality defined in the language. Since we will be using a number of such libraries, it is helpful to leverage existing Python package bundles tailored specifically for scientific computing. For this course, we will be using the Anaconda Scientific Python distribution, provided by Continuum Analytics. Different versions of this distribution are available; we will be using version 3.4 (latest minor version 3.4.3). If you choose to use your own laptop for the course, you should download Anaconda Python version 3.4 by following the Download links at the Anaconda Python site. Make sure you get select version 3.4, since the version 2.7 installer is brought up by default. If you already have a Python installation on your own machine that you actively maintain (e.g., using a package management system), you might need to install some additional packages, such as SciPy, matplotlib, and IPython.

2. Development Environments, Workflows and Interpreters

Python is an interpreted programming language. Therefore, two central elements of any system for programming in Python will involve support for writing and editing Python code and for running that code in an interpreter. (And once code is developed and tested, integrating the results of simulations and analysis is an important part of the research process.) There are three major types of systems / workflows that support such functionality: (1) Editor + Interpreter/Notebook, (2) Integrate Development Environment (IDE), and (3) Notebook Only. Each such system has its advantages and disadvantages, and it is somewhat a matter of personal taste - as well as the specifics of each use case - as to what each person prefers. In this course, you will be asked to use an IPython notebook to summarize the results of your computations, but you can explore different development environments to see which you prefer. The IPython notebook itself is an interpreter, so a suggested workflow is to write code in an editor, run that code within an IPython notebook, and use the notebook for testing, debugging and analyzing the results of simulations. The basic process of working with course modules will be the same regardless of which environment you choose: you will download a Python Hints file, make a copy of it, and then populate the code to implement the desired functionality, using the exercise instructions and code comments as your guide.

3. Suggested Workflow for the Course: Editor + Notebook

Editors are good for writing and editing code, and the IPython notebook is a useful and productive environment for both testing code that you are developing, as well as presenting and documenting the outputs of that code.

The general process by which you will work on course exercises is as follows:

4. Alternative Workflows for the Course

For all of these other workflows, follow the basic outline described above: downloading hints files, filling in skeletal code fragments, and then testing and producing output within an interpreter.

IDEs: Integrated Development Environments (IDEs) are programs that aim to integrate different aspects of programming and running code. There are many such environments, some targeted to specific programming languages and others that support a broader sweep of languages. Included in the Anaconda Scientific Python distribution that we are using for this course is Spyder, an IDE that supports Python code development. The Spyder IDE brings together several tools in one integrated environment, most importantly a code editor, IPython interpreter, and variable inspector. Like the IPython notebook, the interpreter window in Spyder enables the incorporation of plots inline with code, and interpreter sessions can be saved to HTML pages. These sessions do not appear to allow the sort of rich text markup that the notebook does, but they will suffice for integrating project results. In addition, the terminal-based IPython interpreter in Spyder does provide much better command-line recall than does the notebook (e.g., by scrolling back and editing previous commands), and there are benefits to the integration of a number of different tools.

Editor + IPython terminal/console: Instead of using an IPython notebook, it is sometimes preferable to simply use IPython in a terminal or a qtconsole. This environment is, for some tasks, easier to navigate, providing better command-line recall than does the notebook (e.g., by scrolling back and editing previous commands), and less pointing-and-clicking.

IPython notebook only: Code writing and editing can be done completely within a notebook, although the editing facilities are not extensive, and it is difficult to develop large, multi-file codes within this environment. The benefits of the notebook interface described above, however, make this quite useful for certain applications.

5. Additional information on IPython

The core Python interpreter that comes with every installation is called python. It is useful for running standalone programs on the command line (e.g., python my_program.py will run my_program.py until completion), but is not as useful for interactive work. IPython is an enhanced interpreter built on top of the core python interpreter that facilitates interactive programming, development and data exploration. If you have IPython installed and in your shell path, you can start it up via:

IPython provides support for inspecting objects and getting help within the interpreter. For example,

In addition, IPython defines a powerful set of "magic" functions that augment the core Python functionality. These magic functions are all preceded by a percent sign (%) to distinguish them from core Python functions, e.g.,

General IPython documentation can be found here, and specific information on the magic functions is included in the chapter on Built-in magic commands. See below for further information on the use of the IPython interpreter.