๐Ÿ“ ๐Ÿ‰ Python Modules for Engineers#

Python Modules for Engineers

Why Modules?#

  • Reuse Code: Tools for engineering problems.

  • Two Types:

    1. Built-In: Ready-to-go (math, os, datetime).

    2. External: Install with pip (NumPy, matplotlib).

Built-In Modules#

Python has many built-in modules that provide ready-to-use tools for common engineering problems. Here are some of the most useful ones:

# Example: math (Calculations)
import math
print(f"Circle Area (r=10): {math.pi * math.pow(10, 2):.2f}")
Circle Area (r=10): 314.16
import os

print(f"Working Directory: {os.getcwd()}")
Working Directory: /home/jca92/drexel_runner_engineering/actions-runner/_work/ENGR131W25/ENGR131W25/jupyterbook/week_1/lecture

External Modules#

Python has a vast ecosystem of external modules that can be installed using the pip package manager. Here are some of the most useful ones:

Numpy (Numerical Python)#

NumPy is the fundamental package for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays.

!pip install numpy
import numpy as np
print(f"Matrix Product:\n{np.dot([[1, 2], [3, 4]], [[1, 0], [0, 1]])}")
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 Product:
[[1 2]
 [3 4]]

Tip

You can install external modules using the !pip install command in Jupyter notebooks, or just pip install in the terminal. If you are goint to install modules locally it is best to use a package manager like conda or pipenv.

Matplotlib#

Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. It can be used to generate plots, histograms, power spectra, bar charts, error charts, scatterplots, etc.

!pip install matplotlib
import matplotlib.pyplot as plt
plt.plot([0, 1, 2], [0, 1, 4])
plt.title("Simple Plot")
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
../../_images/3a97e5768b5787aaccf3d3e89af5254d2d967c867c79e5331c5df410df17ac07.png

DIY: Create a Module#

# Save this in my_tools.py:
"""
def hello(name):
    return f"Hi {name}!"
"""

# This is the code that will do it automatically:
with open('my_tools.py', 'w') as file:
    file.write("""
def hello(name):
    return f"Hi {name}!"
""")
# Import your module:
import my_tools

print(my_tools.hello("Engineer"))
Hi Engineer!

Summary#

  1. Built-In: Start with math, os.

  2. External: Install essentials (NumPy, matplotlib).

  3. DIY: Build custom tools.