From HandWiki


A Python module is a file containing Python code. Such files can contain classes, functions and constants. They can be can be grouped together in a package. Packages also serve to create separate namespaces so that two classes of the same name can exist and be used in the same code as long as they belong to different packages.

To start using modules, you will need to import them first using "import" command. We considered already such command at the very start of this tutorial in Sect.Arithmetic Let us repeat the previous example:

<jc lang="python"> import math print math.sqrt(100) </jc>

We imported the entire package "math" with mathematical functions. Or you may consider to import only one function:

<jc lang="python"> from math import sqrt print sqrt(100) </jc>

Alternatively, you simply import all functions (in this case you do not need to type "math." on front of each statement

<jc lang="python"> from math import * print sqrt(100) </jc>

How do we know what is inside the package "math"? You can list all functions and classes implemented in this package calling the build-in function dir():

<jc lang="python"> import math print dir(math) </jc>

Let us consider another module "random" which generates random numbers:

<jc lang="python"> import random print dir(random) </jc>

<jc python member_edit> import os,sys

  1. your code goes here


<jc lang="python"> import os,sys print os.__doc__ </jc>