CS8, Spring 2017

Lab01:
Writing and Running Python code,
Expressions, Pair Programming


Goals for this lab

By the time you have completed this lab, you should be able to

You watched a video about Pair Programming for Hw1, and now you get to practice it. Don't worry if this is your first experience with Pair Programming. You will learn more about it as the quarter progresses. In short, there are two roles: pilot and navigator. The pilot sits at the computer and types, and the navigator helps. It is important that each of you thinks about the problems independently. The pilot directs, but does not leave the navigator behind. The navigator assists, but does not give the pilot commands. You are helping each other so that you both understand the work.

Step by Step Instructions

Step 1: Choose initial pair programming roles

Choose who will be the pilot for the first part of the lab. The pilot should sit down in front of the computer now. The navigator gets a chair and sits next to the pilot.

Step 2: Log in, create a lab01 directory, and open IDLE

Log onto the pilot's account. If the pilot's account is not working, allow the navigator to log in instead. Navigate using "cd" to get to your cs8 directory, and use "mkdir" to create a new directory lab01. Then navigate into the lab01 directory, and open IDLE:

-bash-4.2$ cd cs8
-bash-4.2$ mkdir lab01
-bash-4.2$ cd lab01
-bash-4.2$ idle3
Leave the IDLE window open to use in later steps. But first some paper work to do.

Step 3: Writing expressions in Python

On a piece of paper, write the following items as expressions in Python. Make sure you construct your expressions so that the answer is the right type.

Before moving on, make sure you and your partner have gone through the answers. If you disagree on any, discuss why each of you thinks it should be a particular way. Don't worry if you do not come to any agreement right now - you can try both ways and see who is right. Just make sure that when you do verify which is right, you both understand why that was the correct answer.

Step 4: Writing code in IDLE

In IDLE, you have two choices for testing your expressions. First, you may write each one individually into the Python shell and see what the results are. This is useful for trying out something small, like the expressions above. So first try each of your answers out in the Python shell. You can quickly check your work by doing this. For example (user input is bold):

>>> 4 + 3 * 2
10

Often, though, it is useful to write the code and be able to execute it several times without retyping the whole thing. As last time, select File->New Window to open a new window for Python code. At the top of your file, write a Python comment (start it with a '#' character) that includes both your name and your partner's name, as well as the current date. For example:

# JoeBob Smith and SallyMay Johnson, 1/13/2015

Save the file as you saw last time using File->Save As. Name it lab01.py and save it in your lab01 directory.

Then type your expressions into the new window, but enclose each one in a call to print(). The reason is that when you run all of the expressions at once, Python executes them internally. To see each result, you need to print the results of the expression. For example, to evaluate the expression 4 + 3 * 2, type this code:

print(4 + 3 * 2)

Select Run->Run Module to cause all of the Python code to execute, line-by-line. Save when necessary.

Step 5: Assigning Variables in IDLE

First: switch roles between pilot and navigator if you did not already do that.
The navigator is now at the keyboard and becomes the pilot. The pilot becomes the navigator to assist the pilot.
Always stay logged into the original pilot's account.

For each of the expressions in your file, decide on an appropriate variable name and assign the result of the expression into the variable. Then in the print statements, place the variable name rather than the expression. For example:

answer = 4 + 3 * 2
print(answer)

Rerun your module so that you can see the results.

Also, notice that after running the module, you can just type the variable name into the Python shell to display the value. Try that with one or more of your variables.

Step 6: Show off your work and get credit for the lab

Get your TA's attention to inspect your work, and to record your lab completion.

Don't leave early though ... see challenge problems below.

Step 6a. ONLY IF YOU RAN OUT OF TIME TO HAVE THE TA INSPECT YOUR WORK

If you must complete this assignment at CSIL, then submit it with the turnin program. You MUST have both your name and your partner's name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Phelps 3525.

Bring up a terminal window on CSIL, and cd into the original pilot's cs8 directory, and cd again into the lab01 directory. Then type the following:

turnin Lab01@cs8c lab01.py

Respond "yes" when the program asks if you want to turn in (be sure to read the list of files you are turning in), and then wait for the message indicating success.


Evaluation and Grading

Each student must accomplish the following to earn full credit for this lab:

Optional Extra Challenge


Template © 2009, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved. Adapted by Diana Franklin and Michael Costanzo.