15110 Summer 2019

Lab 2


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.)


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:


  1. circle_area.py (demonstration)
  2. answers.txt
  3. sphere_vol.py
  4. my_func.py

Place these files in a lab2 folder. Before leaving lab, zip up this folder, and hand it in.

CA Demonstration

1.1 - Student Activity

  1. 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?

    1. 250
    2. 28 % 5
    3. 2.5e2
    4. 3e5
    5. 3 * 10**5
    6. 20 + 35 * 2
      Why is this different from (20 + 35) * 2?
    7. 2 / 3 * 3
    8. 2 // 3 * 3
      Why is this different from 2 / 3 * 3?
    9. 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

  1. 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.

  2. 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.