Everything you want to know about Modules in Python

In this blog, we would try to learn about what is library/module in python, we will discuss all the concepts with examples, so let’s start.

Modules are the files with the .py extension which contain Python code that can be imported inside another Python Program for different use cases.

Python has already some inbuilt libraries such as math, random. Every module has different methods and features. The math module has some functions related to mathematics such as, sin(), cos() and sqrt() etc. The modules available in the standard library are created by the python core programming team. They are used to perform different types of important tasks.

The standard library in Python is a set of built-in modules that are part of the core programming language.

Modules in Python are just a kind of program. Modules contain functions and classes.

In general python, two types of modules are used.

  • User-defined libraries/modules (defined by the user for specific needs).
  • Built-in libraries/modules (defined by python core team)

Let’s learn about them one by one.

User defined modules

Such modules are created by the user. A User creates modules according to his needs. If the user wants to perform arithmetic operations, then he can create a new module for this feature.

How to create module in python ?

We will learn to create a module in python. for that, An example is given below to elaborate “How to create a module in python?”. Its file name is 'greet.py'. The name of the file is the same as the ‘module’. The functions named hello_greet, welcome_greet, hey_greet and hi_greet are given in the ‘greet’ module.

Create a file named as greet.py and add the below code.

def hello_greet():
    return "HELLO"

def welcome_greet():
    return "WELCOME"

def hey_greet():
    return "HEY

def hi_greet():
    return "HI"

In the above example, we have added different functions to greet a person in different ways. The next step is to import module.

How to import module in python ?

Assume you bought a car and until you don’t bring that car on the road, you can’t make use of it. Similarly, we need to bring our module by importing so that we can use it.

If you want to import a module on the current program in Python, then the import keyword has to be used. After importing that particular module, we can access all the methods/functions and classes of the module.

Syntax

import library_name

Example

import greet

How to use functions of module ?

If you want to use the functions of a module then you have to call the function from the module.

After importing the module, we call the function. To call a function, We use a dot . operator.

Syntax

<module_name>.<function(arguments)>

Example

import greet

print(greet.hello_greet())
print(greet.welcome_greet())
print(greet.hey_greet())
print(greet.hi_greet())

# Output : 
HELLO
WELCOME
HEY
HI

In the above example, we are calling all the functions of the greet module using dot . operator.

There is another way to import module and functions using from keyword. It helps to call a particular function from the module. If the class or function is needed to be accessed on the current program without the module name, then the from statement is used.

Syntax

from <module_name> import <function1, function2,.... functionN>

Let’s take an example that we want to import only two functions from the greet module i.e., hello_greet() and welcome_greet()

from greet import hello_greet, welcome_greet

print(hello_greet())
print(welcome_greet())

Import using * (asterisk)

If you want to import all the classes and functions of a module then we use * () to access all the classes and functions.

Example

from greet import * # Now we can access all functions of greet module

as statement

Sometimes module names will be larger then typing that name again and again will make our code ugly. In such a case, we use as statement. If you want to change the name of the module, then the as statement is used. It gives an alias name to the module.

Syntax

import <module_name> as <alias>

For example, we would change the greet module name to gr

import greet as gr

print(gr.hello_greet())

# Output :
HELLO

How to delete imported module ?

Whenever you want to remove an imported module then the del statement is used.

Syntax

del module/library_name

Example

import greet # Import statement

print(greet.hello_greet())  # calling function
del greet # removing import
print(greet.hello_greet()) # calling function

In the above example,

  • At first, we have imported the greet library/module
  • Next, we are calling a function from the greet module
  • then, we are removing the greet library from the program
  • then, we are trying to use hello_greet() from the greet library. It would show an error. because we have removed the greet library from our so now it is not accessible. It will show the below error.
print(greet.hello_greet())
NameError: name 'greet' is not defined
  • Above error is showing a name error. It means that the name ‘greet’ is not available for use or it is not defined

Built-in module

There are various built-in modules are available in python. These modules are made for different purposes. Python’s library is very huge. There are many modules in this library in which many functions are defined.

Some modules are being explained below which as a programmer you will use regularly.

Math module

Math module is used for mathematical operations like ceil, floor, factorial etc. Some examples are explained below.

import math

print(math.ceil(1.3))
print(math.ceil(1.5))
print(math.ceil(1.7))

print(math.floor(1.3))
print(math.floor(1.5))
print(math.floor(1.7))

print(math.factorial(5))
print(math.factorial(7))

# Output :
2
2
2
1
1
1
120
5040

Explanation:

  • ceil is used to find nearest upward rounded number.
  • floor is used to find nearest downward rounded number.
  • factorial is used to find factorial of number. factorial is multiply of all numbers from 1 to the number for which we are going to find factorial.

Similarly we have various modules are in python like random, sis, statistics, os etc.

How to access module name ?

Just like the function is accessed with the help of module, similarly the names of modules are accessed.

Syntax

module_name.<attribute>

# To find name of the module. we use __name__ attribute

print(greet.__name__)

# Output : greet

Conclusion

In the above information, we read about modules in python which are the files with the .py extension which contain Python code that can be imported inside another Python Program for different use cases.

We have also gone through types of modules in python i.e., built-in and user-defined.

We also learned about the various concept and use cases like –

  • module creation
  • import module
  • how to use functions and classes of a module
  • Import using * (asterisk)
  • as statement
  • module deletion
  • access module name

That’s all about module in python.


Up ↑

%d bloggers like this: