How to add two arrays in Numpy?

Let’s check how to add two arrays in Numpy. We will use Numpy add method and one clever trick.
Numpy add sum arrays

Using an add method

To add values of two different arrays in Numpy, you should use the Numpy add method. Add function takes arrays as arguments.

An example of using the np.add method is np.add(my_array, my_second_array).

import numpy as np

my_array = np.array([10, 20, 30, 40, 50])
print(f"My array \n{my_array}")

my_second_array = np.array([50, 50, 50, 50, 50])
print(f"My second array \n{my_second_array}")

add_array = np.add(my_array, my_second_array)
print(f"My added arrays \n{add_array}")

sum_array = my_array + my_second_array
print(f"My summed arrays \n{sum_array}")

Numpy add sum arrays

As demonstrated, array addition also works with the simple + operator. This is the trick I wanted to share with you that you can simply add array and it also works fine!

See also  How to cast an array from one dtype to another using Numpy astype?