Math 450 Laboratory 1: Introduction to python programming at PSU

Date: 2013-01-27


Goals for this laboratory...

Getting Started...

Check to see if your Mac workstation is turned on. If it is not on, please turn it on.

Login with your PSU id and password.

The first thing we need to do is get out of this gui to a terminal that shows a command line. Open a Finder window and search for an application called "Terminal". Drag this to the Dock so it will be convenient.

Run "Terminal". This will open up a "terminal window", and the program running inside the window is called a "shell". "Shell" is the generic term used in the biological sense - a simple and tough thing surrounding the important stuff. ~~For example, your body is a shell for your mind.~~

For more information see

* [http://en.wikipedia.org/wiki/Shell_%28computing%29]
* [http://en.wikipedia.org/wiki/Unix_shell]

The command line

Welcome to the command line. In the terminal window, there should be a line of text something like "$" This is called a command prompt. It doesn't really matter what it says. The important thing is that there is a cursor next to it. If you start typing, that's where things will appear.

The command-line is the oldest computer interface in use, and is incredibly powerful (before 2001, macs did not have a command-line interface), but also a little scary because of it's arcane and obscure syntax. Fortunately, only a few commands are important for us right now.

For more information about common commands, see here and here.

Python

At the shell prompt in the terminal, type

cd Desktop
python

This tells the shell to change the current working directory to the Desktop and to run the Python interpretter.

Python is a 5th-generation computer language that was created in the late 80's by Guido van Rossum, and becoming popular around 2001. It is easier to work with than many other languages, but also more flexible, having a variety of linguistic constructs not available in other languages. It gained popularity after the turn of the century as a free and flexible tool for general purpose scientific computing.

Python's interpretter

Like any command-line shell, python's interpretter evaluates instructions you enter at the >>> prompt. In this case, the instructions expressed in the python language. Type the following commands into the python prompt and observe the results.

First, python does simple arithmetic like a calculator...

>>> 2+3
>>> 2*3
>>> 2**3
>>> 7^2

Note that ** is used for exponentiation in python, while ^ is used for bit-wise negation (a boolean operation -- if you don't know what it is, do not worry, you do not need to know).

Python uses variables to store things in memory.

>>> x = 13
>>> y = x - 5
>>> print y

Here, we have also introduced the command print, which is one of about 30 reserved words that makes up the python language. Really, it's true, the whole language has only 30 words! Here is a slightly more complicated example of how pring can be used with strings and inputs.

>>> print "Hello, World"
>>> name = raw_input("What is your name? ")
>>> print "Hello, %s.  Nice to meet you."%name

In the two cases above, x, y, and name are "variables". Variables in pythong are "dynamically typed", which means they can store many different things, and you just have to remember what kinds of things those are. Python has a few basic variable types built in, and a simple class system for creating more of your own.

Getting help

One of the big improvements of python over preceeding languages was the use of in-line documentation of code. This can be accessed in two easy ways.

  1. From the standard terminal shell, documentation can be found using the command pydoc.

    $ pydoc topics list
    $ pydoc keywords if
    $ pydoc time
    
  2. Inside the python interpretter, documentation on functions and variable types can be found using the function help().

    >>> help(range)
    >>> x = 3
    >>> help(x)
    

These commands are your friends. Use them early and often.

Writting your first script

Because it is tedious to write out lengthy instructions every time you want to program a task, we usually save our commands to a file called a "script". Below is a simple program that prints a table for converting between Celcius and Farenheit.

  1. In a new terminal window, change the working directory to "Desktop" with cd Desktop.
  2. Create a new text file with touch c2f.py.
  3. Find the new file on the Desktop, and double-click it. This should bring up the Xcode editor, where you can type in code. WARNING: The white-space at the beginning of lines of python code is ESSENTIAL! -- If you don't include the correct number of tabs or spaces (choose 1), the script will not work.

  4. Save the script.

c2f.py


print '------------------'
C = -20
dC = 5
while C <= 40:
    F = (9.0/5)*C + 32
    print C, F
    C = C + dC
print '------------------'

Running your first script

To run your new script, go back to the terminal window where you previously did touch c2f.py, and type python c2f.py. This invokes the python interpretter and tells it to read commands from the file c2f.py. What output do you get?

Writting your first function

In most modern computer languages, functions are used regularly. A function in python is a piece of code that takes 1 or more arguments, and returns 1 or more results. Functions begin with the keyword def and finish with the keyword return followed by whichever variables you want to return. Try the following script.

c2f_v2.py


def calculation(C):
    F = (9.0/5)*C + 32
    return C,F

print '------------------'
C = -20
dC = 5
while C <= 40:
    print calculation(C)
    C = C + dC
print '------------------'

Turtle graphics

Python has things called modules which provide new functions that can be used in programs. One module called turtle provides simple functions for simulating turtle-like robots. Try this...

pretty_picture.py


from turtle import *
# comments begin with a pound-sign, #
bgcolor('black')
pencolor('red')
for i in range(40):
    circle(50);
    rt(15);
raw_input("Can I quit now? ")

SAVE YOUR WORK TO YOUR PASS DIRECTORY!

On the Desktop, grab your new scripts and drag them to your PASS directory. If you do not save your scripts like this, they may be lost and gone forever after you log out.

Assignment

Change pretty_picture.py so it draws 4 overlaid yellow cirles on blue. Your picture should be symmetric over horizonal and vertical axes.