Home > Articles > Programming > Python

Python Libraries

  • Print
  • + Share This


This group handles many functions that are available for all Python versions.


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)
>>> math.sin(90)
>>> math.sqrt(64)
>>> math.log(10)
>>> math.pi  # The mathematical constant pi
>>> math.e  # The mathematical constant e


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.


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.


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


It returns a random floating-point number between 0.0 and 1.0.

basic syntax: random.random()


It returns a random integer n, where x <= N <= y.

basic syntax: random.randint(x,y)


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.


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()
>>> rgb.random()


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.


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().


The ConfigParser module is a basic configuration file parser that handles structures similar to those found in the Microsoft Windows INI file.


Note that as of Release 2.0, the ConfigParser module is also able to write config files as well as read them.


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


The calendar module provides general calendar-related functions that emulate the UNIX cal program, allowing you to output calendars, among other things.


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.


The shlex module helps you write simple lexical analyzers (tokenizers) for syntaxes that are similar to the UNIX shell.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.