The import
Statement¶
The Python Standard Library has a bunch of modules we can import and use in our Python code. These come built into Python. You can think of a module as a code library which contains all sort of functions and tools to use in your application code. Another thing that makes Python so amazing is all the external libraries you can install and import into your code from The Python Package Index (PyPI).
First we need to learn to import modules and libraries into our code. We will begin with examples of modules that are built into the The Python Standard Library. That is, we do not have to install any external packages. They simply come along with your installation of Python.
Let’s start with a simple example. Suppose we want to take the square root of a number. The square root of a number \(x\) is a number \(y\) such that \(y \cdot y = x\). For example, the square root of 9 is 3 because 3 multiplied by 3 is 9. The square root of 16 is 4 because 4 multiplied by 4 is 16.
Python has a built-in module that you can use for mathematical tasks and is called the math
module. We can use it to perform many mathematical operations and here we will first use
it to compute the square root. We begin by importing the math
module by using the import
statement.
import math
math.sqrt(16)
4.0
math.sqrt(4)
2.0
math.sqrt(49)
7.0
math.sqrt(50)
7.0710678118654755
There are all sorts of cool functions and constants we can use from the math module
Here is the famous mathematical constant Pi.
math.pi
3.141592653589793
Here is the factorial function
math.factorial(5)
120
When we use import math
we have access to all the “stuff” (functions, constants, etc) from the math
module. We simply just have to use math.<name_of_tool>
. If you want to import
only one function or object you can do it like this.
from math import pi
pi
3.141592653589793
from math import pi, sqrt, factorial
pi
3.141592653589793
sqrt(49)
7.0
factorial(6)
720
But in this case, it’s more readable to just import math
and then use the dot notation
math.
to access whatever tool you require.
Lets import some other modules that comes built into the Python standard library.
import time
time.sleep(2) # waits for 2 seconds
from datetime import date
date.today() # will return the date that this code is executed on
datetime.date(2021, 1, 23)
Earlier in the course we used the random
module. It has all
sorts of cool stuff
import random
numbers = [i for i in range(10)]
x = random.choice(numbers)
x
0
x = random.choice(numbers)
x
4
x = random.choice(numbers)
x
3
x = random.choice(numbers)
x
1
random.shuffle(numbers)
numbers
[9, 7, 8, 3, 2, 6, 4, 5, 1, 0]
random.shuffle(numbers)
numbers
[0, 7, 5, 8, 1, 3, 2, 4, 9, 6]
sample_one = random.sample(numbers, 5)
sample_one
[5, 6, 8, 0, 9]
sample_two = random.sample(numbers, 5)
sample_two
[6, 3, 8, 7, 1]
There is lot more that could be said about the import
statement but for now
I just want you to know how to import a module and how to import a particular function or
object from a module.