Exploring Python
Modules & Packages
In-depth with math, random, and matplotlib
Presented by: Muskan Jain, Ankit Chauhan, Paras
Sharma and Yashwant Rajput.
What are Python Modules?
• A module is a .py file with Python code (functions,
classes, etc.)
• Encapsulates reusable code
• Logical organization of large programs
• Examples: math, os, random
What are Packages?
• A collection of Python modules organized in directories
• Must include __init__.py
• Useful for large projects
• Example:
• my_package/
• ─ __init__.py
• ─ module1.py
• ─ module2.py
Types of Modules
1. Built-in Modules (math, sys, os)
2. User-defined Modules (custom)
3. Third-party Modules (matplotlib, numpy via pip)
How to Import Modules
• • import module
• • import module as alias
• • from module import function
• Example:
• import math
• import random as rnd
• from math import sqrt, pi
Difference
1. Import module: access using module.function()
2. From module import …..: access directly without
prefix
3. As alias: shorter names, helpful in large programs
Creating a User-defined Module
• File: mygreetings.py
• def greet(name):
• return f"Hello, {name}!"
• File: main.py
• import mygreetings
• print(mygreetings.greet("Muskan"))
• Output
Hello, Muskan!
math Module – Introduction
• Built-in math library for calculations
• Useful in scientific, financial, and engineering
computations
math Module – Arithmetic
Functions
• • math.sqrt(25) → 5.0
• • math.pow(2, 3) → 8.0
• • math.exp(2) → 7.38...
math Module – Rounding and
Constants
• • math.floor(2.9) → 2
• • math.ceil(2.1) → 3
• • math.pi → 3.141592...
math Module – Trigonometric
Functions
• • math.sin(math.radians(90)) → 1.0
• • math.cos(math.radians(60)) → 0.5
• • math.tan(math.radians(45)) → 1.0
random Module – Introduction
• • Used to generate pseudo-random numbers
• • Applications: Games, testing, sampling, simulations
random Module – Basic Usage
• • random.random() → float [0.0, 1.0)
• • random.randint(1, 100) → random integer
random – Selection and Shuffle
• • random.choice(['a','b','c']) → 'b'
• • random.shuffle(list) → shuffle in place
random – Sampling and Uniform
• • random.sample(range(1,50),6)
• • random.uniform(10, 20)
Applications of random Module
• • Games (AI behavior, dice)
• • Simulations
• • Data Science
• • Testing
matplotlib – Introduction
Matplotlib is a comprehensive library used for creating
static, animated, and interactive visualizations in
python. It is widely used in:
1. Data science
2. Scientific research
3. Machine learning
4. Engineering and academic projects
Key Features of matplotlib and
Installation
• High level plotting interface
• Wide range of charts: line plots, bar charts, histograms,
pie charts, scatter plots
• Highly customizable: add legends, labels, colors,
annotations
• Works well with NumPy and Pandas data
• Pip install matplotlib
• Use: import matplotlib.pyplot as plt
What is pyplot?
Pyplot is a submodule of matplotlib that provides a
MATLAB- like interface for creating plots easily.
• Think of pyplot as a quick plotting tool in matplotlib.
• Each plotting function in pyplot makes some change
to a figure: like creating a figure, adding a plot, setting
labels, etc.
What if You Don’t Use pyplot?
• matplotlib can be used without pyplot.
• In that case, you use its Object-Oriented (OO)
interface.
• This is more flexible but also more complex.
Using pyplot – Simple and Quick
• import matplotlib.pyplot as plt
• x = [1, 2, 3]; y = [2, 4, 1]
• plt.plot(x, y)
• plt.title("Line Graph")
• plt.show()
Without pyplot – Object-Oriented
Style
• from matplotlib.figure import Figure
• from matplotlib.backends.backend_agg import
FigureCanvasAgg as FigureCanvas
• fig = Figure()
• canvas = FigureCanvas(fig)
• ax = fig.add_subplot(111)
• ax.plot([1, 2, 3], [2, 4, 1])
• fig.savefig("plot.png")
When to Use pyplot
• • Quick data visualizations
• • Jupyter notebooks
• • Scripts for EDA
• • Easy to learn and use
• • Great for line, bar, pie charts
When to Avoid pyplot
• • Building interactive apps (Tkinter, PyQt)
• • Embedding in web apps
• • Need for high customization with multiple
axes/figures
Summary: pyplot vs OO Style
• pyplot:
• Simple and fast
• Limited customization
• OO Style:
• Powerful and flexible
• More code and complexity
Line Plot Example
• x = [1,2,3]
• y = [2,4,1]
• plt.plot(x,y)
• plt.show()
Using Titles and Labels
• x = [1,2,3]
• y = [2,4,1]
• plt.plot(x,y)
• plt.title(‘Title of the chart’)
• plt.xlabel(‘X-axis’)
• plt.ylabel(‘Y-axis’)
• plt.show()
Bar Chart
• plt.bar(['A', 'B', 'C'], [10, 20, 15])
• plt.show()
Pie Chart
• labels = ['A', 'B', 'C']
• sizes = [30, 40, 30]
• plt.pie(sizes, labels=labels)
• plt.show()
Histogram
• data = [1,1,2,3,3,3,4,5]
• plt.hist(data)
• plt.show()
Scatter Plot
• x = [5, 7, 8]
• y = [99, 86, 87]
• plt.scatter(x, y)
• plt.show()
Multiple Plot
• plt.plot(x, y, label='Line')
• plt.scatter(x, y, label='Points')
• plt.legend()
• plt.show()
Customization and Saving
• plt.grid(True)
• plt.xlim(0,10)
• plt.ylim(0,50)
• plt.savefig('plot.png')
Using Aliases and Integration
• • import numpy as np
• • import matplotlib.pyplot as plt
Conclusion
• • Modules = reusable code
• • math & random = logic
• • matplotlib = visuals
• • Combine for powerful tools
Q&A
• Any Questions?
• Thank you!