Overview
Teaching: 10 min
Exercises: 10 minQuestions
How can I use Python’s standard libraries?
Where do I find documentation on Python’s standard libraries?
Objectives
Use
import
to load entire libraries and elements of libraries.Use
import
to load libraries under aliases.Use elements of libraries via dot notation.
Use the
math
andrandom
libraries.Use the
csv
library to read CSV files.
import
to load a library into a program’s memory.library_name.thing_name
.import math
print('pi is', math.pi)
print('cos(pi) is', math.cos(math.pi))
pi is 3.141592653589793
cos(pi) is -1.0
help
to find out more about a library’s contents.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.
⋮ ⋮ ⋮
from...import...
to load only specific items from a library.from math import cos, pi
print('cos(pi) is', cos(pi))
cos(pi) is -1.0
import...as...
to give a library a short alias while importing it.import math as m
print('cos(pi) is', m.cos(m.pi))
cos(pi) is -1.0
matplotlib
plotting library is often aliased as mpl
.Exploring the Math Library
- What function from the
math
library can you use to calculate a square root without usingsqrt
?- Since the library contains this function, why does
sqrt
exist?
Locating the Right Library
You want to select a random character from a string:
bases = 'ACTTGCTTGAC'
- What standard library would you most expect to help?
- Which function would you select from that library? Are there alternatives?
When Is Help Available?
When a colleague of yours types
help(math)
, Python reports an error:NameError: name 'math' is not defined
What 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
import
withoutas
.- 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)
Checking Random Numbers
Look up the documentation for the
random
library, then write a short program that generates a large number of samples from the normal distribution with mean 0.0 and standard deviation 1.0 and see how close the sample average comes to 0.0.
Reading Comma-Separated Values
Look up the documentation for the
csv
library and use it to read a file containing tabular data in comma-separated values (CSV) format. Why would you use a library like this rather than just reading lines and splitting on the commas?
Key Points
Use
import
to load a library.Use dot notation to get library’s contents.
The
math
library has common mathematical functions.The
random
library produces pseudo-random numbers.The
csv
library can read CSV files correctly.