14
Com S 104b Notes for Lab Exercises, 3/11 and 3/12 During this lab, you are free to work together with the person next to you, as long as you eventually get everything done yourself. Remember that collaboration is NOT allowed if you are doing these exercises outside of the scheduled lab period. If you are doing the lab on your own, or if you don’t finish, just remember to show the checkpoints to one of the TAs within one week of your scheduled lab time. If you have questions or if something doesn’t work, raise your hand and the TA will come help you. This lab is intended to provide you an easy introduction to the lab environment and make sure you are comfortable with a few tools we will need to use quite a bit: 1. WebCT announcements and discussions 2. Windows Explorer, network drives, and file extensions 3. The Python interpreter 4. Submitting homework via WebCT There aren’t really any “problems” for you to solve, just a few things to try out. There are three “checkpoints”. When you reach a checkpoint, be sure to show it to the TA, since that’s how we’ll give grades for participation in the lab. Note: If the TA is busy when you reach a particular checkpoint, you can continue on with the rest of lab! 1.Using WebCT We use WebCT for announcements, discussions, posting clarifications about homework problems, submission of homework, and recording grades. There will be a discussion topic dedicated to each homework where questions can be posted; you can also reply to questions posted by others. The instructor and TAs try

web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

Com S 104bNotes for Lab Exercises, 3/11 and 3/12

During this lab, you are free to work together with the person next to you, as long as you eventually get everything done yourself. Remember that collaboration is NOT allowed if you are doing these exercises outside of the scheduled lab period.

If you are doing the lab on your own, or if you don’t finish, just remember to show the checkpoints to one of the TAs within one week of your scheduled lab time. If you have questions or if something doesn’t work, raise your hand and the TA will come help you.

This lab is intended to provide you an easy introduction to the lab environment and make sure you are comfortable with a few tools we will need to use quite a bit:

1. WebCT announcements and discussions2. Windows Explorer, network drives, and file extensions3. The Python interpreter4. Submitting homework via WebCT

There aren’t really any “problems” for you to solve, just a few things to try out. There are three “checkpoints”. When you reach a checkpoint, be sure to show it to the TA, since that’s how we’ll give grades for participation in the lab. Note: If the TA is busy when you reach a particular checkpoint, you can continue on with the rest of lab!

1. Using WebCT

We use WebCT for announcements, discussions, posting clarifications about homework problems, submission of homework, and recording grades. There will be a discussion topic dedicated to each homework where questions can be posted; you can also reply to questions posted by others. The instructor and TAs try to check the posted questions regularly. (The only thing to be careful of is not to directly post solutions to homework problems that are still due, which would violate the academic dishonesty policy.)

Here’s a simple exercise to get started.

1. Log into WebCT. (Go to the ISU main page and click the WebCT link under “Sign-Ons”) Important: when you see the security warning as shown below for running an application published by dc.blackboard.com, be sure to click “Run”.

Page 2: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

2. Read the latest announcement, and follow the instructions.

2. Where are my files?

You probably know by now that any work you do on a computer has to be saved as some kind of file if you want it to still be there after the computer is turned off. When you are writing papers and such on your own computer at home, you don’t have to worry much about exactly where those files are saved; you can usually just let Microsoft Word (or whatever application you’re using) save your documents in its “default” location, sometimes called the “My Documents” folder, depending on your operating system.

For a programming course, you’re going to have to keep track of exactly where your files are. In particular, when working in the lab, you need to be sure to store them somewhere that is NOT on the lab computer’s local file system or “hard drive” (normally called the “C:” drive on the lab machines). User files that are stored on the lab machines may be erased at any time.

There are several good choices for safely storing your work:

1. Your computer science department account. Once your account is activated, you can just double-click on the “Odin” icon on the desktop of the lab machines and log in by typing COMS\username (You need to type the COMS\ prefix) You have an account you can use as long as you are registered in a CS course. To activate it, use the link http://register.cs.iastate.edu.

2. Your AFS directory. This is storage on a university network server. It is normally visible on the lab machines as the “G:” or “H:” drive after you log in.

3. A flash drive that plugs into the USB port. (However, these are not as reliable as a network server, and seem to get lost along with our keys from time to time.)

Even if you are using your own computer, you should always back up your files on a network server such as (1) or (2), or at least on a flash drive or CD. Local hard drives always fail at some point (and always seem to do it right when an assignment is due).

Page 3: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

Viewing directories and files

Right-click on the “Start” button and select the “Explore” option to open up Windows Explorer. In the left-hand pane click on “Computer”. Your AFS directory should appear as “G:” or “H:”, as shown in Figure 1 below. (If you use a CS account, it will be similar but will appear under “Network” rather than “Computer”.) If you insert a flash drive into one of the USB ports, it will appear under “Computer”, mapped to the next available drive letter.

NOTE: If you are using your own laptop for the lab, follow the instructions using a flash drive. Where the instructions refers to drive H:, use whatever letter the system assigns to your flash drive.

NOTE: If you do not see your AFS directory, try the following: log out, and log back in. When the system comes up you’ll see a command shell that says something like “Using G: for //AFS/iastate/blah/blah/…” Leave that shell alone until it closes on its own. If that doesn’t work, you may need to do a full restart.

Double-click on the network drive icon to view its contents. Create a new directory (folder) as follows:

1. Right-click in the Explorer pane and select “New” and then “Folder” 2. Edit the folder name to be “cs104”. 3. Then create a directory inside cs104 called “lab1”.

This new directory can be referred to by a full pathname, which will be something like H:\cs104\lab1 (depending on the drive letter assigned by the system).

Page 4: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

Figure 1 - Your AFS active directory in Windows Explorer

How to see file extensions

Most files have an “extension” – two or three letters that follow a dot at the end of the filename. The extension usually helps identify the type of the file. For example, Word documents have an extension .doc or .docx, a JPEG image usually has the extension .jpg, and so on. Unfortunately, Windows Explorer does not show the extensions by default. In programming, we often have to distinguish between files that have the same name, but different extensions.

To make file extensions visible in Explorer, follow the steps below:

1. In Windows Explorer Click on “Organize” and from the drop-down menu select “Folder and Search Options”. (Note: in older versions of Windows or in the “Classic” view, the Folder Options are under the Tools menu.)

Page 5: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

Figure 2 - changing folder options

2. In the Folder Options dialog that comes up, click on the “View” tab.

3. UN-check the option “Hide extensions for known file types”.

4. Click “Apply to Folders” and then “OK”

(See figure 3.)

Page 6: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

Figure 3 – uncheck the box next to “Hide extensions for known file types”, then click “Apply to Folders”

Open up the Notepad text editor (go to Start -> Programs -> Accessories -> Notepad), and type a few lines of text. Save the file as “testfile.txt” in the cs104\lab1 directory you created on your network drive. (or flash drive, if you are using your own computer).

3. Using the Python interpreter

We’re starting our experience with programming using a language called Python. It is a fully-featured, object-oriented language that is used in real-world applications, but it is also very easy to start using because of its simple syntax and because it has an interactive interpreter that is ideal for experimentation and learning.

On the lab machines, Python should be available through the Start menu: Go to Start -> Programs -> Python 2.6 -> IDLE to bring up an interactive shell for the interpreter.

If you don’t see Python on the start menu, you can start it using a command shell:

Page 7: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

Go to Start -> Programs -> Accessories -> Command Prompt. At the command prompt, first navigate to the directory C:\Python26. Depending on the details of the system you’re running, you’ll use the command “cd .. ” (that’s cd space dot dot) several times to change the directory to one level up, then once you get to C:\, use cd again to get to Python26, for example:

C:\Documents and Settings\myusername> cd ..C:\Documents and Settings> cd ..C:\> cd Python26

Then run the python command to start IDLE as shown:

C:\Python26> python Lib\idlelib\idle.py

After IDLE starts you’ll see the interpreter prompt “>>>” as in Figure 4. You can type a Python statement and the interpreter will execute it, or you can type a Python expression and the interpreter will evaluate it and display the result.

Figure 4 - The Python interpreter shell in IDLE

Try typing the following simple statement:

>>> print 2 + 3

The interpreter should respond, not surprisingly, by displaying the value 5.

Important terminology:

Page 8: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

A statement is an instruction to DO something, like printAn expression represents a value, such as 2 + 3

Normally an expression doesn’t “do” anything, it just “is”. (The interpreter shell is special, because it will always try to display the value of an expression that you type.) For example, try just typing

>>> 2 + 3

Again you should see 5.

Most programs, of course, are more than one line. Although the shell gives you an easy way to experiment with single statements and expressions, you will usually want to type up statements in a file. A file containing Python statements often called a “script” or “module” and is really just a kind of program. A Python script is normally saved with the extension .py.

It is easy to create, save, and run Python scripts in the IDLE environment:

Go to File -> New Window

You now have an editor window. Type the following three lines:

The first line, starting with the “#” character, is a comment. It is ignored by the interpreter, but is there to describe the script to a human reader.

Next, save the file using File -> Save As. Save it in the cs104\lab1 directory you created earlier. Call it “test1.py”. Important: you have to type the “.py” extension – it won’t be put there automatically, as in many other applications.

Now you can have the interpreter run the script by selecting Run -> Run Module (or just press F5 when your editor window is active). The interpreter window should become active and display the output 5 again, followed by the text Hello, world. This gives you a really easy way to experiment with short programs in Python without having to retype them in the interpreter.

Try another one: this time, modify the script so its first line the expression 2 + 3, without the “print” keyword, as shown below:

Page 9: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

Save it as test2.py and try running it. Notice there’s no output from the first line, because 2 + 3 is just an expression, it isn’t a statement that tells the interpreter to “do” something.

“Hello, world” is an example of a string, or sequence of characters. You can use double quotes or single quotes to represent literal strings in Python.

Notice the difference between 2 + 3 (an arithmetic expression with value 5) and “2 + 3”, which is a string of five characters (counting the spaces). The interpreter evaluates the expression 2 + 3, but “2 + 3” in quotes just is what it is – a literal sequence of characters.

>>> print 2 + 35>>> print "2 + 3"2 + 3

Checkpoint 1

Show the TA your two files in the H:\cs104\lab1 directory: testfile.txt that you created previously, and the script test1.py. Be sure they are located on your network drive or flash drive and that the file extensions are visible.

4. Submitting homework using WebCT

Creating a zip file

For programming assignments you are usually asked to submit a single zip file containing a specific set of files (possibly organized into specific directories). In this week’s lab, you created a directory cs104\lab1 containing (at least) two files, a text file called testfile.txt and a Python script called test1.py. As an exercise, let us suppose that you are being asked to submit a zip file called assignment0.zip containing just those two files. Here is how you might create a zip file with that format.

1. In Windows Explorer, open the cs104\lab1 directory and be sure you see the two files (with the correct extensions visible)

2. Holding down the Ctrl key, select the two files to be submitted.

Page 10: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

3. When both files are highlighted, right-click on one of them and select “Send To” and then select “Compressed/zipped folder”.

4. Rename the resulting zip file “assignment0.zip”

Checking the contents of a zip file

In Vista you can normally double-click on a zip file to explore its contents without actually extracting (unzipping) it. (In some cases Windows reports an error even when the zip file is ok. If that happens, right-click on the file and select the option for “7-zip” to open the zip file.) Make sure that within the zip file you see the two files, testfile.txt and test.py, and nothing else.

Submitting via WebCT

On the course homepage there is a document entitled “How to submit homework via WebCT”. Follow the instructions there to submit your zip file for “Assignment 0”.

Checkpoint 2

Show the TA that your submitted assignment is present on WebCT. Then download the submitted zip file and show the TA its contents. (The howto document also explains how to take back a submission and turn in a new one, in case you mess up!)

5. Arithmetic operators

Now that you are familiar with IDLE and the Python shell, take a look at the online book called “How to Think Like a Computer Scientist” :

http://openbookproject.net//thinkCSpy/index.html

Do exercise 2 from Chapter 1 (the exercises are in section 1.11). Note there are 6 operators for doing arithmetic.

+ addition- subtraction* multiplication/ division% modulus (remainder)** exponentiation (powers)

The behavior of the division operator might be surprising. When used for whole numbers, it is like the kind of division you used to do in grade school: “25 divided by 10 is 2, with 5 left over”.

Using the interpreter, evaluate some expressions such as 25 / 10 and 25 % 10. Try 13 / 5 and 13 % 5. Try a few more pairs of numbers using the operators “/” and “%”.

Page 11: web.cs.iastate.eduweb.cs.iastate.edu/~smkautz/cs127f15/labs/Lab1_notes.…  · Web viewThe Python interpreter. Submitting homework via WebCT. There aren’t really any “problems”

What is the operator “%” doing?

Try 25.0 / 10, and compare it to 25 / 10.

Try using the “**” operator. For example, evaluate 5 ** 2. Try 2 **3. Try 25 ** 0.5 Can you recognize what this does?

Expressions can be composed of other expressions, and you can use parentheses. For example, try evaluating (2 + 3) * 5 versus 2 + 3 * 5

Checkpoint 3

Show the items above to your TA.