Lab 1: Python Programming and Numeric Operations
Goals
After the lab, you should know how to
- create a Python program, run it, create output files, print, and submit it
- solve simple arithmetic problems in Python
Objective: Preparing for Lab
- Review your Linux notes from last week.
- If there were any issues with your web page, go back and fix them first.
- Run
runHelpClient &
to help us know who to help next.Recall: what does the
&
allow you to do? - Create a directory called
lab1
in yourcs111
directory. Your programs and the output for this lab will all be saved in thelab1
directory. - Copy the files from
the
/csdept/courses/cs111/handouts/lab1/
directory that end in.py
into yourlab1
directory. - write your programs in jEdit and use the terminal to run
Python. This is what I will tend to do in class.
OR
- write your programs in IDLE, using its shell to run the programs and then restart the shell to demonstrate your program working.
- 26
- 1.414
- 3/4
- 3//4
- 'Hello there!'
- 'Hello ' + 'there!'
- 'Hi' * 4
- 'Hi' + 4
- 3/0
- 3 */ 4
- .1+.1+.1
- print("Hello, world!")
Now, open the file
practice.py
and execute the program.Note that the output from executing
practice.py
is after all the output from your earlier practice.- Close IDLE's interactive Python window and
re-execute
practice.py
. This has the net effect of clearing the window so as not to pollute the output you'll print out when you submit the lab. - (10 points) Copy
practice.py
into a file calledpractice2.py
. Modify the program in the following ways:- Change the first print statements to display another expression and value in the first statement and that same expression that results in that value in the second statement.
- Change the next two print statements to print out another emotion or adjective besides "happy".
- Modify the comments at the top of the program appropriately.
After you are sure that your program works and you have saved your program, start a new IDLE shell by closing the shell and starting a new one. Execute your program to demonstrate that your program works. Save the output from the shell in a file called
practice2.out
. - (10 points) Write a Python program
called
info.py
that prints out information about yourself. The info printed should list your name, favorite pop culture icon, favorite color, and favorite dwarf (Sleepy, Sneezy, Happy, Grumpy, Dopey, Bashful, or Doc). Don't forget to document your program appropriately!Best practice: Use four separate print statements on four separate lines to print this information.
The output should looks something like:
Name: Sara Sprenkle Favorite pop culture icon: Tina Belcher Favorite color: purple Favorite dwarf: Doc
After you are sure that your program works and you have saved your program, close the IDLE shell and re-excute the program. Save the output from the shell in a file called
info.out
. - (15 pts) This problem has several parts. You will create a program (i.e.,
a script) in the first step and modify it for each subsequent step.
Note how the progression of steps for this problem adheres to the good
development practices we dicussed in class. Submit one program and one
output file. I'll know you did the steps from your comments.
- Create three variables (
i
,j
, andresult
) to calculate and display result = i² + 3j - 5 for the case where i=7 and j=2. Your code will not look exactly like this formula. Display the result and verify that it is correct. Consider if you were the user of the program and make the program display appropriate output. Put the answer/output in a comment. - Change the name of the variable
i
toi21
. Be sure to change the name everywhere the variable is used. Execute it to verify it still works. - Now edit the Python script again and change the variable name
to
21i
. What error message do you get? Record in comments the error message and why the error message occurred. - Revert your program back to a correct variable name.
- Make sure that a user looking at your program's execution would understand what the program is doing.
You only need to save the output from executing the final version of your program.
- Create three variables (
- (10 pts) How do you measure, measure a year? The
musical Rent asks how you would measure a year and suggests
love as the metric. Since we don't have a primitive data type that
represents love, we'll measure a year in minutes. Write a program
that computes the number of minutes in a year and plugs it into part
of the chorus of Seasons Of Love.
Note: do not recompute the number of minutes each time it is
used in the chorus. Compute the minutes once and save it in a
variable that you use in the
print
statements.Output should look like:
525600 minutes, 525600 moments so dear. 525600 minutes - how do you measure, measure a year?
- (15 pts) MLB Hall of Famers. Rickey Henderson
was voted into the Major League Baseball Hall of Fame in 2009. At
one time Henderson was the career leader in three major offensive
categories: runs, stolen bases, and walks. (Barry Bonds passed
him in walks.)
Henderson stole 1406 bases and was caught 335 times. Lou Brock is second with 938 stolen bases and 307 caught stealing.
Calculate and display Henderson's and Brock's successful base stealing percentages. (Hint: Divide the number of bases successfully stolen by the number of attempts.) Then, display the difference in their percentages.
Note: The output is not very pretty yet. We'll learn how to format the output more nicely later this term.
- (10 pts) Create a program that converts a given Fahrenheit
temperature to Celsius.
The formula to convert is C=5/9(F-32).
Note that you cannot use this formula exactly in your program.
Output should look similar to:
This program prints the Celsius temperature given a Fahrenheit temperature. 70 degrees F is 21.111111111111111 degrees C
Run the program several times, changing your variable for Fahrenheit, and show the output.
Note: When you demonstrate this program, especially think about what are good test cases for this program. What answers do you know?
- Submit your
lab1
directory to yourturnin
directory by running the command turnin.sh lab1
If everything is set up correctly, you copied yourlab1
directory into yourturnin
directory. If you run the script again, it will copy your current directory and create a backup of the previous submission. - Cleanup: jEdit and IDLE may make backup files. The backup files have an "~" appended to the name of your file. Delete any "~" files from your lab directory. (What is the UNIX command that you can use to delete all of them at once? Use carefully!) Don't worry if you don't have these files.
- If you're not already, go into your
cs111
directory. Create the printable file of your lab using the command:
createPrintableLab <labname>
In this case, the lab name islab1
.Putting it all together, the command you should execute is
createPrintableLab lab1The script
createPrintableLab
creates a condensed version of your lab in a postscript file calledlab1.ps
. To save paper, the script puts two pages on a piece of paper. The script also highlights the Python code appropriately. - View your printout using the command evince
lab1.ps &
evince
allows you to view postscript files, which are suitable for printing. You should verify that the file "looks correct", i.e., no "garbage characters" and that your file is not be more than a few pages. Otherwise, you're printing too much. See the instructor or student assistant if you're having trouble. - The syntax of the command to print the assignment
is
lpr -P<printername> <filetoprint>
The printer name iscslab
when you're in P405 oradvlab
if you're in P413.
The commandlpr
means to print the file. The option 'P' says which printer to use (one of the CS lab printers), and the argument says which file to print--in this caselab1.ps
.For example, the command to print this lab, assuming that you're in your
cs111
directory and you're in the regular classroom (P405), is
lpr -Pcslab lab1.ps - Review the print out with Professor Sprenkle or one of the student assistants before submitting.
- Using IDLE: 15 pts
- Python programs: 70 pts
Objective: Set up for Lab 1
Objective: Introduction to the Python Interpreter (15 points)
On our system, you have two options for writing Python programs:
For your lab work, use IDLE.
Using the Interpreter in Interactive Mode
In a terminal, navigate to your directory for this lab, which you
set up earlier. (The directory should
be ~/cs111/lab1
.)
To open Python's integrated development environment IDLE,
use the command idle3 &
You can use the interpreter in interactive mode to try out expressions before using them in a script. IDLE labels the interactive mode as "Python Shell". In IDLE, the main window--opened by default on start--is the interactive Python interpreter.
Type the following expressions at the Python interpreter prompt, one line at a time, and note what the interpreter returns:
Save the output from the
interpreter in a file called
interactive_practice.out
.
Make
sure that this file is saved in the appropriate directory
(cs111/lab1
).
Using the Interpreter in Batch Mode
Save the output from the interpreter in
a file called
practice.out
. Again, make sure that this file is saved
in the appropriate directory (cs111/lab1
).
Objective: Writing Your Own First Programs
Your programs will be graded on both correctness and style. Style will become more important as the semester continues.
As you probably figured out, the convention is that the names of
Python scripts end in ".py
".
Objective: Arithmetic Operations in Python
We'll practice writing several Python programs, each in their own
text file. Name the files lab1.1.py
through lab1.4.py
.
Your programs will be graded on correctness and style. Make sure you adhere to the good development practices we discussed in class, e.g., use good variable names.
After you've developed a correct solution to each program, close the
IDLE "shell" and reopen it by running the program again (using F5),
demonstrate that the program works, and save the output to a file
named lab1.x.out
, where x is the problem
number.
If you need help, you can use the Python visualizer. Make sure to select "Write code in Python 3". This is the same tool that is used in the online textbook.
Finishing up: What to turn in for this lab
You should be in the terminal to execute the following commands. You should be in your cs111/
directory.
Print out your lab and turn that in by Friday. Write out the Honor Pledge and sign your lab. Staple all pages together and print your name at the top of the first page.
Labs are due at the beginning of Friday's class. You should
hand in the printed copy before the beginning of class, and the
electronic version should be in the turnin
directory before 1:30 p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!