CSC 111: Introduction to Computer Science

Lab 3: Math Library and Operators; The Datetime Library

Thursday, Feb 10, 2011

Part 1: The Math Library, and Math Operators

The goal for this part of the lab is for you to play around with, and become comfortable with, basic mathematical and numerical operations in Python. You will also want to be comfortable with the general capabilities and constraints in using the math operations, as they are often similar from language to language.

Math Library

Import the math library into IDLE and experiment with the following operations until you are comfortable with what they do. Among things to think about are: What type (as in datatype) of argument does the operation, or function, expect you to provide, and what type of value does the operation return to you as the answer? (You can use type() to help in determining the datatype of the returned value.)

Raising a number to a power

Compare the '**' operator for raising a number to a power, with the math library pow() by trying some calculations, such as those below. Make sure you understand why some give unexpected answers. You may need to resort to the docs.python.org site.

Make sure you are comfortable with the order of operations, and which math operators, if any, take precedence over the other. For this experimentation, you could try calculations such as:

>>> 3 + 5 * 2
>>> (3+5) * 2
>>> 3 + (5*2)
Also experiment with the other math operators, '/,' '%,' '-,' 'abs(),' and '**' (more with that next)
>>> -1 ** 2
>>> (-1) ** 2
>>> 3 ** 5.6
>>> pow(3, 5.6)
>>> 5.6 ** 3
>>> 5.6 ** (-3)
>>> -3 ** 5.6
>>> pow(5.6, -3)
>>> pow(-3, 5.6)

What do you need to do to these statements to get the expected result? Why don't these work as expected?

>>> 4.0**(1/2)  
>>> pow(4,1/2)

Part 2: The Math Library and Cube Roots

This is the part of the lab for which you need to create some computer code all on your own. It can be daunting to have a blank page in front of you, so begin by writing out some pseudocode, and then fill in the details once you have the basic algorithm and program flow worked out. Your task is to write a program to calculate the sum of the cube roots of the first n natural numbers, where n is input by the user. The objective of this exercise is for you to Here is one of many possible solutions.

Part 3: Experimenting with Time and Datetime

For this final part of the lab you will experiment with the time and datetime libraries. This will also give you some more experience using objects, which will be covered in more detail next week in chapter 5. Using the date and time functions of your computer's operating system can be very helpful in keeping track your own computations and when you completed them if you are performing extensive numerical analysis that goes on for days. They are also just fun to use.

'time' Library

The library 'time' is a low level library that has quite a few useful functions. Try playing around with the following options, as well as others things you think of to try. Share things you try with others in the class, and feel free to use Google to see what else you can discover.

NOTE: Be certain you do not save these files either as time.py or datetime.py. These names are taken! They are the names of the libraries you are importing! If you were to do this, or save any file of your own with the name of any python library, you will hopelessly confuse Python.

import time

# note the output of the first line, and observe what the "[7]" in the second line does
print "The time is ", time.localtime()
print "Today is day", time.localtime()[7], "of this year" 

print
# A short pause in program operation is provided by:
seconds = 6   # use any number you want here
print "Pausing for ",seconds, " seconds..."
time.sleep(seconds)
print "I'm awake!"

print
print "Timing the time required to count to 100,000 in a 'for' loop"
cnt = 100000
start = time.clock()
for x in range(cnt):
   y = x  # a simple assignment for the for loop
end = time.clock()
print "Time taken to count to ", cnt, "is ", end - start, "seconds"

print
# This *might* be different for Mac OS versus Windows 
print "Using time a second time"
print "Timing a 100,000 counted 'for loop' "
start = time.time()
for x in range(cnt):
   y = x  # a simple assignment
end = time.time()
print "The time that has passed this time is ", end - start, "seconds"

'datetime' Library

A second library is the 'datetime' library. Try these lines of code to start, and experiment with other things you think of, to get to understand some of this library. Which library do you find more versatile and useful? Is each better for some functions or does one seem to do it all? If something seems new below, such as 'ctime' or 'ordinal,' look it up online and see what you find. Are you getting the message that the web is a tremendous resource for answering questions you have about programming and computers?
import datetime

today = datetime.date.today()
print "Today is day", today.timetuple()[7], "of ", today.year

# Finding today's date
today = datetime.date.today()
print
print "The date is", today 
print 'ctime:', today.ctime()
print 'tuple:', today.timetuple()
print 'ordinal:', today.toordinal()
print 'Year:', today.year
print 'Mon :', today.month
print 'Day :', today.day

print
rightNow = datetime.datetime.now()
print rightNow

print
t = datetime.time(1, 2, 3)
print 't :', t
print 'hour  :', t.hour
print 'minute:', t.minute
print 'second:', t.second
print 'microsecond:', t.microsecond

dateTm = datetime.datetime.combine(today, t)
print 'dateTm: ', dateTm