CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 3 - Arithmetic


As usual, create two directories for today's class. Create a directory called lecture3 under activities, and a directory called lab3 under labs.

Pick a partner that you haven't worked with yet this semester.


Python Interactive Mode

On Friday, we saw how Python can execute files that it reads in. This is the non-interactive mode of Python. There is an interactive mode that Python can execute in, which can be accessed by issuing the python3 command to the terminal.


Introduction to Arithmetic Expressions

Python supports a variety of arithmetic operators that behave as you would expect them. However, you will see that these operators are more powerful that first sight.


In-class Activity 1

From the command line, copy your classmates.py file from your lecture1 directory into lecture2. Open this new copy in Emacs.

If you did not complete last classes assignment, quickly get at least the name and age of four total classmates, and record them using print statements in your file.

Add a line of code to your program that prints the average age of the four people. The output should be nicely formatted in a sentence that is all one line. The program should compute the average age using the Python addition and multiplication operators. When you test your program, be sure to verify that the calculation is correct.


In-class Activity 2

Age in Seconds

In Emacs, create a Python program in a file called age_in_seconds.py in your lecture2 directory. The program should print your age in seconds. Assume there are 365 days in a year. You don't need to compute your exact age, just convert your age in years to seconds. The program should use Python's arithmetic operations to perform the calculations and should print the number of seconds nicely formatted an labeled.


Comments

As programs become larger, reading a code file becomes more and more labor intensive. Comments are a plain text explanation about what code does.


Lab Assignment 3

U.S. Population

In Emacs create a python program in a file called population.py. The program should print what the U.S. popluation is projected to be in 25 years if the current population growth rate remains constant. All of these calculations should be performed using Python's arithmetic operations and printed nicely labeled and formatted.

According to the U.S. Census the current U.S. population is approximately 314,301,306 and it is changing in the following ways:

Make sure to include a comment header which lists who the authors are, as well as the purpose of the file.


Submission

When you have finished, create a tar file of your lab3 directory. You can do this by first navigating to your labs directory, using the cd command. Then, issue the following command.

tar czf tarfilename.tgz lab3/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 3. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your file.

Make sure you also email all of your code to your partner for this lab.


Last modified: Mon Sep 2 10:31:01 EDT 2013