In this section of our short course on Python for Physics and Astronomy we take a short path to using Python easily.
Python is open source software availble for free from www.python.org. Version 2.7 is the current mature version that is widely supportly by other add-on modules, and is the one for which examples here are written. Python 3 is still under development and it currently lacks packages that are necessary for astronomy.
If you have Linux, then mostly likely you already have Python and you skip down to try use it.
For other operating systems, consider using the academic version from Enthought Solutions if you meet their licensing terms for the free version. This version supplies Matplotlib, Numpy and Scipy which we will use. For more information about other options, see Python4Astronomers
Alternatively, the source of Python development has versions for MacOS and Windows of the base system that avoids the commercial (though free) route of other vendors:
Most users would probably prefer running Python through the IDLE IDE, the development interface that comes with many installations. (It's named after Eric Idle, who does the "Galaxy Song" in Monty Python.) On the command line you would simple run "idle" to get started. The software includes an editor and also a runtime environment.
Additional modules would have to be installed separately later.
The first step is to figure out how to start up Python on your computer after it is installed. In Linux you open a console and type "python" on the command line. You'll immediately see a prompt that looks like ">>" after which you can type Python code and see the results.
If you installed the Enthought distribution of Python on Windows or Mac, take a look at their release notes and website for additional advice on getting started.
If you installed from the python.org, then they have some additional pages to offer help. On Windows, its not necessarily as straightforward as Linux, but it can be. It will help to read this "frequently asked question" (FAQ) page about Python on Windows to help you at first, and also consult setup and usage guide. On a Macintosh OS X system using Python is very similar to other Unix platforms like Linux or BSD. There are some helpful notes at the Using Python on a Macintosh website.
Once you have a command line prompt you have access to all of Python's capabilities. We'll show you some simple examples here to test your installation and give you a quick sense of how to use it.
To exit Python in the interactive mode, use "Ctrl+d" or "exit()" from the command line, or the Exit menu entry if you are running IDLE.
You will usually edit a file that contains your Python program and then run that program by calling the Python interpreter. Therefore, the first thing is to pick an appropriate editor. One way is to use IDLE, which makes it especially easy on Windows systems and others to edit and test with a consistent interface. On Linux systems where the command line is more commonly used, an alternative is a standard graphical editor that is aware of Python syntax like gedit. Other alternatives are nedit and emacs. Python text files have a required format, and it generally not a good idea to embed tabs in the text so the tab function has to be set for spaces instead. On MacOS your could try emacs, BBEdit, or another one of your preferred editors. The Python website maintains a list of editors and their features for different operating systems with links to the editor websites if you need to download one.
For example, if your program is in the file "myprogram.py" you can run it from the command line with "python myprogram.py". On Windows systems, the file extension ".py" may be associated with this command, and in that case you can start a program by clicking on the icon or name in a window. On MacOS and Linux, you would first make the file executable with a command such as
chmod a+x myfile.py
and also see that the first line of the file is exactly
assuming that python is installed in /usr/bin/. With those changes, any file of Python code becomes an executable program. Simply type
Note that programs that interact with the window manager may need to be started with pythonw instead of python. For MacOS, see 4.1.1 How to run a Python script.
For examples of Python illustrating how to use it interactively and to write very simple programs, see the section Python examples.
For the assigned homework to use very simple Python interactively and as a script, see the section Python assignments.