Difference between revisions of "CSC111 Lab 1"

From dftwiki3
Jump to: navigation, search
(A Third Program)
 
(19 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
<bluebox>
 
<bluebox>
Feel free to work in pairs, share insights and help your neighbors during the labs. While some homework assignments will have to be done as individual work, the labs are a good time to cooperate with neighbors or work in pairs.
+
Feel free to work in pairs, share insights and help your neighbors during the lab.  This is true of all labs. While some homework assignments will have to be done as individual work, the labs are a good time to cooperate with neighbors or work in pairs.
 
</bluebox>
 
</bluebox>
  
Line 20: Line 20:
 
If everything went well, you should see this:
 
If everything went well, you should see this:
  
SSH Secure Shell 3.1.0 (Build 235)
+
SSH Secure Shell 3.1.0 (Build 235)
Copyright (c) 2000-2001 SSH Communications Security Corp - http://www.ssh.com/
+
Copyright (c) 2000-2001 SSH Communications Security Corp - http://www.ssh.com/
 +
 +
This copy of SSH Secure Shell is a non-commercial version.
 +
This version does not include PKI and PKCS #11 functionality.
 +
 +
[111c-xx@beowulf ~]$
  
This copy of SSH Secure Shell is a non-commercial version.
+
Keep your account and password information in a safe place, or learn both by heart! You will need them every time you need to use your account and work on your homework assignment or lab.
This version does not include PKI and PKCS #11 functionality.
 
  
 +
You may want to log out now, to practice this login procedure a couple times. If you are working with a lab partner, it is a good time to log out and let her/him try the procedure.
  
[111c-xx@beowulf ~]$
+
To logout of the session with beowulf, simply type '''exit''' at the Linux prompt:
Keep your account and password information in a safe place, or learn both by heart! You will need them every time you need to use your account and work on your homework assignment or lab.
 
  
You may want to log out now, to practice this login procedure a couple times. If you are working with a lab partner, it is a good time to log out and let her/him try the procedure.
+
  [111c-xx@beowulf ~]$  '''exit'''
  
Call on the TA or your instructor if you need help.
+
Don't hesitate to call on the TA or your instructor if you need help.
  
 
==Playing with the Emacs Editor==
 
==Playing with the Emacs Editor==
  
First execute this command from the Linux prompt:
+
In order for you not to suffer from a misbehaving backspace/delete key as happened in class yesterday, you should execute the following command (your input is in bold face)
  
        getcopy .emacs
+
  [111c-xx@beowulf ~]$  '''getcopy .emacs'''
 +
  cp: overwrite `./.emacs'? '''y'''
  
 
(That is "getcopy" space period "emacs")
 
(That is "getcopy" space period "emacs")
It's a configuration file that will allow emacs to understand the backspace key correctly. You only need to do this once and you will be set for the semester.
+
It's a configuration file that will allow emacs to understand the backspace key correctly. ''You only need to do this once and you will be set for the semester.''
  
 
You will now get a copy of a file from your instructor's account, and edit it. The file is called Fulghum and is a short story from [http://www.robertfulghum.com/ Robert Fulghum's] book [http://en.wikipedia.org/wiki/All_I_Really_Need_to_Know_I_Learned_in_Kindergarten ''All I really need to know I learned in Kindergarten''].
 
You will now get a copy of a file from your instructor's account, and edit it. The file is called Fulghum and is a short story from [http://www.robertfulghum.com/ Robert Fulghum's] book [http://en.wikipedia.org/wiki/All_I_Really_Need_to_Know_I_Learned_in_Kindergarten ''All I really need to know I learned in Kindergarten''].
Line 48: Line 53:
  
  
        getcopy fulghum
+
  [111c-xx@beowulf ~]$  '''getcopy fulghum'''
 +
 
  
 
followed by the return key. Then edit the file with the command
 
followed by the return key. Then edit the file with the command
  
        emacs fulghum
+
 
 +
  [111c-xx@beowulf ~]$  '''emacs fulghum'''
 +
 
  
 
Simply work on the first paragraph of the file, and try to straighten it up. I have indicated below, on the right-hand side, which emacs commands you can use to fix the text on your screen.
 
Simply work on the first paragraph of the file, and try to straighten it up. I have indicated below, on the right-hand side, which emacs commands you can use to fix the text on your screen.
Line 108: Line 116:
  
 
Part I of the homework assignment for this week is to edit the complete story about Larry Walters and his flight over Los Angeles.
 
Part I of the homework assignment for this week is to edit the complete story about Larry Walters and his flight over Los Angeles.
 +
 +
 +
==Paying with Python in Interactive Mode==
 +
 +
* At the Linux prompt, type:
 +
 +
  [111c-xx@beowulf ~]$  '''python'''
 +
 +
followed by the '''Return''' key.  Try some of the examples we did in class yesterday.  See if you can guess how python will interpret the commands before you press the '''Return''' key.
 +
 +
<code><pre>
 +
Python 2.6 (r26:66714, Nov  3 2009, 17:33:38)
 +
[GCC 4.4.1 20090725 (Red Hat 4.4.1-2)] on linux2
 +
Type "help", "copyright", "credits" or "license" for more information.
 +
 +
>>> 3
 +
 
 +
>>> 4
 +
 
 +
>>> 6
 +
 +
>>> 3 + 4
 +
 +
>>> a = 3
 +
 +
>>> b = 4
 +
 +
>>> a
 +
 +
>>> a + b
 +
 +
>>> a * 2 + b
 +
 +
>>> 'first' = 'Carole'
 +
                                                (why does this generate an erro?)
 +
                                     
 +
>>> first = 'Carole'
 +
 +
>>> last = 'Christ'
 +
 +
>>> first
 +
 +
>>> first + last
 +
 +
>>> first * 2
 +
 +
>>> ( first + ' ' ) * 2                  (predict the output before you press Return)
 +
 +
>>> fullname = first + ' ' + last
 +
 +
>>> print fullname
 +
 +
>>> print 'fullname'                  (predict the output before you press Return)
 +
 +
                                              (does the output make sense?)
 +
>>>
 +
 +
</pre></code>
 +
 +
* Type '''Control-D''' to exit the Python interactive shell and to return to the Linux prompt.
 +
  
 
==Creating your first Python program==
 
==Creating your first Python program==
  
You are now ready to create your first Python program. Don't worry if what you are doing does not make sense. We will go over all of these steps in details in class these coming weeks.
+
* You are now ready to create your first Python program. Don't worry if what you are doing does not make sense yet. We will go over all of these steps in details in class these coming weeks.
Make sure you have exited emacs and are back to the Unix prompt. If you are still "inside" emacs, type c-x c-s to exit back to the Unix prompt.
 
  
Open a second SSH window. You will use one to edit your program, the other one to execute your program.
+
* Make sure you have exited emacs and are back to the Unix prompt.
  
 +
* Open a second Secure Shell (ssh) window. You will use one to edit your program, the other one to execute your program.
  
In Window 1, use emacs to create a file that will contain your program:
 
  
 +
* In Window 1, use emacs to create a file that will contain your program:
  
    emacs first.py
 
  
Type in the following lines
+
  [111c-bz@beowulf ~]$ '''emacs temp.py'''
 +
 
 +
* Type in (or copy-paste) the following lines
  
<font size="+1">
 
 
<source lang="python">
 
<source lang="python">
  
# first.py
+
# temp.py
# 111c-xx (change and use your own account)
+
# 111c-xx (change and use your own account)
# firstname lastname
+
# yourfirstname yourlastname
#
+
#
# My first python program                               
+
# My first python program                               
# Adds two numbers together and displays them
+
# Gets a temperature in Farenheit and prints its equivalent
 +
# in Celsius
 +
 +
def main():
 +
      tempF = input( "Enter temperature in Farenheit: " )
 +
      tempC = ( tempF - 32 ) * 5 / 9
 +
      print "The temperature in Celcius is", tempC
  
x = 5
+
main()
y = 6
 
print "x = ", x
 
print "y = ", y
 
print "x+y = ", x+y
 
  
 
</source>
 
</source>
</font>
 
  
Type '''c-x c-s''' (control-x followed by control-s) to save the file. In Window 2, type in the following command at the Unix prompt to run the program:
+
* Be careful that the three lines under '''def main():''' are indented exactly the same way. It's an important aspect of python, that the indentation indicates a form of hierarchy of the program.
  
 +
* Type '''c-x c-s''' (control-x followed by control-s) to save the file. In Window 2, type in the following command at the Unix prompt to run the program:
  
    python first.py
 
  
If you get a message of this form:
+
  [111c-bz@beowulf ~]$ '''python temp.py'''
  
  
   File "firstlab.py", line 5
+
* If you get a message of this form:
     print "x = " x
+
 
                  ^
+
 
 +
   File "temp.py", line 5
 +
     print "The temperature in Celcius is" tempC
 +
                                        ^
 
  SyntaxError: invalid syntax
 
  SyntaxError: invalid syntax
  
then you probably didn't type the program correctly. Make sure didn't forget double quotes or commas, and spelled all the words correctly.
+
:then you probably didn't type the program correctly. Make sure didn't forget double quotes or commas, and spelled all the words correctly.
  
You will know that your program works if you see it output x+y = 11.
+
* You will know that your program works if you see it output the equivalent Celsius temperature of one expressed in Farenheit.  A quick way to check that the program works correctly is to verify that the program outputs 0 for 32 degrees farenheit, and 5 degrees Celsius for 41 farenheit.
  
==Version 2 of your program==
+
==Another program: ask the user for her name==
  
Modify your program so that it outputs the sum of three variables, x, y, and z, which you will have initialized with the number 5, 6 and 10. Use emacs to open your program as you did before, make the changes you feel are necessary.
+
* If you are still inside emacs, return to the Linux prompt by typing '''C-x C-c'''
 +
 
 +
* Your cursor should be at the Linux prompt :
  
<bluebox>
 
If you get stuck, don't hesitate to ask for help!
 
</bluebox>
 
  
Make sure your program works and outputs the correct output before moving on to the next part.
+
  [111c-xx@beowulf ~]$ _
  
==A Third Program==
+
* You are going to create a new program.  Let's call it getname.py
  
Once you are done with the first two programs, exit emacs, and at the prompt call emacs again, but this time give it the name of a new program:
+
  [111c-bz@beowulf ~]$  '''emacs getname.py'''
  
emacs third.py
+
* And type in (or copy/paste) the program below:
  
Enter the following lines:
 
<font size="+1">
 
 
<source lang="python">
 
<source lang="python">
 
+
# getname.py
# third.py
+
# yourname here
# 111c-xx
+
# 111c-xx (replace xx by your two-letter Id)
  # first lastname
+
 
  #
+
def main():
  # prompts the user for first and last name, along with year of birth
+
    first = raw_input( "Enter your first name, please: " )
# outputs approximate age
+
    last = raw_input( "Enter your last name, please:  " )
 +
    print "Welcome to CSC111,", first, last, "!"
 +
 
 +
main()
 +
 
 +
</source>
 +
 
 +
* Save your program and return to the Linux prompt with '''C-x C-c"
 +
 
 +
* Run your program
 +
 
 +
    [111c-bz@beowulf ~]$ '''python getname.py'''
 +
 
 +
* Verify that it runs without errors.
 +
 
 +
* Once it works correctly, modify your program again, and make it ask for
 +
** the town where you were born
 +
** the country where you were born
 +
:and make it display this information in the following fashion:
 +
 
 +
    [111c-bz@beowulf ~]$ '''python getname.py'''
 +
    Enter your first name, please: '''Alice'''
 +
    Enter your last name, please: '''Doe'''
 +
    Which town were you born in? '''Northampton'''
 +
    What country is this in?          '''USA'''
 +
 
 +
    '''Alice Doe, of Northampton, USA, welcome to CSC111 !'''
 +
 
 +
* When your program works, move on to the next section.
 +
 
 +
==Version 2 of your temperature program program==
 +
 
 +
* Make sure you are back at the Linux prompt
 +
* Edit your program one more time:
 +
 
 +
  [111c-bz@beowulf ~]$ '''emacs temp.py'''
 +
 
 +
 
 +
* Modify your program so that it prompts the user for the name of the town first, then the name of the state, then for the temperature in Fahrenheit, and then outputs the town, the state, and the temperature in Celsius.
  
fname      = raw_input( "what is your first name? " )
+
Here's an example of the way the program should work (the user input is in bold face):
lname      = raw_input( "what is your last name? " )
 
yearOfBirth = input( "What year were you born? " )
 
  
  print "Hello", fname, lname
+
  [111c-xx@beowulf ~]$  '''python temp.py'''
  print "you must be about", 2007 - yearOfBirth, "years old"
+
   
   
+
  What town to you live in? '''Northampton'''
 +
What state is this in? '''Massachusetts'''
 +
What is the current temperature in degrees Fahrenheit? '''41'''
 
   
 
   
</source>
+
The temperature in Northampton in Massachusetts is 5 degrees Celsius.
</font>
+
 
 +
 
 +
<bluebox>
 +
If you get stuck, don't hesitate to ask for help!
 +
</bluebox>
 +
 
 +
<br />
 +
Make sure your program works and outputs the correct output before moving on to the next part.
 +
 
 +
That's enough for the first lab.  The purpose of the lab is more to get you acquainted with the programming environment than writing Python code. 
  
Once you're done, exit emacs and run your program:
+
There is nothing to hand back for this lab. 
  
  python third.py
+
If you have time and want to check the homework assignment, feel free to have a look at it!
  
Verify that it works and gives you correct information.
 
Modify your program so that instead of asking for the year of birth, the program asks for your age, and then outputs what year you were born in.
 
  
 
==Homework #1==
 
==Homework #1==
  
That's it for this lab! You are now ready for [[CSC111 Homework 1 | Homework #1]]!
+
You are now ready for [[CSC111 Homework 1 | Homework #1]]!
  
 
You can also get it by going to your instructor's Web page, http://cs.smith.edu/~thiebaut, and by clicking on the CSC111 shortcut.
 
You can also get it by going to your instructor's Web page, http://cs.smith.edu/~thiebaut, and by clicking on the CSC111 shortcut.
  
Locate class page, then the week by week calendar, and the link to the first homework assignment. It is due the evening of Thursday Sept. 13th, at midnight.
+
Locate the schedule page, then the week by week calendar, and the link to the first homework assignment. It is due the evening of Thursday Feb. 7th, at midnight.
  
==Installing and using Secure Shell on a Windows PC or a Mac==
+
==How to Installing and using Secure Shell on a Windows PC or a Mac==
  
The Smith College Technology Services Web pages have some good information on how to install the Secure Shell client on a Windows machine. Visit this [http://www.smith.edu/its/tara/file_transfer/ssh_sftp.html page] for more information.
+
The Smith College Technology Services Web pages have some good information on how to install the Secure Shell client on a Windows machine. Visit their [http://www.smith.edu/its/tara/file_transfer/ssh_sftp.html Web site] for more information.
  
<!--Carnegie Mellon University also has a nice page on the Secure Shell utility on a Mac running OS X. Just replace unix.andrew.cmu.edu with beowulf.csc.smith.edu in the tutorial. page on SSH for the Mac.-->
+
[[Category:CSC111]][[Category:Labs]]

Latest revision as of 11:08, 3 February 2010

Feel free to work in pairs, share insights and help your neighbors during the lab. This is true of all labs. While some homework assignments will have to be done as individual work, the labs are a good time to cooperate with neighbors or work in pairs.

Login/logout

To login refers to the action of connecting to the computer as a user. To connect to Beowulf, the computer used for Computer Science classes, one must have an account and a password. Logout is the term used when you are finished with the computer and want to terminate your session. You should have a Unix account form at this point, and have found on it your account name (of the form 111c-xx, where xx are two letters, different for everybody), and your password. Using the Windows PC in front of you, find the SecureShell program/icon on the desktop (or via the Start/Program menu) and click on it.

If the Quick Connect window is not greeting you, click on the Quick Connect icon to get it:

Enter beowulf.csc as the host name, 111c-xx as your user name (change the xx to reflect your 2-letter Id) Click on the Connect button Enter your password. and click "Connect".

If everything went well, you should see this:

SSH Secure Shell 3.1.0 (Build 235)
Copyright (c) 2000-2001 SSH Communications Security Corp - http://www.ssh.com/

This copy of SSH Secure Shell is a non-commercial version.
This version does not include PKI and PKCS #11 functionality.

[111c-xx@beowulf ~]$ 

Keep your account and password information in a safe place, or learn both by heart! You will need them every time you need to use your account and work on your homework assignment or lab.

You may want to log out now, to practice this login procedure a couple times. If you are working with a lab partner, it is a good time to log out and let her/him try the procedure.

To logout of the session with beowulf, simply type exit at the Linux prompt:

 [111c-xx@beowulf ~]$  exit

Don't hesitate to call on the TA or your instructor if you need help.

Playing with the Emacs Editor

In order for you not to suffer from a misbehaving backspace/delete key as happened in class yesterday, you should execute the following command (your input is in bold face)

  [111c-xx@beowulf ~]$  getcopy .emacs
  cp: overwrite `./.emacs'? y

(That is "getcopy" space period "emacs") It's a configuration file that will allow emacs to understand the backspace key correctly. You only need to do this once and you will be set for the semester.

You will now get a copy of a file from your instructor's account, and edit it. The file is called Fulghum and is a short story from Robert Fulghum's book All I really need to know I learned in Kindergarten.

To get a copy of this file, type


  [111c-xx@beowulf ~]$  getcopy fulghum


followed by the return key. Then edit the file with the command


  [111c-xx@beowulf ~]$  emacs fulghum


Simply work on the first paragraph of the file, and try to straighten it up. I have indicated below, on the right-hand side, which emacs commands you can use to fix the text on your screen.

Fulghum emacs.jpg

When you are done with the modification, or if you want to stop for right now and save your modifications, type C-x C-c, and press y when emacs asks if you want to save the file. Below is a summary of some useful emacs commands.

You should find some time this week to go to a lab and read the Emacs at Smith handout while in front of the terminal, on your own. This is the best way to test and learn the different features of the editor!

DEL/Shift Backspace Delete previous character
c-d Delete character at cursor
Esc-d Delete word
c-k Kill line
c-a Go to beginning of line
c-e Go to end of line
c-f Move cursor 1 character forward
c-b Move cursor 1 character backward
c-p Move to previous line
c-n Move to next line
c-x c-c quit emacs and save file
c-x c-s save file but remain in emacs
c-g GE OUT OF TROUBLE!

Part I of the homework assignment for this week is to edit the complete story about Larry Walters and his flight over Los Angeles.


Paying with Python in Interactive Mode

  • At the Linux prompt, type:
  [111c-xx@beowulf ~]$  python

followed by the Return key. Try some of the examples we did in class yesterday. See if you can guess how python will interpret the commands before you press the Return key.

 Python 2.6 (r26:66714, Nov  3 2009, 17:33:38) 
 [GCC 4.4.1 20090725 (Red Hat 4.4.1-2)] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 
 >>> 3
  
 >>> 4
  
 >>> 6
 
 >>> 3 + 4
 
 >>> a = 3
 
 >>> b = 4
 
 >>> a
 
 >>> a + b
 
 >>> a * 2 + b
 
 >>> 'first' = 'Carole'
                                                 (why does this generate an erro?)
                                      
 >>> first = 'Carole'

 >>> last = 'Christ'

 >>> first

 >>> first + last

 >>> first * 2

 >>> ( first + ' ' ) * 2                  (predict the output before you press Return)

 >>> fullname = first + ' ' + last

 >>> print fullname

 >>> print 'fullname'                  (predict the output before you press Return)

                                               (does the output make sense?)
 >>> 

  • Type Control-D to exit the Python interactive shell and to return to the Linux prompt.


Creating your first Python program

  • You are now ready to create your first Python program. Don't worry if what you are doing does not make sense yet. We will go over all of these steps in details in class these coming weeks.
  • Make sure you have exited emacs and are back to the Unix prompt.
  • Open a second Secure Shell (ssh) window. You will use one to edit your program, the other one to execute your program.


  • In Window 1, use emacs to create a file that will contain your program:


  [111c-bz@beowulf ~]$ emacs temp.py
  • Type in (or copy-paste) the following lines
# temp.py
# 111c-xx (change and use your own account)
# yourfirstname yourlastname
#
# My first python program                              
# Gets a temperature in Farenheit and prints its equivalent
# in Celsius
 
def main():
      tempF = input( "Enter temperature in Farenheit: " )
      tempC = ( tempF - 32 ) * 5 / 9
      print "The temperature in Celcius is", tempC

main()
  • Be careful that the three lines under def main(): are indented exactly the same way. It's an important aspect of python, that the indentation indicates a form of hierarchy of the program.
  • Type c-x c-s (control-x followed by control-s) to save the file. In Window 2, type in the following command at the Unix prompt to run the program:


  [111c-bz@beowulf ~]$ python temp.py


  • If you get a message of this form:


 File "temp.py", line 5
   print "The temperature in Celcius is"  tempC
                                        ^
SyntaxError: invalid syntax
then you probably didn't type the program correctly. Make sure didn't forget double quotes or commas, and spelled all the words correctly.
  • You will know that your program works if you see it output the equivalent Celsius temperature of one expressed in Farenheit. A quick way to check that the program works correctly is to verify that the program outputs 0 for 32 degrees farenheit, and 5 degrees Celsius for 41 farenheit.

Another program: ask the user for her name

  • If you are still inside emacs, return to the Linux prompt by typing C-x C-c
  • Your cursor should be at the Linux prompt :


  [111c-xx@beowulf ~]$ _
  • You are going to create a new program. Let's call it getname.py
  [111c-bz@beowulf ~]$  emacs getname.py
  • And type in (or copy/paste) the program below:
# getname.py
# yourname here
# 111c-xx (replace xx by your two-letter Id)

def main():
    first = raw_input( "Enter your first name, please: " )
    last  = raw_input( "Enter your last name, please:  " )
    print "Welcome to CSC111,", first, last, "!"

main()
  • Save your program and return to the Linux prompt with C-x C-c"
  • Run your program
   [111c-bz@beowulf ~]$  python getname.py
  • Verify that it runs without errors.
  • Once it works correctly, modify your program again, and make it ask for
    • the town where you were born
    • the country where you were born
and make it display this information in the following fashion:
   [111c-bz@beowulf ~]$  python getname.py
   Enter your first name, please: Alice
   Enter your last name, please: Doe
   Which town were you born in? Northampton
   What country is this in?          USA 
  
   Alice Doe, of Northampton, USA, welcome to CSC111 !
  • When your program works, move on to the next section.

Version 2 of your temperature program program

  • Make sure you are back at the Linux prompt
  • Edit your program one more time:
 [111c-bz@beowulf ~]$ emacs temp.py


  • Modify your program so that it prompts the user for the name of the town first, then the name of the state, then for the temperature in Fahrenheit, and then outputs the town, the state, and the temperature in Celsius.

Here's an example of the way the program should work (the user input is in bold face):

 [111c-xx@beowulf ~]$  python temp.py

What town to you live in? Northampton 
What state is this in? Massachusetts
What is the current temperature in degrees Fahrenheit? 41

The temperature in Northampton in Massachusetts is 5 degrees Celsius.


If you get stuck, don't hesitate to ask for help!


Make sure your program works and outputs the correct output before moving on to the next part.

That's enough for the first lab. The purpose of the lab is more to get you acquainted with the programming environment than writing Python code.

There is nothing to hand back for this lab.

If you have time and want to check the homework assignment, feel free to have a look at it!


Homework #1

You are now ready for Homework #1!

You can also get it by going to your instructor's Web page, http://cs.smith.edu/~thiebaut, and by clicking on the CSC111 shortcut.

Locate the schedule page, then the week by week calendar, and the link to the first homework assignment. It is due the evening of Thursday Feb. 7th, at midnight.

How to Installing and using Secure Shell on a Windows PC or a Mac

The Smith College Technology Services Web pages have some good information on how to install the Secure Shell client on a Windows machine. Visit their Web site for more information.