NumPy array operations

This section will guide you through the creation and manipulation of numerical data with NumPy. Let's start by creating a NumPy array from the list:

In [17]: my_list = [2, 14, 6, 8]         my_array = np.asarray(my_list)         type(my_array)Out[17]: numpy.ndarray

Let's do some addition, subtraction, multiplication, and division with scalar values:

In [18]: my_array + 2Out[18]: array([ 4, 16, 8, 10])In [19]: my_array - 1Out[19]: array([ 1, 13, 5, 7])In [20]: my_array * 2Out[20]: array([ 4, 28, 12, 16, 8])In [21]: my_array / 2Out[21]: array([ 1. , 7. , 3. , 4. ])

It's much harder to do the same operations in a list because the list does not support vectorized operations and you need to iterate its elements. There are many ways to create ...

Get Mastering Numerical Computing with NumPy now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.