Datasheet
Chapter 1: A Python Primer
12
For example, if a function were defined as follows:
def jimsFunc(age, name = “Jim”):
Then you could call the function in any of the following three ways:
jimsFunc(23)
jimsFunc(42, “James”)
jimsFunc(42, firstName=”Joe”)
In the first example, I simply took the default value for the first parameter; in the second, I replaced it
with “ James. ”
Modules
A module is the highest - level programming unit in Python. A module usually corresponds to a program
file in Python. Unlike in Ruby, modules are not declared — the name of the
*.py file is the name of the
module. In other words, basically each file is a module, and modules import other modules to perform
various programming tasks.
Importing Modules
Importing modules is done with either the import or reload command.
Import
To use a module, you import it. Usually import statements occur at the beginning of the Python
module. Importing modules is a fairly simple operation, but it requires a little explanation. Consider the
following examples:
1. import os
2. import os, sys
3. from os import getcwd
4. import os as operatingSystem
These examples highlight some variations in how you can import modules:
1. This first example is the simplest and easiest to understand. It is merely the keyword import
followed by the module name (in this case,
os ).
2. Multiple modules can be imported with the same import command, with the modules
separated by a comma.
c01.indd 12c01.indd 12 6/2/08 12:03:11 PM6/2/08 12:03:11 PM