Goals for this laboratory...
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]
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.
pwd: Displays the path to the directory you're currently in.
Immediately after login this will be your home directory.
ls: list the contents of the current directory. Use
ls -l (
long) to get lots more detail.
cd: Changes the current directory. You can either give it:
..* a relative path:
..* an absolute path:
..* or use it on it's own to jump straight to your home
man: used to find the manual pages about a command, like
man is itself confusing.
man man will tell you more about man
For more information about common commands, see here and here.
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.
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
** 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 "Hello, World" >>> name = raw_input("What is your name? ") >>> print "Hello, %s. Nice to meet you."%name
In the two cases above,
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.
Python has lists for sequences of objects
>>> a_list = range(7) >>> a_list >>> a_list >>> a_list >>> a_list[-1] >>> a_list[1:3]
Python has dictionaries (called
dict's) for paing items.
>>> a_dict = dict() >>> a_dict['red'] = 'apple' >>> a_dict['blue'] = 'sky' >>> a_dict['red']
Integers are different from floating-point numbers!
>>> u = 3 >>> v = 3. >>> type(u) >>> type(v) >>> u/2 >>> v/2
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.
From the standard terminal shell, documentation can be found using the command
$ pydoc topics list $ pydoc keywords if $ pydoc time
Inside the python interpretter, documentation on functions and variable types can be found using the function
>>> help(range) >>> x = 3 >>> help(x)
These commands are your friends. Use them early and often.
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.
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.
Save the script.
print '------------------' C = -20 dC = 5 while C <= 40: F = (9.0/5)*C + 32 print C, F C = C + dC print '------------------'
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?
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.
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 '------------------'
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...
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? ")
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.
Change pretty_picture.py so it draws 4 overlaid yellow cirles on blue. Your picture should be symmetric over horizonal and vertical axes.