# 22 Python Tricks

• Print
This chapter is from the book

## 4.6 A Guided Tour of Python Packages

Thousands of other packages are available if you go to python.org, and they are all free to use. The group of packages in Table 4.1 is among the most useful of all packages available for use with Python, so you should be sure to look them over.

The re, math, random, array, decimal, and fractions packages are all included with the standard Python 3 download, so you don’t need to download them separately.

The numpy, matplotlib, and pandas packages need to be installed separately by using the pip or pip3 utility. Later chapters, starting with Chapter 12, cover those utilities in depth.

#### Table 4.1. Python Packages Covered in This Book

Name to import

Description

re

Regular-expression package. This package lets you create text patterns that can match many different words, phrases, or sentences. This pattern-specification language can do sophisticated searches with high efficiency.

This package is so important that it’s explored in both Chapters 6 and 7.

math

Math package. Contains helpful and standard math functions so that you don’t have to write them yourself. These include trigonometric, hyperbolic, exponential, and logarithmic functions, as well as the constants e and pi.

This package is explored in Chapter 11.

random

A set of functions for producing pseudo-random values. Pseudo-random numbers behave as if random—meaning, among other things, it’s a practical impossibility for a user to predict them.

This random-number generation package includes the ability to produce random integers from a requested range, as well as floating-point numbers and normal distributions. The latter cluster around a mean value to form a “bell curve” of frequencies.

This package is explored in Chapter 11.

decimal

This package supports the Decimal data type, which (unlike the float type) enables you to represent dollars-and-cents figures precisely without any possibility of rounding errors. Decimal is often preferred for use in accounting and financial applications.

This package is explored in Chapter 10.

fractions

This package supports the Fraction data type, which stores any fractional number with absolute precision, provided it can be represented as the ratio of two integers. So, for example, this data type can represent the ratio 1/3 absolutely, something that neither the float nor Decimal type can do without rounding errors.

This package is explored in Chapter 10.

array

This package supports the array class, which differs from lists in that it holds raw data in contiguous storage. This isn’t always faster, but sometimes it’s necessary to pack your data into contiguous storage so as to interact with other processes. However, the benefits of this package are far exceeded by the numpy package, which gives you the same ability, but much more.

This package is briefly covered in Chapter 12.

numpy

This package supports the numpy (numeric Python) class, which in turn supports high-speed batch operations on one-, two-, and higher-dimensional arrays. The class is useful not only in itself, as a way of supercharging programs that handle large amounts of data, but also as the basis for work with other classes.

This package is explored in Chapters 12 and 13. numpy needs to be installed with pip or pip3.

numpy.random

Similar to random, but designed especially for use with numpy, and ideally suited to situations in which you need to generate a large quantity of random numbers quickly. In head-to-head tests with the standard random class, the numpy random class is several times faster when you need to create an array of such numbers.

This package is also explored in Chapter 12.

matplotlib.pyplot

This package supports sophisticated plotting routines for Python. Using these routines, you can create beautiful looking charts and figures—even three-dimensional ones.

This package is explored in Chapter 13. It needs to be installed with pip or pip3.

pandas

This package supports data frames, which are tables that can hold a variety of information, as well as routines for going out and grabbing information from the Internet and loading it. Such information can then be combined with the numpy and plotting routines to create impressive-looking graphs.

This package is explored in Chapter 15. It also needs to be downloaded.