Understanding Modules in Python

Modules in Python can be defined as a Python file that contains Python definitions and statements. It contains Python code along with Python functions, classes, and Python variables. The Python modules are files with the .py extension. Python has the import keyword for this purpose. It provides reusability of code.
Example

import math 

print (“Square root of 100:”, math.sqrt(100))

Output: Square root of 100: 10.0

Types of Python Modules

There are two types of Python modules:

  1. Built-In modules in Python
  2. User-Defined Modules in Python

Built-In Modules:

Python’s standard library comes bundled with a large number of modules. They are called built-in modules. There are several built-in modules in Python, which you can import whenever you like.

os

sys

math

datetime

collections

statistics

random and many more.

Examples:

  • import platform

x = platform.system()

print(x)

Output: Windows

  • import random

x = random.randint(10,50)

print(x)

Output: 46

  • import math

 

x = math.cos(60)

y = math.sin(30)

z = math.tan(1)

pie = math.pi

print(f”Value of cos60 is: {x}”)

print(f”Value of sin30 is: {y}”)

print(f”Value of tan1 is: {z}”)

print(f”Value of pie is: {pie}”)

Output

Value of cos60 is: -0.9524129804151563

Value of sin30 is: -0.9880316240928618

Value of tan1 is: 1.5574077246549023

Value of pie is: 3.141592653589793

User-Defined Modules

To create a module just save the code you want in a file with the file extension .py:

  • Save this code in a file named mymodule.py

def greeting(name):

print(“Hello, ” + name)

Now we can use the module we just created, by using the import statement:

import mymodule

mymodule.greeting(“Anas Shafiq”)

Output: Hello, Anas Shafiq

  • create a module. Type the following and save it as example.py.

def add(a, b):

result = a + b

return result

We use the import keyword to do this. To import our previously defined module example, we type the following in the Python prompt.

import example

addition.add(4,5) 

Output: 9

Variables in Python Modules

We have already discussed that modules contain functions and classes. But apart from functions and classes, modules in Python can also contain variables in them. Variables like tuples, lists, dictionaries, objects, etc.

Save this code in the file xyz.py

p1= {

  “name”: “Anas”,

  “age”: 25,

  “country”: “Pakistan”

}

Import the module named xyz, and access the p1 dictionary:

Import xyz

a = mymodule.person1[“country”]

print(a)

Output: Pakistan

Renaming the Module

Python provides an easy and flexible way to rename our module with a specific name and then use that name to call our module resources. For renaming a module, we use the as keyword.

import math as m

import numpy as np

import random as r

print(m.pi)

print(r.randint(0,10))

print(np.__version__)

Output:

3.141592653589793

10

1.22.0

Conclusion:

In this article, we have learned about modules in Python. There are 2 modules, i.e., Built-In and user-defined modules. We can store functions, classes, and variables in our module.

Visited 10 times, 1 visit(s) today