0% found this document useful (0 votes)
7 views4 pages

Python Libraries

Uploaded by

oduduebunkamaodo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Python Libraries

Uploaded by

oduduebunkamaodo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

the “import” statement.

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.

Therefore, for the math library, we can do the following

import math

print(dir(math))

To use these methods, you simply type library_name.method. eg

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

many engineering applications. Numpy functions allow you to perform mathematical

operations on arrays efficiently. For example, to create a 2D array in Python and perform a

matrix multiplication, you can use the numpy library:

import numpy as np

A = np.array([[1, 2], [3, 4]])

B = np.array([[5, 6], [7, 8]])

C = np.dot(A, B)

print(C)
Matplotlib Library

Matplotlib is a popular Python library for creating visualizations, including charts, graphs, and

plots. Let us take an example by plotting velocity and acceleration profiles.

import matplotlib.pyplot as plt

import numpy as np

# create time array

t = np.linspace(0, 10, 100)

# create velocity and acceleration arrays

v = 10 + 5 * np.sin(t)

a = 5 * np.cos(t)

# create velocity and acceleration plots

fig, axs = plt.subplots(2)

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()

You might also like