Learning Objectives
- Be able to import entire libraries.
- Be able to import certain members of a library.
- Be able to create a library.
- Understand how a library’s namespace functions.
Libraries
Python includes code that has already been written for you. For example, we do not need to write how print works. There is much more code stored in libraries. Many functions are built-in to Python, such as print and input, but others are stored in modules, which we can import into our program.
One of the most used examples is the math module. This module contains the trigonometric functions, such as tan, sin, and cos. It is important to know which library contains which functions and/or constants. You can see the Python library reference here: https://docs.python.org/3/library/index.html
Syntax
There are two ways to import libraries into our code. We can import the entire library as a package. We use the import
keyword to import these libraries.
import math a = float(input("Enter a number in radians: ")) b = math.sin(a) print("The sine of {} is {}".format(a, b))
The import keyword takes a parameter, which is the library we want to import. The link I provided above contains a lot of libraries that you can import. The example above shows the math library as you can see below:

You can see the members in this module here: https://docs.python.org/3/library/math.html
Accessing Members
Members can be accessed by using the name of the module followed by the member access operator (the dot operator).
We can also use the from keyword to import certain functions into your program. Notice that if we use import, we import the entire library and access it through the name of the module, such as math.sin.
from math import sin a = float(input("Enter a value in radians: ")) b = sin(a) print("The sine of {} is {}.".format(a, b))
Notice that when we use the from keyword, we import the function (or functions) into our program. Notice the lack of math. in front of sin.
Directory of a Library
When we look at a new library, it is helpful to look at the documentation, but also, we can see what was imported into Python by using the dir() function.
>>> import random >>> dir(random) ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_floor', '_inst', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randbytes', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate'] >>>
In the case above, I imported the random module. We can see when I type dir(random), it shows me a ton of different functions that we can use. There is also a help that will give us the documentation of the module. Unfortunately, computer scientists are not very good at documenting their code, so it’s a gamble on whether the documentation is good.
>>> help(random.shuffle) Help on method shuffle in module random: shuffle(x, random=None) method of random.Random instance Shuffle list x in place, and return None. Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the standard random.random will be used. >>>
When I type help(random.shuffle), Python will tell me what that function is, what the parameters are, and then some documentation on what the specific function is supposed to do.
How to use a Third-party Module
A module is code packaged together that was written by someone else. This makes our lives much easier, but we have to know how that programmer wrote the code. Mainly, we need to know the names of the functions, what parameters they take, and what they return to be able to use them. The Python documentation shows all of the standard modules that comes packaged and shipped with Python. This can be seen here: https://docs.python.org/3/library/index.html.
Certain modules, such as NumPy and Pandas was not written by the same people who write Python. Therefore, they have their own documentation: https://numpy.org/doc/stable/reference/index.html.