Working with libraries¶
##3 Most of the power of a programming language is in its libraries.
A library is a collection of files (called modules) that contains functions for use by other programs.
May also contain data values (e.g., numerical constants) and other things.
Library’s contents are supposed to be related, but there’s no way to enforce that.
The Python [standard library][stdlib] is an extensive suite of modules that comes with Python itself.
Many additional libraries are available from [PyPI][pypi] (the Python Package Index).
We will see later how to write new libraries.
Most of the power of a programming language is in its libraries.¶
A library is a collection of files (called modules) that contains functions for use by other programs.
May also contain data values (e.g., numerical constants) and other things.
Library’s contents are supposed to be related, but there’s no way to enforce that.
The Python [standard library][stdlib] is an extensive suite of modules that comes with Python itself.
Many additional libraries are available from [PyPI][pypi] (the Python Package Index).
We will see later how to write new libraries.
Note
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 module’s name.
math.cos(pi)
won’t work: the reference topi
doesn’t somehow “inherit” the function’s reference tomath
.
Use help
to learn about the contents of a library module.¶
Works just like help for a function.
help(math)
Import specific items from a library module to shorten programs.¶
Use
from ... import ...
to load only specific items from a library module.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 module 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., the
matplotlib
plotting library is often aliased asmpl
.
But can make programs harder to understand, since readers must learn your program’s aliases.
Exercise: Exploring the Math Module
What function from the
math
module can you use to calculate a square root without usingsqrt
?Since the library contains this function, why does
sqrt
exist?
See Solution
Using
help(math)
we see that we’ve gotpow(x,y)
in addition tosqrt(x)
, so we could usepow(x, 0.5)
to find a square root.The
sqrt(x)
function is arguably more readable thanpow(x, 0.5)
when implementing equations. Readability is a cornerstone of good programming, so it makes sense to provide a special function for this specific common case. Also, the design of Python’smath
library has its origin in the C standard, which includes bothsqrt(x)
andpow(x,y)
, so a little bit of the history of programming is showing in Python’s function names.
Exercise: Locating the right Module
You want to select a random character from a string:
bases = 'ACTTGCTTGAC'
Which [standard library][stdlib] module could help you?
Which function would you select from that module? Are there alternatives?
Try to write a program that uses the function.
See Solution
The [random module][randommod] seems like it could help you.
The string has 11 characters, each having a positional index from 0 to 10.
You could use random.randrange
function (or the alias random.randint
if you find that easier to remember) to get a random integer between 0 and 10, and then pick out the character at that position:
from random import randrange
random_index = randrange(len(bases))
print(bases[random_index])
from random import randrange
print(bases[randrange(len(bases))])
Perhaps you found the random.sample
function? It allows for slightly less typing:
from random import sample
print(sample(bases, 1)[0])
Note that this function returns a list of values. There’s also other functions you could use, but with more convoluted code as a result.