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.
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.
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$ idle3Leave the IDLE window open to use in later steps. But first some paper work to do.
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.
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.
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.
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.pyRespond "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.
Optional Extra Challenge
- If you did not close IDLE yet, good. If you did, then open it back up again and type a couple of expressions in the Python shell. In both cases, learn how to save a "script" of your work in the Python shell. To do this, in the Python shell window, use File->Save As to save the transcript to lab01.transcript.txt in the original pilot's lab01 directory. During a later IDLE session, you can use File->Open to reopen the file as "text" - it is a record of your former IDLE session, but not an active session, so results of variable assignments and so on will not still exist.
- You should also know that it is not necessary to use IDLE to run Python. Your completed lab01.py file can be executed directly by Python. If you navigate into your lab01 directory (cd ~/cs8/lab01), you should be located in the directory containing lab01.py. Then execute the following command:
-bash-4.2$ python3 lab01.pyThis should act as if you had attempted Run->RunModule in IDLE. This is how the TAs will grade your programs, so it is useful to test your code one last time this way before turning in assignments.- Alternatively, you can just run the Python interpreter directly - without starting IDLE and without feeding in a Python program. Try it now - start python, type in some expressions and watch the results being displayed. Type exit() to quit. For example:
-bash-4.2$ python3 Python 3.3.2 (default, Dec 4 2014, 12:49:00) [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 5 * 4 20 >>> exit() -bash-4.2$- You learned about using (and even writing simple functions) in text and lectures - and you will have do those things in later labs. So if you have time today, why not try it out now? Open IDLE, or a Python shell directly - you know how to do both now - and try out one or more of the built-in functions like max and min:
>>> max(5,7) 7 >>> min(22, -45, 17) -45To use some functions, it is necessary to import a module, and then to use the module's name as part of the function call. For example, the math module has a square root function (sqrt), and here is how you would use it:>>> import math >>> math.sqrt(17) 4.123105625617661 >>> math.sqrt(144) 12.0Typehelp(math)
to learn about more functions from this module, and try some of the other functions for the rest of your lab time.- Really? Still have some time after trying out all the math functions you care about? Then import turtle, make a turtle like the text and lectures showed you - myTurtle=turtle.Turtle() - and make a line drawing of your choice. Remember: help(myTurtle) will tell you what you can ask such an object to do. Next week's lab will be all about using turtle, so this is just a preview.
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.