There are many features of Python that were not covered in this chapter. Chapter 27 has some information about using the CGI module of Python to write CGI scripts, but for further information about the language you will need to find a book devoted to Python. Several good references are mentioned in the later section “How to Find Out More.”
Table 23–2 lists some of the most important Python functions introduced in this chapter.
Function | Use |
---|---|
| Print a string to standard output. |
raw input() | Read a string from standard input. |
import | Load a module. |
rstrip() | Remove trailing white space from a string. Other string methods: lower(), center(), split(), join(), find(), replace(), count() |
sort() | Sort the items in a list. Other list methods: reverse(), append(), extend(), insert(), pop() |
keys() | Get a list of keys in a dictionary. Use has_key() to test for keys. |
len() | Get the length of a string, list, or dictionary. |
del | Delete an element from a list or dictionary. |
range() | Generate a list of integers. |
if . . . elif . . . else | Conditional statement. |
for . . . in | Loop through the elements in a list. |
while | Loop while a condition is true. Can use break to exit. |
open() | Open a file. File methods: read(), readline(), readlines(), write(), writelines(), close(). |
def | Define a procedure. |
return | Exit from a procedure, returning a value. |
class | Define a class. |
try . . . except | Catch exceptions. |
Table 23–3 lists the Python modules mentioned in this chapter. See the Python documentation at http://docs.python.org/modindex.html for details.
Module | Use |
---|---|
sys | Standard 1/0, command-line arguments |
fileinput | Iterate through files, especially command-line arguments |
getopt | Parse command-line options |
os | UNIX commands and files |
shutil | Copy files |
re | Regular expressions |
math | Mathematical functions |
cmath | Complex number support |
random | Generate random numbers |
time | System time and date |
CGI | CGI scripting |