15-110 Principles of Computing - SUMMER TWO 2014

Lab 2

Goals

The purpose of this lab is to introduce you to the skills you need to use Autolab, interact with Python, and to create and test simple Python programs. When you are done, you should be able to

Files

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

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

CA Demonstration

Activities

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

  3. Pick a mathematical formula that is used in your major. (For APEA students, pick any mathematical formula that interests you.) 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.