Learning How to Work with Modules in Python

Python Tutorials 8 min read
modules in python
Blog / Python Tutorials / Learning How to Work with Modules in Python

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.

Modules are pre-written codes, modules in python

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.

A package is a collection of modules, 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.

Packages are sometimes called libraries, modules in python

data science training

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.

Python's standard library, modules in python

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.

Built in features of a standard library, modules in python

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.

Python has a broad practical application, modules in python

Statisticians may be tempted to install a module comprising a large set of statistical functions and methods.

Statsmodels, modules in python

Whereas, a data scientist will likely use a 24/7 package that helps him organize his data into tables.

vs data analyst package, modules in python

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.

statsmodels can be used by anybody, modules in python

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.

use statsmodels for calculations, modules in python

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.

Why models are useful

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.

sqrt 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:

  1. Type the name of the module “math”
  2. Then write the dot operator
  3. The name of the function of interest, sqrt()
  4. And an argument in parentheses.

import math 4.0

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.

import sqrt 5.0

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.

import sqrt as s 6.0

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.

import as m 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.

import * 8.0

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)?math sqrt

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.

Description of all functions

If we are interested in the meaning of sqrt(), then we could simply type:

help(math.sqrt)

return the square root of 8

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 super practical tutorials!

 

Earn your Data Science Degree

Expert instructions, unmatched support and a verified certificate upon completion!

Leave a Reply

Your email address will not be published.

free data science career guid

You have Successfully Subscribed!

×
Learn Data Science
this Summer!
Get 50% OFF