๐ ๐ Python Modules and Packages#
Introduction#
Hey, Dragons ๐! Are you ready to harness the power of Python modules to tackle engineering problems like a Drexel pro? Whether youโre in the lab, the classroom, or the Innovation Studio, Pythonโs got your back.
In this notebook, weโll explore:
Built-in modules: Pythonโs ready-to-go tools.
Non-built-in modules: The ones youโll install but will love forever.
What is a Module?#
# A module is a Python file with tools you can reuse.
# Think of it as the digital equivalent of the lab kits at Drexelโyou donโt have to build it all from scratch!
print("A Python module: because Dragons code smarter, not harder.")
A Python module: because Dragons code smarter, not harder.
In Python tools are built around modules that perform operations.
Modularity allows the isolation of code to simplify programming
Built-In Modules (Always There, Like Drexel Spirit!)#
Python contains many built in functions that are ready to use.
You import modules using
import <Module Name>
# Math: Great for calculating pizza slices per engineer.
# Example 1: math - The Nerdy Workhorse
import math
radius = 10 # Maybe you're designing something cool in the Drexel Machine Shop?
area = math.pi * math.pow(radius, 2)
print(f"Area of a 10-unit radius circle (ideal for Drexel plaques): {area:.2f}")
Area of a 10-unit radius circle (ideal for Drexel plaques): 314.16
# OS: Where am I in the digital jungle?
# Example 2: os - File Navigation Like the Drexel Campus
import os
current_dir = os.getcwd()
print(f"Your code is running in: {current_dir}")
Your code is running in: /home/jca92/drexel_runner_engineering/actions-runner/_work/ENGR131W25/ENGR131W25/jupyterbook/week_1/readings
# Example 3: datetime - Keep Track of Deadlines (Quarters Fly By!)
from datetime import datetime
now = datetime.now()
print(f"As of now ({now}), you're already thinking about finals. ๐
")
As of now (2025-03-11 22:00:19.637132), you're already thinking about finals. ๐
You can find more built-in functions
Non-Built-In Modules (Install and Thrive)#
Drexel students never settle for the basics. Letโs bring in the advanced tools.
There are many additional packages. Anyone in the world can make a package. Most packages are distributed using the Python Package Index (PyPI).
You can install packages using package managers:
pip install <package name>
or
conda install <package name>
Note
Usually when you download python distributions they will contain many of the common packages. We have installed all the packages you need for the course on the JupyterHub.
Syntax#
from {package name} import {module}
from {package name} import {module} as {name}</code></pre>
# NumPy: The Dragon's secret weapon for data wrangling.
# Example 1: NumPy - Math for Engineers
!pip install numpy
import numpy as np
data = np.array([[1, 2], [3, 4]]) # A classic matrix for your ECE 201 homework
print("Matrix multiplication result:")
print(np.dot(data, data)) # Multiply the matrix by itself, because why not?
Requirement already satisfied: numpy in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (2.2.2)
[notice] A new release of pip is available: 24.3.1 -> 25.0.1
[notice] To update, run: pip install --upgrade pip
Matrix multiplication result:
[[ 7 10]
[15 22]]
# Example 2: matplotlib - Visualize Like You're in URBN
!pip install matplotlib
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y, label="sin(x)")
plt.title("Dragons โค๏ธ Charts")
plt.xlabel("x-axis (Time)")
plt.ylabel("y-axis (Amplitude)")
plt.legend()
plt.show()
Requirement already satisfied: matplotlib in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (3.10.0)
Requirement already satisfied: contourpy>=1.0.1 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from matplotlib) (1.3.1)
Requirement already satisfied: cycler>=0.10 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from matplotlib) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from matplotlib) (4.55.3)
Requirement already satisfied: kiwisolver>=1.3.1 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from matplotlib) (1.4.8)
Requirement already satisfied: numpy>=1.23 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from matplotlib) (2.2.2)
Requirement already satisfied: packaging>=20.0 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from matplotlib) (24.2)
Requirement already satisfied: pillow>=8 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from matplotlib) (11.1.0)
Requirement already satisfied: pyparsing>=2.3.1 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from matplotlib) (3.2.1)
Requirement already satisfied: python-dateutil>=2.7 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from matplotlib) (2.9.0.post0)
Requirement already satisfied: six>=1.5 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from python-dateutil>=2.7->matplotlib) (1.17.0)
[notice] A new release of pip is available: 24.3.1 -> 25.0.1
[notice] To update, run: pip install --upgrade pip

# Example 3: scipy - Engineer Like a Drexel Grad
!pip install scipy
from scipy.integrate import quad
result, _ = quad(lambda x: x**2, 0, 1) # Integrate x^2 from 0 to 1
print(f"Integration result (for x^2, 0 to 1): {result:.2f}")
Requirement already satisfied: scipy in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (1.15.1)
Requirement already satisfied: numpy<2.5,>=1.23.5 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from scipy) (2.2.2)
[notice] A new release of pip is available: 24.3.1 -> 25.0.1
[notice] To update, run: pip install --upgrade pip
Integration result (for x^2, 0 to 1): 0.33
Submodules#
Many modules contain submodules. These can be accessed by calling <module>.<submodule>
Tip
If you type a module name. you can use tab
to discover the available submodules
plt.
Build Your Own Module (Drexel DIY Spirit)#
Want to showcase your Drexel engineering skills? Build your own module!
Save this in a file named drexel_tools.py
# drexel_tools.py
def hello_dragon(name):
return f"Welcome to Drexel, {name}! Letโs solve some engineering problems."
def calculate_gpa(grades):
return sum(grades) / len(grades)
Now, letโs import your module and use it!
import drexel_tools
welcome = drexel_tools.hello_dragon("Alex")
gpa = drexel_tools.calculate_gpa([3.7, 3.8, 3.9, 4.0]) # Realistic Drexel GPA goals
print(welcome)
print(f"Your Drexel GPA: {gpa:.2f}")
Welcome to Drexel, Alex! Letโs solve some engineering problems.
Your Drexel GPA: 3.85
Bonus: Drexel Engineering Favorites#
# pandas: Organize Data Like You're Prepping for Co-op
!pip install pandas
import pandas as pd
data = {"Course": ["ENGR 101", "ECE 200", "MATE 221"],
"Grade": [4.0, 3.7, 3.8]}
df = pd.DataFrame(data)
print("Your Drexel Grades:\n", df)
Requirement already satisfied: pandas in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (2.2.3)
Requirement already satisfied: numpy>=1.23.2 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from pandas) (2.2.2)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from pandas) (2024.2)
Requirement already satisfied: tzdata>=2022.7 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from pandas) (2024.2)
Requirement already satisfied: six>=1.5 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)
[notice] A new release of pip is available: 24.3.1 -> 25.0.1
[notice] To update, run: pip install --upgrade pip
Your Drexel Grades:
Course Grade
0 ENGR 101 4.0
1 ECE 200 3.7
2 MATE 221 3.8
# sympy: Do Symbolic Math Like a True Engineer
!pip install sympy
from sympy import symbols, solve
x = symbols('x')
equation = x**2 - 4
solutions = solve(equation, x)
print(f"Solutions to x^2 - 4 = 0 (like solving statics problems): {solutions}")
Requirement already satisfied: sympy in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (1.13.1)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /home/jca92/drexel_runner_engineering/actions-runner/_work/_tool/Python/3.11.11/x64/lib/python3.11/site-packages (from sympy) (1.3.0)
[notice] A new release of pip is available: 24.3.1 -> 25.0.1
[notice] To update, run: pip install --upgrade pip
Solutions to x^2 - 4 = 0 (like solving statics problems): [-2, 2]
Summary#
Python modules save you time and effortโjust like Drexelโs quarter system (maybe?).
Built-in modules are always available for quick solutions.
Non-built-in modules unlock advanced functionalityโperfect for capstones, co-ops, and research.
Build your own modules to share your engineering genius with the world.