Modules in Python may become your best friend because their purpose is to make it easier for you to program.
“Effort is important, but knowing where to make an effort makes all the difference!”
When programming, is there anything better than using something that you have not particularly written yourself? However, before you read on, keep in mind that this article may well refer to the concept of object-oriented programming every now and again.
What are Modules in Python?
A module is pre-written code containing definitions of variables, functions, and classes.
Its charm lies in the fact that we can load it in all new programs, and don’t need to rewrite the code manually at the beginning of a new program.
What are Packages in Python?
A package is a collection, or directory, of related modules in Python.
Technically, you could create your own modules and packages, but things are easier than that. People in the programming community have developed a growing set of packages that are readily available for download and tailored for various types of professions and specializations.
Side note: Sometimes, people use the term library when referring to a package, so keep in mind that the two can be used interchangeably. That said, if you're curious to learn which are the Python libraries you simply can't live without, you can read more in our Ultimate Python Programming Guide.
Python’s Standard Library
Python’s standard library is a collection of modules available as soon as you install Python. For instance, we can use the built-in function len() directly because it is in this library. We don’t have to write down code manually and count the number of elements in an object. Similarly, we can use methods from the “list” class to extend a list, and we don’t have to define a method on our own before applying it.
This is because Python’s developers have prepared a certain amount of code which runs automatically when you start a new notebook file. There is a set of modules there, at the beginning of your program; you cannot see it, but it contains all those built-in features like the len() function or the “list” class. Namely, these modules form the standard library.
Which Modules in Python Do We Need to Install?
Python has a broad practical application. To use the language, it is not mandatory to install all the modules in Python. So, besides using the standard library that comes with the normal installation, people only download the modules and packages they need.
Statisticians may be tempted to install a module comprising a large set of statistical functions and methods.
Whereas, a data scientist will likely use a 24/7 package that helps him organize his data into tables.
Important: Don’t expect to create modules in Python all the time. In fact, you may never need to create a module on your own. It is true that this is one of the best features of Python, but this is typically carried out by highly specialized experts. For example, some developers will create a certain statistical module that anyone can import and use.
If you are a statistician, you will be happy to take advantage of this statistical module. In most cases, it will be free, and you can download and use it for your calculations.
Improving Modules in Python
Once a module has been released, it can still be improved and updated. Frequently, developers team up with statisticians to do that.
Modules in Python are a dramatic improvement in their efficiency. Once established and proven to be functional, a module can be easily reused multiple times facilitating the work of thousands of users.
How to Import Modules in Python?
There are four ways to import a module. You can decide which one you will use when working on your own.
Let’s learn how to import the “math” module. It contains multiple mathematical functions. Among them is the sqrt() function, which calculates the square root of an argument.
By default, this module isn’t a part of Python’s standard library, but if you are using Jupyter it is installed with the whole Anaconda Package. So, to use its features, you must import it. Let’s see the four available options to do that.
The First Way to Import Modules in Python
You can type:
import math
Now, the code of the “math” module will run. To apply the sqrt() function properly, we should respect the following syntax:
- Type the name of the module “math”
- Then write the dot operator
- The name of the function of interest, sqrt()
- And an argument in parentheses.
The Second Way to Import Modules in Python
If you know in advance that you won’t need all the functions and classes provided in the “math” module and that sqrt() is the only function you’ll use, you could type:
from math import sqrt()
The syntax will allow you to omit the name of the module and the dot operator when calling this function. We can make sure it is working properly by testing it with the number 25.
The Third Way to Import Modules in Python
The third option we’ll see is the one experts probably love the most. In advanced bits of code, you will see how modules or their functions can be renamed simply because using shorter names can be time-saving.
If we write
from math import sqrt() as s
we can use s() instead of sqrt(). When it is applied to 36, this function will return the value of 6.
We can rename modules, too. Let’s re-do the first option.
Try typing
import math
again and add
as m
Accordingly, m.sqrt(49) will result in 7.0.
You may not immediately realize how useful this is, but here are some common examples:
import numpy as np
and
import pandas as pd
and
import matplotlib.pyplot as plt
These are three very common packages used in data science. Whenever you are using a method from them, you prefer typing:
plt.show()
instead of
matplotlib.pyplot.show()
right?
Moreover, commonly used libraries have established abbreviations (such as np, pd, plt, etc.)
The Fourth Way to Import Modules in Python
The fourth option is usually frowned upon by specialists but can be appropriate for interactive sessions. We can type:
from math import *
And all the features from “math” – functions, classes or methods - will be imported. Interpret it as import everything from math. To verify we did things right, let’s apply sqrt() to 64.
The Problem with the Fourth Way to Import Modules
The problem is that this option could be problematic for Python in some situations. Suppose you imported everything from “math” and then you imported a second module that contains an sqrt() function. And what if the second module uses the function differently? Which of the two should the software apply when you type sqrt(81)?
Python will choose one of the two functions, but you won’t be able to select which one. This is a sign of unprofessional coding, so experts avoid this when working with longer files.
The help() Function
In addition, for further reference, you can read about the features related to the “math” module. You can achieve this by typing help(math). A description of all the functions will follow.
If we are interested in the meaning of sqrt(), then we could simply type:
help(math.sqrt)
What Are the Benefits of Using Modules in Python?
If you are not a fan of re-doing the exact same work, modules in Python should be right up your street. By simply importing a particular module, you can save yourself quite some time by not writing the logic behind a couple of functions. There are four ways you can import modules in Python and all of them are more appropriate in certain cases. Provided that you are not sure what the features of a specific module are, you can change that by using the help() function. If you want to learn how conditionals work in Python check out our article on them.
***
If you’re enthusiastic about boosting your Python knowledge, check out our Introduction to Python course and Python Programmer Bootcamp.