15110 Summer 2015
Lab 2
IMPORTANT RULES FOR ALL LABS
In order to receive credit for lab, you must attend your lab section
and submit your work during that lab. You cannot attend another lab
section, and you cannot submit from elsewhere
(e.g. your dorm room). We want you to go to lab since you will get
additional information from your course assistant that will help you,
and you will be able to work with other students to share ideas and
learn how to solve problems more effectively. It is also important to
go to lab and use the Linux machines since your lab exams will be
run on these machines so you need to familiarize yourself with
this environment.
If you go to a different
lab section or submit the work early or late, you will receive a 0.
You are allowed to drop 2 labs without penalty, so please do
not ask for makeups. Look ahead at your schedule to see if you will be
missing a lab due to another event and plan on dropping that lab.
If you drop a lab, you are still responsible for reviewing the
material covered.
(Note: You must be in attendance for lab exams. Lab exams cannot be
dropped.)
Goals
The purpose of this lab is to introduce you to the skills you need to interact with Python and to create and test simple Python
programs. When you are done, you should be able to:
- Understand and navigate your AFS space using Unix commands.
- Write, save and run simple programs in Python3.
- Understand the difference between print and return.
- Write comments in your code and understand their purpose.
Deliverables
circle_area.py (demonstration)
answers.txt
sphere_vol.py
my_func.py
Place these files in a lab2 folder. Before leaving lab,
zip up this folder,
and hand it in.
CA Demonstration
- Autolab - How to get grades, view source, check comments.
- Unix commands - ls, cd, cd .., pwd, mkdir, zip, gedit &.
- Permissions and saving work in private folder.
- Creating a lab2 directory (using the command line).
- Starting and using the python3 interpreter in a Terminal window.
- Creating and using functions:
- If you are using GEdit, set Gedit line numbers and tabs.
- Use IDLE / Gedit to define a function in force.py.
- Load force.py: from force import *.. (if you are using IDLE)
or python3 -i force.py. (if you are using terminal)
- Parameters and return value of force(m, a).
- Math constants:
- A = π r2
- math.sqrt(x) and math.pi
1.1 - Student Activity
Type each of the following expressions into python3.
What value do each of the following Python expressions evaluate to?
Is that value an integer or a floating point?
250
28 % 5
2.5e2
3e5
3 * 10**5
20 + 35 * 2
Why is this different from (20 + 35) * 2?
2 / 3 * 3
2 // 3 * 3
Why is this different from 2 / 3 * 3?
25 - 5 * 2 - 9
Is this different from ((25 - 5) * 2) - 9
and/or 25 - ((5 * 2) - 9)? Why?
Write your answers in the file answers.txt.
(Review Unit 2A lecture slides if
necessary.)
2.1 - Class Activity
In circle_area.py, define a Python function
circle_area(r) that calculates and returns
the area of a circle given radius r. This can be calculated
using the formula:
A = π r2
Also in the file circle_area.py, define a Python function print_circle_area(r) that calculates and prints the area of a circle given radius r.
In answers.txt, explain in your own words the difference between print and return.
2.2 - Student Activity
In sphere_vol.py, define a Python function
sphere_vol(r) that calculates and returns
the volume of a sphere with a radius r. This can be calculated
using the formula:
V = (4/3) π r3
Place in answers.txt a copy of your interaction with python3
in which you call sphere_vol(r) to compute the
volume of a sphere with a radius of 7, and python3 shows
you the result.
Pick a mathematical formula that is used in your major. In a file
called my_func.py, create a python3 function
that calculates some value using that formula.
At the beginning of your my_func.py include a comment
(one or more lines of English text that start with "#" and are
ignored by python3) that describes what the fomula the
function computes and what units the parameters and results are in.
Submit Your Lab Work
Create a zip file called
lab2.zip that contains your
text files, and submit it for grading using the handin
instructions. Be sure that you create a zip file and
not a compressed file in another format.