- Python Libraries
- Python Services
- The String Group
- Miscellaneous
- Generic Operational System
- Optional Operational System
- Debugger
- Profiler
- Internet Protocol and Support
- Internet Data Handling
- Restricted Execution
- Multimedia
- Cryptographic
- UNIX Specific
- SGI IRIX Specific
- Sun OS Specific
- MS Windows Specific
- Macintosh Specific
- Undocumented Modules
- Summary
Miscellaneous
This group handles many functions that are available for all Python versions.
math
The math module provides standard mathematical functions and constants. It doesn't accept complex numbers, only integers and floats. Check out the following example:
import math >>> math.cos(180) -0.598460069058 >>> math.sin(90) 0.893996663601 >>> math.sqrt(64) 8.0 >>> math.log(10) 2.30258509299 >>> math.pi # The mathematical constant pi 3.14159265359 >>> math.e # The mathematical constant e 2.71828182846
cmath
The cmath module also provides standard mathematical functions and constants. However, its implementation enables it to accept complex numbers as arguments. All the returned values are expressed as complex numbers.
random
The random module generates pseudo-random numbers. This module implements all the randomizing functions provided by the whrandom module plus several pseudo-random real number generators. These random modules aren't very secure for encryption purposes.
random.choice()
It randomly picks one element from list.
basic syntax: random.choice(list)
>>> lst = ["A","l","b","a","t","r","o","s","s","!","!"] >>> while lst: ... element = random.choice(lst) ... lst.remove(element) ... print element, # inserts a linefeed ... b l o A s r ! ! t s a
random.random()
It returns a random floating-point number between 0.0 and 1.0.
basic syntax: random.random()
random.randint()
It returns a random integer n, where x <= N <= y.
basic syntax: random.randint(x,y)
whrandom
The whrandom module provides a Wichmann-Hill floating-point pseudo-random number generator. This module is mostly useful when you need to use multiple independent number generators.
whrandom.whrandom()
This function initializes multiple random generators using the same seed.
>>> import whrandom >>> rga = whrandom.whrandom(2,1,3) >>> rgb = whrandom.whrandom(2,1,3) >>> rga.random() 0.0337928613026 >>> rgb.random() 0.0337928613026
bisect
The bisect module has an array bisection algorithm that provides support for keeping lists in sorted order without the need for sorting them out all the time.
array
The array module is a high efficiency array implementation that handles large lists of objects. The array type is defined at the time of creation.
By using this module, you can create an ArrayType object that behaves exactly like any other list, except that it isn't recommended for storing elements of different types.
>>> import array >>> s = "This is a string" >>> a = array.array("c", s) >>> a[5:7] = array.array("c", "was") >>> print a.tostring() This was a string
Note that NumPy provides a superior array implementation, which can be used for more than just numeric algorithms.
Note that Python 2.0 has improved the array module, and new methods were added to its array objects, including: count(), extend(), index(), pop(), and remove().
ConfigParser
The ConfigParser module is a basic configuration file parser that handles structures similar to those found in the Microsoft Windows INI file.
NOTE
Note that as of Release 2.0, the ConfigParser module is also able to write config files as well as read them.
fileinput
The fileinput module helps you by writing a loop that reads the contents of a file, line by line.
>>> import fileinput >>> for line in fileinput.input("readme.txt"): ... if line.isfirstline: ... print "<< This is the first line >>" ... print "filename = %s" % line.filename ... print " ---------------------------" ... else: ... print "<< This is the line number %d>>" % line.lineno ... print line ...
calendar
The calendar module provides general calendar-related functions that emulate the UNIX cal program, allowing you to output calendars, among other things.
cmd
The cmd module is a simple interface used as a framework for building command line interpreters and shells. You just need to subclass its cmd.Cmd class in order to create your own customized environment.
shlex
The shlex module helps you write simple lexical analyzers (tokenizers) for syntaxes that are similar to the UNIX shell.