Intro to Python

5. NumPy Arrays Part 2: Operations

NumPy makes doing math with arrays much easier.

For example, if you want to add each corresponding number of two arrays of the same length, you can just do this:

list1 = np.array([1, 2, 3, 4, 5, 6])
list2 = np.array([1, 2, 3, 4, 5, 6])

list_add = list1 + list2

This can be done with subtraction (-) , multiplication (*), division (/) , and mod (%) as well:

But be careful!

This won’t work for normal Python Lists as seen if you try to run this:

You can also multiply an entire NumPy Array by a scalar:

If you’re familiar with linear algebra, NumPy also has a ton of operations that make operations with matrices (represented as NumPy arrays) incredibly easy.

For example, if you want to perform matrix operation between two NumPy arrays you could use the @ symbol.

This is just a small taste of the operations you can do with NumPy arrays, for a full list you can find more information in NumPy's documentation (https://numpy.org/devdocs/index.html).