Subsections

Using Modules

Here's this chapter's typing exercise (name it cal.py)[*]:
from __future__ import division, print_function
import sys
if sys.version_info.major == 2:
    input = raw_input
import calendar

year = int(input("Type in the year number:"))
calendar.prcal(year)

And here is part of the output I got:

Type in the year number:2001
                                  2001

       January                  February                    March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                1  2  3  4
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       5  6  7  8  9 10 11
15 16 17 18 19 20 21      12 13 14 15 16 17 18      12 13 14 15 16 17 18
22 23 24 25 26 27 28      19 20 21 22 23 24 25      19 20 21 22 23 24 25
29 30 31                  26 27 28                  26 27 28 29 30 31
(I skipped some of the output, but I think you get the idea.) So what does the program do? The first line import calendar uses a new command import (well, we have seen it, but never explained it before). The command import loads a module (in this case the calendar module). To see the commands available in the standard modules either look in the library reference for python (if you downloaded it) or go to https://docs.python.org/3/library/index.html. The calendar module is described in 8.2. If you look at the documentation it lists a function called prcal that prints a calendar for a year. The line calendar.prcal(year) uses the function. In summary to use a module import it and then use module_name.function for functions in the module. Another way to write the program is:


from __future__ import division, print_function
import sys
if sys.version_info.major == 2:
    input = raw_input
from calendar import prcal

year = int(input("Type in the year number:"))
prcal(year)

This version imports a specific function from a module. Here is another program that uses the Python Library (name it something like clock.py) (press Ctrl and the 'c' key at the same time to kill the program):

from __future__ import division, print_function
from time import time, ctime

prev_time = ""
while(1):
    the_time = ctime(time())
    if(prev_time != the_time):
        print("The time is:", ctime(time()))
        prev_time = the_time
With some output being:
The time is: Sun Aug 20 13:40:04 2000
The time is: Sun Aug 20 13:40:05 2000
The time is: Sun Aug 20 13:40:06 2000
The time is: Sun Aug 20 13:40:07 2000
Traceback (innermost last):
  File "clock.py", line 5, in ?
    the_time = ctime(time())
KeyboardInterrupt
The output is infinite of course so I canceled it (or the output at least continues until Ctrl+C is pressed). The program just does a infinite loop and each time checks to see if the time has changed and prints it if it has. Notice how multiple names after the import statement are used in the line from time import time, ctime.

The Python Library contains many useful functions. These functions give your programs more abilities and many of them can simplify programming in Python.

Also, finally, we can explain the code fragment we have been using for programs with input:

import sys
if sys.version_info.major == 2:
    input = raw_input

The module sys includes a variable for the which version of Python is running, so we import it. Then we use an if statement to switch raw_input to input if we are still in Python 2.

Exercises

Rewrite the high_low.py program from section 5.2 to use the last two digits of time at that moment to be the 'random' number.