write a program in Python to sum of two arrays using numpy
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Verified answer
[tex]\texttt{\textsf{\large{\underline{Solution}:}}}[/tex]
The given problem is solved using NumPy module in Python.
import numpy as np
array_1=list()
array_2=list()
n=int(input('Enter the number of elements for the array: '))
print('Enter the elements in first array..')
for i in range(n):
array_1.append(int(input(f'array_1[{i}] = ')))
print('----------------------------------------')
print('Enter the elements in second array..')
for i in range(n):
array_2.append(int(input(f'array_2[{i}] = ')))
resultant_array=list(np.add(array_1,array_2))
print('Array_1:',array_1)
print('Array_2:',array_2)
print('Their Sum: ',resultant_array)
[tex]\texttt{\textsf{\large{\underline{Explanation}:}}}[/tex]
See attachment for output.
A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. ... A list is the Python equivalent of an array, but is resizeable and can contain elements of different types. A common beginner question is what is the real difference here. The answer is performance.