Overview
After you have done your Python installation (most likely by installing the Anaconda or Canopy Express distribution as described in the Courseware or Quick Start pages), you should start up the Python interpreter and start working through a Python language tutorial. There are a number of different ways for the user to interact with Python, and the experienced user winds up using all of them at one time or another. All these methods feed Python instructions to the same underlying Python interpreter, but give the user different ways of interacting with the interpreter.
- You can start Python from the command line and just begin typing commands:
rtp1$ python
Enthought Canopy Python 2.7.6 | 64-bit | (default, Jun 4 2014, 16:42:26)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1.
>>> y=2.
>>> x+y
3.0
>>> - You can do the same, but with the
ipython
command line tool instead. Theipython
command line tool offers a lot of useful features that the plain vanillapython
command processor doesn't, such as automatic indentation and ability to access previous commands and results by name. You can read about the features of ipython here . This is how our previous example would work if run throughipython
:rtp1$ ipython
Python 2.7.6 | 64-bit | (default, Jun 4 2014, 16:42:26)
Type "copyright", "credits" or "license" for more information.IPython 2.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.In [1]: x = 1.
In [2]: y = 2.
In [3]: x+y
Out[3]: 3.0In [4]:
- You can write a file of commands in the text editor of your choice and then execute it from the command line. Suppose you create a file
slask.py
with the following contents:x = 1.
y= 2.
print 'The sum is ',x+ythen you can execute it from the command line as follows:
rtp1$ python slask.py
The sum is 3.0
rtp1$You could also run this using
ipython
instead ofpython
. Note that the default is to run your script and then exit back to the command prompt. If you wanted to stay in the interpreter after running, so you could enter more commands, you would instead usepython -i slask.py
, and similarly foripython
.
- You can use the ipython notebook feature to execute commands in ipython through a web browser. You start up a notebook server by typing
ipython notebook
at a command line on your computer. This will bring up a window in your web browser called the iPython Notebook Dashboard, from which you will be able to create and save a new notebook. A notebook consists of "cells" of editable text that can do many, many things, one of which is to execute Python instructions inipython
. Here is an example of an ipython notebook being used to run our standard example, plus a bit more. (Click on the image to see a larger version). The window shown is a standard Firefox browser window.
I will be writing a lot more about notebooks as I transition my instructional material to make use of this very powerful new feature. My quick-start notebook tutorial is here, and provides all you need to get started using notebooks. You can read about notebooks and how to use them in more detail here . If the instructor has set up a notebook server, then instead of starting up the notebook on your own computer using the
ipython notebook
command, you simply point your browser to the URL provided by your instructor, and you will be presented with a login dialogue, whereafter the Dashboard will appear directly in your web browser and you proceed exactly as if the notebook were running on your own computer.
- (Needed only for development of complex scripts) Via an Integrated Development Environment such as the Canopy application, which can be started up either by double-clicking its icon or by typing the
canopy
command at a command prompt. Canopy provides a Python-aware editor, anipython
interpreter window into which you can type commands directly, and also the ability to execute code in an open Editor window in the current interpreter session. An introduction to the use of the Canopy application can be found here. There was an older (open-source) integrated development environment known as IDLE, which was used by earlier versions of the Enthought distribution, but while IDLE is still available in some Python installations further development seems to have ceased and it is no longer well supported.
From an integrated development environment, you can open an existing file in the editor, edit it if necessary, and run it in the interpreter window, create a new file in the editor and then save it and run it, or enter commands directly into the interpeter window. In this Canopy window, we have opened
slask.py
in the editor, and executed it using the run command from Canopy's run menu (or equivalently by clicking the green right-arrow in the toolbar), which generates the %run command in In[2]. The upper right pane is the ipython interpreter window, and the lower right pane is the editor window. Commands can also be entered directly into the ipython interpreter window.
Resources for learning Python
- My introduction to Python, with an emphasis on techniques of interest to the mathematician or computational scientist, can be found here. I am currently writing a textbook on Python for mathematicians and physical scientists, and this is the first chapter. It isn't exactly "Python in an Hour," but most readers should be able to work through it in a week or less. Comments and suggestions are welcome.
- My older quick-start introduction to Python for physical scientists (updated 9/30/2010) is here, and has been retained for archival purposes though much of it is outdated. This introduction is somewhat outdated, but some parts may still be of interest.
- A comprehensive online reference to setting up and using Python, as well as documentation of the Python language (including many useful tutorials) can be found here. The Tutorial is especially recommended for beginners.
- Enthought offers extensive Python Training on Demand for a fee, and is currently offering free training to academics.
- Coursera offers several courses on Python, including Programming for Everybody (Python) and Learn to Program. Additional Python courses at all levels appear frequently, so browse the offerings to find one you like.
- If you'd like to buy a physical book or ebook to help you with Python, the following are worth a look:
- Magnus Lee Hetland Practical Python. Apress. (Currently being reprinted; available soon)
- Kinder and Nelson, A Student's Guide to Python for Physical Modeling . Princeton University Press
- Hans Petter Langtangen Python Scripting for Physical Science. Springer. (Recommended especially for Python programmers seeking a master of more advanced topics. This book has good coverage of techniques for using compiled c, c++ or Fortran routines for extending Python. )
- Dive into Python I'm not certain of the commercial status of this book. It seems to be available for reading online, but if it is available for purchase, the author deserves the encouragement and the royalties!