# Difference between revisions of "Python examples"

Line 144: | Line 144: | ||

Hello World! | 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. | ||

+ | |||

+ | #!/usr/bin/python | ||

+ | |||

+ | 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? >> ') | ||

+ | a = float(angle) | ||

+ | arad = math.radians(a) | ||

+ | sine = math.sin(arad) | ||

+ | cosine = math.cos(arad) | ||

+ | tangent = math.tan(arad) | ||

+ | print '\n' | ||

+ | print 'Radians: ', arad | ||

+ | print 'Sine: ', sine | ||

+ | print 'Cosine: ', cosine | ||

+ | print 'Tangent: ', tangent | ||

+ | print '\n' | ||

+ | exit() |

## Revision as of 19:31, 12 February 2013

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

## Contents

## 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.

Type

>>>x=1

and then simply

>>>x

and you'll see

1

Type

>>>x=1.01

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

>>>x 1.01

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

>>>x=1.01 >>>y=1.0001 x/y

and you'll see something like this

1.0098990100989902

Modify that with

>>>z=x/y >>>z

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

>>>int(z)

and you'll see

1

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

>>>a=int(z) >>>a 1

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 >>>a 1.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

>>>math.pi

and you'll see

3.141592653589793

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

>>>math.sin(math.pi/4.)

returns

0.7071067811865475

as does

>>>math.sqrt(0.5)

Similarly

>>>4.*math.asin(math.sqrt(0.5))

returns

3.1415926535897936

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

exit()

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 test.py . 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 test.py" and give you the result of the program in the file. Alternatively, in Linux or MacOS, if the first line of the file is

#!/usr/bin/python

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:

#!/usr/bin/python print "Hello World!\n" exit()

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 test.py

returns

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

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

./test.py

which means "run the program in this directory called test.py". 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

python test.py

which will take "test.py" 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.

- !/usr/bin/python

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? >> ') a = float(angle) arad = math.radians(a) sine = math.sin(arad) cosine = math.cos(arad) tangent = math.tan(arad) print '\n' print 'Radians: ', arad print 'Sine: ', sine print 'Cosine: ', cosine print 'Tangent: ', tangent print '\n' exit()