Python examples

From AstroEd

Revision as of 10:13, 14 February 2013 by WikiSysop (talk | contribs) (Solving problems with Python)

This page contains examples and links to programs used for our Research Methods - Programming with Python short course.

Very simple Python

Start a Python interactive session using the "python" command to get a >>> prompt.

Command line use

There are many built-in features of Python that are described in the documentation, but to get started let's do something very easy.



and then simply


and you'll see




and then after you type "x" you'll see


Clearly you have a real-time calculator in hand, so try something more exciting.


and you'll see something like this


Modify that with


and you'll see the same result. But now try


and you'll see


That is, the function int() took the integer part of z. You can put that in another variable such as


Curiously, a seems to be an integer. It is said to be dymanically typed in this assignment. That can change. If you now add a little bit to a you'll see it turns into a floating point number

>>>a = a + 0.001

Importing math

There's much more you can do, of course, but you need to import the math functions first. Here's one way to do that.

>>>import math

Now try


and you'll see


The functions in the math package need the "math." in front of them.




as does






The comprehensive list of math functions is on the Python documentation site. You can try out some of the more exotic possibilities on your own.

Writing an executable program

Let's turn off the real-time version of Python if you have it running by typing


or simply the combination of "ctrl" and "d"keys "ctrl+d" to return to the system prompt.

Select your favorite Python editor, and create a file we'll call . If you have the Python interpreter associated with a .py extension, then when you click on a this file in a graphical user interface (regardless of the operating system) it will run "python" and give you the result of the program in the file. Alternatively, in Linux or MacOS, if the first line of the file is


this will tell the user interface to run python followed by the code that is in the file (assuming that the executable "python" program is in /usr/bin. Here is how you would write a "Hello World" program in python:

print "Hello World!\n"

Hint here: the "\n" is a line feed that will create a blank line after printing the text.

Now to run this program in MacOS or Linux you would make sure it's executable

ls -l


-rwxr-xr-x 1 john users 48 Feb  7 02:16

which tells me that I own the program and that I can execute it (The letters in groups in the listing of the program give the permissions, and the "x" means execute, "r" means read, and "w" means write. The first three are for the user who owns the file, the second three are for the group that use belongs to, and the last three are for everybody else.) With that, I can simply type


which means "run the program in this directory called". The prefatory "./" is not needed if the PATH includes the current directory. Obviously there are operating system nuances here that have nothing in particular to do with Python, but have to be mastered to use the computer to its fullest. Alternatively on any system you can explictly ask for the python program


which will take "" as input and run it. In either case, you will see

Hello World!

Elements of Python programming

This is a an example of a Python program that asks for a value, calculates a result, and displays it for the user. The source code is available here in the file trig.example . Down load by right clicking and saving the file locally with the extension .py rather than .example.

import math
# Print something for the user
print 'I will find trigometric functions for an angle.\n'

# Ask for a value for the angle
angle = raw_input('What is the angle in degrees? >> ')
# Convert the angle from degrees to radians and to the math
a = float(angle)
arad = math.radians(a)
sine = math.sin(arad)
cosine = math.cos(arad)
tangent = math.tan(arad)
# Output the results
print '\n'
print 'Radians: ', arad
print 'Sine: ', sine
print 'Cosine: ', cosine
print 'Tangent: ', tangent
print '\n'
# Exit nicely

Solving problems with Python

Suppose that we have a catalog of data containing information about astronomical objects, and we want to find the data in the catalog that are specifically for one entry, given my name. A typical use would be on the command line when that's possible, or by reading the name from another file. For this purpose we'll take the catalog called ngc2000.dat which contains all NGC and IC objects, read it, and find the one item we want.

A program illustrating the ideas we have discussed so far is available here. Look for ngc_reader.example and download it with a right click. The data file is ngc2000.dat .

The reader will look for this catalog in the same directory it is executing, and will expect an NGC or IC number on the command line. If your operating system does not handle command line input, try it with the number assigned as text within the program body, and delete the code that reads the command line.