Python  

Modules, Packages and Library In Python

In the last session, we discussed the Python functions Operations. Now, we will look at Modules, Packages and Library In Python methods and Operations. This is the most important topic in Python.

Importing modules and packages in Python allows you to organize your code, reuse functionalities, and keep our projects clean and manageable. By understanding how to import modules, specific functions, and use relative imports within packages, we can structure our Python applications more effectively.

Let’s start,

1. Math module operations - Example 1

#Input
import math
math.sqrt(16)

#Output
4.0

2. Math module operations - Example 2

#Input
from math import sqrt,pi
print(sqrt(16))
print(sqrt(25))
print(pi)

#Output
4.0
5.0
3.141592653589793

3. Math module operations - Example 3

#Input
from math import *
print(sqrt(16))
print(pi)

#Output
4.0
3.141592653589793

4. Math module operations - Example 4

Here package is a new file name and addition is a function name.

#Input
from package.maths import addition
addition(2,3)

#Output
5

5. Math module operations - Example 5

Here package is a new file name and addition is a function name.

#Input
from package import maths
maths.addition(2,3)

#Output
5

6. Standard Library Overview

Python's Standard Library is a vast collection of modules and packages that come bundled with Python, providing a wide range of functionalities out of the box. Here's an overview of some of the most commonly used modules and packages in the Python Standard Library.

#Input
import array
arr=array.array('i',[1,2,3,4])
print(arr)

#Output
array('i', [1, 2, 3, 4])

7. Random function methods

#Input
import random
print(random.randint(1,10))
print(random.choice(['apple','banana','cherry']))

#Output
2
cherry

8.File And Directory Access

#Input
import os
print(os.getcwd())

#Output
C:\python\Modules

9. Shutil Operations in Python

Example File names : source.txt and destination.txt

#Input
import shutil
shutil.copyfile('source.txt','destination.txt')

#Output
'destination.txt'

10. Data Serialization in python

#Input
import json
data={'name':'Krish','age':25}

json_str=json.dumps(data)
print(json_str)
print(type(json_str))

parsed_data=json.loads(json_str)
print(parsed_data)
print(type(parsed_data))

#Output
{"name": "Krish", "age": 25}
<class 'str'>
{'name': 'Krish', 'age': 25}
<class 'dict'>

11. CSV file operations in python

#Input
import csv

with open('example.csv',mode='w',newline='') as file:
    writer=csv.writer(file)
    writer.writerow(['name','age'])
    writer.writerow(['Krish',32])

with open('example.csv',mode='r') as file:
    reader=csv.reader(file)
    for row in reader:
        print(row)

#Output
['name', 'age']
['Krish', '32']

12. Datetime operations in python

#Input
from datetime import datetime,timedelta
now=datetime.now()
print(now)

yesterday=now-timedelta(days=1)
print(yesterday)

#Output
2025-11-13 22:39:30.849440
2025-11-12 22:39:30.849440

13. Time Operations in python

#Input
import time
print(time.time())
time.sleep(2)
print(time.time())

#Output
1718086104.8242216
1718086106.82563

14. Regular expression

#Input
import re
pattern=r'\d+'
text='There are 123 apples 456'
match=re.search(pattern,text)
print(match.group())

#Output
123

Now, We have covered advanced Modules, Packages and Library In Python with various examples and methods. In the next article, we will dive into File handiing in Python.