Smith College Computer Science 111, Fall 2006
Lab #1    September 7, 2006
(Keep this handout for future reference in lab).
Read Carefully (always).


The purpose of today's lab is to introduce you to both the Windows and the Linux Operating Systems, to beowulf (the Linux machine we will be running PYTHON programs on), to emacs (a text editor) and to Mozilla Firefox (web browser).

In next week's lab you will work with a partner. For this week, each of you will work at your own machine, to get used to them.

Log on to the Smith network.
Sit down at a PC. Use the top button on the system unit to turn on the PC (if it isn't already on). A login window should pop up. Log in using your Smith Novell account. Close the Login Results window. Then log in to the Windows workstation using username "student" and no password. If you don't get a login window, then shut down the computer (described at the end of today's lab) and start over again.

Open an SSH window to Connect to the Linux machine called beowulf.
In Windows, click on the SSH (Secure Shell) Client icon. In the SSH window, hit the enter key. In the pop-up window that appears, type beowulf.csc for the host name, and, for the username, the beowulf computer account name that you receive in lab today (111a-xx). Click on the Connect button and then in the password window type in the password that you received with the account name. You will see some messages and finally you will see beowulf's prompt -- a short line of text ending with $.
You have now logged into the Computer Science machine called beowulf, and can give it Linux Operating System commands.

Run the Python Interpreter
We will follow the examples given in the book pages 9-18.
To start the Python intepreter, just type in the command python and hit Enter
You should obtain something like this:

Python 2.2.2 (#1, Feb 24 2003, 19:13:11) 
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
where >>> is the Python interpreter's prompt. The prompt is prompting you for a Python command, or statement, as it is called in programming languages. Try this:
>>> print "Hello, World!"
and also this:
>>> print 5+7
and finally this:
>>> print 16+32, "is the sum of 16 and 32"

Create Your Own Python Command
You can make your own command, called a function, using a simple definition format. Try this, using the Tab key to make the indentation (this is important), and hitting Enter twice after the last statement in your function definition:

>>> def smith():
...     print "Smith College"
...     print "Northampton, MA"

>>>
Now try typing
>>> smith
What happens? Next try
>>> smith()

Create Your Own Python Multi-purpose Command, using parameters
We can make the function do different things by giving it different information when we use it. Each piece of information is called a parameter. We have to indicate that we want to do this when we define the function. We just do this by placing the parameter name between the parentheses when we define the function, and then by using that name in the part of the function that will change each time. For example:

>>> def election(winner):
...     print winner, " will win the 2004 election "

>>> election("Judy")
Judy  will win the 2004 election
>>> >>> election("Gloria Steinem")
Gloria Steinem  will win the 2004 election
>>> 
Try typing
>>> election(Judy)
What happens? What's different?

Exit from the Python Intepreter
Exit from the Python interpreter by typing Ctrl-d. This means hold down the Ctrl key and while holding it, press the d key.

Bad news. When we exit from the Python Interpreter, we lose our definitions. What to do?
To save our function definitions, we can use a text editor and create a module file that contains all the function definitions we want to use. Then we save this file and import it into the Python interpreter.
Do not use Word for your editor. It puts hidden formatting characters into your file that will affect the interpreter.
Instead, use a Linux editor called emacs.

Create a file with the text editor emacs.
You are now ready to create a file called hobbies.py with emacs, the editor we will be using in this course. At the beowulf prompt, type emacs hobbies.py When emacs pops up, enter (type in, do not cut and paste) the following Python program, exactly as it appears here (don't worry if you don't understand the program -- you'll be learning about Python programs in class):

Be sure to press the Enter key at the end of each line. The \ character appearing at the end of a line is a warning that you did not press the Enter key..) Also, make sure to use the Tab for indentation in the definition of the main() function.

# File:    hobbies.py
# Description:  This program asks the user her name and
#            favorite hobby, and how much the hobby costs per month.
#            It then estimates how much this will cost her per year.
#            As a friendly touch, the program asks for
#            the user's name and then uses her name in the output.
# Account: 111a
# Author:  Judy Franklin
# Date:    September 1, 2006


def main():
    this_year = 2006
    final_year = 2010
    months_in_year = 12
              # print welcome and ask questions
    print "Welcome to the Smith College hobby  cost  program!"
              # use raw_input for strings
    first = raw_input("What is your first name? ")
    last = raw_input("What is your last name? ")
              # use input for numbers
    hobby = raw_input("What is your favorite hobby? ")
    month_cost = input("How much does it cost per month (e.g. 25.00) ")
              # Now Compute number of the total cost
    total_cost = (final_year - this_year) * months_in_year * month_cost
              # Print the result and say goodbye 
    print "Well, ", first, last, " this costs you ", total_cost, " dollars over four years!"
    print "Goodbye, and thank you for using this program."

When you are done, you may leave emacs by entering ctrl-x ctrl-c. Emacs will ask you whether you want to save your file. ENTER yfor yes!

If you need to make changes while using emacs, here are some special keys that you can use. You should also consult the emacs handout that is available in today's lab.

Cursor keys (arrows) : to move around in the file
Delete key : delete the character to the left of the cursor
ctrl-a (i.e., simultaneously press the control ("ctrl") and "a" keys) : move cursor to beginning of line
ctrl-e : move cursor to end of line
ctrl-k : delete line
ctrl-x [ (i.e., simultaneously press the control and x keys and then press the "[" key) : move cursor to beginning of file
ctrl-x ] : move cursor to end of file
ctrl-v : scroll down one screenful
esc v : scroll up one screenful
ctrl-x ctrl-c : exit emacs
ctrl-x ctrl-s : save current file without exiting emacs

If you want to go to a particular line number, type esc x, and then goto-line. You will be asked for the line number.

After leaving emacs, if you want to erase the emacs stuff that still appears on the screen, type clear after beowulf's $ prompt.
Now that you are back at the Linux command prompt, out of emacs, try the command ls, which means: list the files in my account. You should see a file called hobbies.py.
Import and run the hobbies program. Run the Python interpreter again, by typing python after beowulf's Linux $ prompt.
When you see the Python Interpreter's prompt (>>>) type

>>> import hobbies

If you get ERROR messages, then you made a mistake when you typed the file. In this case, go back to emacs and compare the file with what I handed out, and correct your typos.

To run the program, type hobbies.main() at the Python prompt. As the program runs, answer the questions on the screen. When the program is done running, you will see the Python prompt (>>>) on the screen once again.

Exit Python and print your file Type Ctrl-d to exit the Python Interpreter. then type cprint movies.py
When it asks, choose the printer in Engineering 203, and go obtain your printout. It is very useful sometimes to print your file and look at it on paper, away from the computer.

Change your password. You should change your password soon. However, if everyone does it at once, it's easy to overwhelm the password system. Try to change it now, and if it is unresponsive, try agan later. But be sure to do it.
Enter the command passwd and, when asked, type in the password that was given to you. You will then be asked for a new password. Choose one carefully -- you will be required to have a password that uses both upper and lower case, and also some digits or maybe punctuation. Also, be sure to write down this password! When you are asked to verify, type in the new password a second time. If all went successfully, beowulf's prompt will reappear. Remember to use this new password the next time you log in! Remember, your instructor does not have access to your password, so you must remember it.

Close the beowulf session.
You should now log off of beowulf: type logout at beowulf's prompt. Then close the SSH windows by either pressing the X in the upper righthand corner of the window or choosing Exit from the File menu.

Use Mozilla Firefox to find the web pages for this course.
You will now access the World Wide Web with the Firefox Navigator web browser. You may double-click the Firefox icon on the desktop, or alternatively, click on the Start button, and select Programs/Internet Tools/Mozilla Firefox .
After Firefox pops up, the Clark Science Center home page will appear on your screen. In the "Location" box near the top of the screen, you will see the URL for this home page: http://www.science.smith.edu.

Use the scroll bar to move around on the page. Underscored text indicates a link, which you may use to move to other information. Find the link for the Computer Science Department page, and click on it. From here, follow the link for Faculty, then the link for Judy Franklin. Click on More info and you'll get to my web page. At the top right are the courses I am teaching. Click on CSC111. This is the syllabus that was handed out in class, except that in the schedule I have added links for Lab1 and Hw1. Click on the link for Lab 1, and you'll see this very lab handout! Return to the Syllabus page by pressing the Back button at the top of the screen. Click on the link for Homework 1, and you'll see the homework we distributed today. In the future, if you need a lab or homework assignment, you can use these links to get one.
You can also go to the web site directly by typing in http://www.cs.smith.edu/~jfrankli/111f06 Although you don't need to do this right now, you can print any Web page by clicking on the Print button or by pulling down the File menu at the top left corner of the screen and choosing Print. Printing will be directed to the printer in your lab.

Exit Mozilla Firefox by pressing the X button or using the File menu.
Log off of the network server. Press the Start button, choose Shut Down, then select Close all programs and log on as a different user to set it up for the next person.
You are now ready to do Homework #1.