Libraries
Overview
Teaching: 10 min
Exercises: 5 minQuestions
How can I use software that other people have written?
How can I find out what that software does?
Objectives
Explain what software libraries are and why programmers create and use them.
Write programs that import and use libraries from Python’s standard library.
Find and read documentation for standard libraries interactively (in the interpreter) and online.
Most of the power of a programming language is in its libraries.
- A library is a collection of functions that can be used by other programs.
- May also contain data values (e.g., numerical constants).
- Library’s contents are supposed to be related, but there’s no way to enforce that.
- Python’s standard library is installed with it.
- Many additional libraries are available from PyPI (the Python Package Index).
- We will see later how to write new libraries.
A program must import a library in order to use it.
- Use
importto load a library into a program’s memory. - Then refer to things from the library as
library_name.thing_name.- Python uses
.to mean “part of”.
- Python uses
import math
print('pi is', math.pi)
print('cos(pi) is', math.cos(math.pi))
pi is 3.141592653589793
cos(pi) is -1.0
- Have to refer to each item with the library’s name.
math.cos(pi)won’t work: the reference topidoesn’t somehow “inherit” the function’s reference tomath.
Use help to find out more about a library’s contents.
- Works just like help for a function.
help(math)
Help on module math:
NAME
math
MODULE REFERENCE
http://docs.python.org/3.5/library/math
The following documentation is automatically generated from the Python
source files. It may be incomplete, incorrect or include features that
are considered implementation detail and may vary between Python
implementations. When in doubt, consult the module reference at the
location listed above.
DESCRIPTION
This module is always available. It provides access to the
mathematical functions defined by the C standard.
FUNCTIONS
acos(...)
acos(x)
Return the arc cosine (measured in radians) of x.
⋮ ⋮ ⋮
Import specific items from a library to shorten programs.
- Use
from...import...to load only specific items from a library. - Then refer to them directly without library name as prefix.
from math import cos, pi
print('cos(pi) is', cos(pi))
cos(pi) is -1.0
Create an alias for a library when importing it to shorten programs.
- Use
import...as...to give a library a short alias while importing it. - Then refer to items in the library using that shortened name.
import math as m
print('cos(pi) is', m.cos(m.pi))
cos(pi) is -1.0
- Commonly used for libraries that are frequently used or have long names.
- E.g.,
matplotlibplotting library is often aliased asmpl.
- E.g.,
- But can make programs harder to understand, since readers must learn your program’s aliases.
Locating the Right Library
You want to select a random value from your data:
ids = [1, 2, 3, 4, 5, 6]
- What standard library would you most expect to help?
- Which function would you select from that library? Are there alternatives?
Exploring the Math Library
- What function from the
mathlibrary can you use to calculate a square root without usingsqrt?- Since the library contains this function, why does
sqrtexist?
When Is Help Available?
When a colleague of yours types
help(math), Python reports an error:NameError: name 'math' is not definedWhat has your colleague forgotten to do?
Importing With Aliases
- Fill in the blanks so that the program below prints
90.0.- Rewrite the program so that it uses
importwithoutas.- Which form do you find easier to read?
import math as m angle = ____.degrees(____.pi / 2) print(____)
Importing Specific Items
- Fill in the blanks so that the program below prints
90.0.- Do you find this easier to read than preceding versions?
- Why would’t programmers always use this form of
import?____ math import ____, ____ angle = degrees(pi / 2) print(angle)
Key Points
Most of the power of a programming language is in its libraries.
A program must import a library in order to use it.
Use
helpto find out more about a library’s contents.Import specific items from a library to shorten programs.
Create an alias for a library when importing it to shorten programs.