Python Mastery โ€“ From Basics to Brilliance / Python Libraries for Real-World Use

What is NumPy?

๐Ÿ”ข NumPy

NumPy (Numerical Python) is a popular Python library used for numerical and mathematical operations, especially when working with large amounts of data.

It provides a powerful data structure called an array.


๐Ÿ“ฆ Installing NumPy

NumPy is not part of Python's Standard Library, so it is usually installed using pip:

pip install numpy

Then import it:

import numpy as np

๐Ÿ”ข NumPy Array

A NumPy array is similar to a Python list, but is designed for fast numerical operations.

import numpy as np

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

print(numbers) #[1 2 3 4 5]

โšก Why NumPy?

With a Python list:

numbers = [1, 2, 3]

print(numbers * 2) #[1, 2, 3, 1, 2, 3]

But with NumPy:

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

print(numbers * 2) #[2 4 6]

NumPy performs the mathematical operation on every element.


๐Ÿงฎ Common Uses

NumPy is widely used for:

  • ๐Ÿ”ข Numerical calculations

  • ๐Ÿ“Š Data analysis

  • ๐Ÿงฎ Mathematical operations

  • ๐Ÿค– Machine Learning

  • ๐Ÿ“ˆ Scientific computing

  • ๐ŸงŠ Working with multidimensional arrays


๐Ÿ“Œ Summary:

Clause

When it runs

Purpose

try

Always

Try code that might raise errors

except

Only if error occurs

Handle specific errors

else

Only if no error

Do something if all is well

finally

Always

Do final cleanup (even if error happens)