How Python modules and packages work in Lecture 8. Understand import statements, pip installations, built-in modules, external libraries, and virtual environments with examples, MCQs, and practice problems.
What You Will Learn Today
By the end of this lecture, you will understand:
What modules and packages are
How to import modules
Built-in Python modules
Installing external packages using pip
Understanding virtual environments
How to create your own modules
How to use Python packages in Data Science
MCQs + practice tasks
1) What Are Modules?
A module is simply a Python file containing:
- Functions
- Variables
- Classes
Examples of modules:math, datetime, random, os, json
Using modules allows you to reuse code instead of writing everything from scratch.
2) Importing Modules
Basic import:
import math
print(math.sqrt(25))
Import specific function:
from math import sqrt
print(sqrt(25))
Import with alias:
import math as m
print(m.pi)
Lecture 7 – Lambda, Map, Filter, Reduce
3) Built-in Python Modules (With Examples)
datetime – Working with date/time
import datetime
today = datetime.date.today()
print(today)
random Generate random values
import random
print(random.randint(1, 100))
statistics – Data analysis
import statistics
nums = [10, 20, 30]
print(statistics.mean(nums))
os – Access system commands
import os
print(os.getcwd())
json – Convert Python ↔ JSON
import json
student = {"name": "Ali", "age": 20}
print(json.dumps(student))
4) What Are Packages?
A package is a collection of multiple modules stored inside a folder with an __init__.py file.
Examples of famous Python packages:
- NumPy → mathematical operations
- Pandas → data analysis
- Matplotlib → visualizations
- Requests → APIs
- TensorFlow / PyTorch → deep learning
5) Installing Packages Using pip
pip = Python package installer.
Install:
pip install numpy
Upgrade:
pip install --upgrade numpy
Uninstall:
pip uninstall numpy
Check version:
pip show numpy
6) Importing Packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
7) Real Data Science Examples
Example 1 – Using NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr * 2)
Example 2 – Using Pandas
import pandas as pd
data = {"name": ["Ali", "Sara"], "score": [90, 85]}
df = pd.DataFrame(data)
print(df)
Example 3 – Using Matplotlib
import matplotlib.pyplot as plt
plt.plot([1,2,3], [2,4,6])
plt.show()
Example 4 – Using Requests to call API
import requests
response = requests.get("https://api.github.com")
print(response.status_code)
Example 5 – Using OS to automate files
import os
files = os.listdir()
print(files)
8) Creating Your Own Module
Step 1: Create a Python file
mymath.py
def add(a, b):
return a + b
Step 2: Import it
import mymath
print(mymath.add(3, 4))
9) Understanding Virtual Environments (Important!)
A virtual environment keeps project packages separate.
Create:
python -m venv myenv
Activate (Windows):
myenv\Scripts\activate
Activate (Mac/Linux):
source myenv/bin/activate
Install packages inside it:
pip install numpy
Deactivate:
deactivate
Quiz – Test Your Understanding
Instruction: First read the question and choose your answer. Then scroll down to see the correct answer
A module in Python is:
a) A file
b) A package
c) A class
d) A folder
Answer: a) A file
Which command installs a package?
a) install package numpy
b) python install numpy
c) pip install numpy
d) import numpy
Answer: c) pip install numpy
What will this print?
import math
print(math.pi)
Answer: 3.141592653589793
What is required for a folder to be considered a package?
a) readme.txt
b) init.py
c) requirements.txt
d) activate.bat
Answer: b) init.py
Next Lecture 9 – Object-Oriented Programming
Practice Questions
Instruction for students: Read the question, open your Jupyter Notebook, and try to solve it yourself. After that, come back and check the answer.
import random
print(random.randint(1, 10))
myfile.py
def hello():
print("Welcome to ElecturesAI")
pip install pandas
import pandas as pd
import json
print(json.dumps({"name": "Ali", "age": 20}))
import os
print(os.listdir())




