Python Libraries
Python Libraries
Python libraries are collections of pre-written and pre-tested code that provide developers
with useful functionalities to solve specific programming problems. Thus they can save time
and effort by providing solutions to commonly encountered problems. To use them, we use
Python libraries that are essential for mechanical engineers include, but not limited to, math
for basic mathematical operations, NumPy for numerical computing, and Matplotlib for data
visualization.
To see all the methods and attributes in a library, we use the dir() function.
import math
print(dir(math))
math.sqrt()
Math Library
The math library provides functions for common mathematical operations, such as
trigonometric functions, logarithms, and exponential functions. For example, to calculate the
square root of a number in Python, you can use the math library's sqrt() function:
import math
x = 25
y = math.sqrt(x)
print(y)
Numpy Library
The numpy library provides tools for working with arrays and matrices, which are essential for
operations on arrays efficiently. For example, to create a 2D array in Python and perform a
import numpy as np
C = np.dot(A, B)
print(C)
Matplotlib Library
Matplotlib is a popular Python library for creating visualizations, including charts, graphs, and
import numpy as np
v = 10 + 5 * np.sin(t)
a = 5 * np.cos(t)
axs[0].plot(t, v)
axs[0].set_title('Velocity Profile')
axs[0].set_xlabel('Time (s)')
axs[0].set_ylabel('Velocity (m/s)')
axs[1].plot(t, a)
axs[1].set_title('Acceleration Profile')
axs[1].set_xlabel('Time (s)')
axs[1].set_ylabel('Acceleration (m/s^2)')
plt.tight_layout()
plt.show()