Home » Chapter 7 : Libraries
Library Concepts
In programming, a library is a collection of prewritten, reusable, code that programmers can use to optmize tasks. These collections usually target specific categories of tasks, like mathematics, text analysis, mapping, etc. Programmers use libraries for a number of reasons, such as:
Python, like many programming languages, has a rich ecosystem of libraries available for use. There are libraries that are part of the Python language installation, called the Python Standard Library, and there are thousands of External Libraries available from various sources. The primary repository of external libraries is located online at pypi.org.
To use a library in Python, you use the import statement in your program along with the name of the library of interest. For example, one of the libraries in the Standard Python Library is called math which includes over 50 mathematical functions you can use in your programs. Here's a code example of using the math library:
import math
x = 9
y = 2
print(math.sqrt(x))
print(math.pow(x, y))
print(math.pow(x, (math.sqrt(x))))
And the output:
3.0
81.0
729.0
Additional details about the Standard Python Library and External Libraries are provided on the next couple of pages.