Data Analysis with Python (Numpy Operations) - Error numpy arange

I currently learn about Numpy Operations. I try this Python code:

a = np.arange(5)
a + 20

The result of this code is [20, 21, 22, 24, 24] but it was not the correct answer. Can I explain why is not correct answer? Thank you?

The question is not for the result of the code, but for the value of a.
Something that’s not even special to numpy or Python in this context.

Think of the difference between

a = np.arange(5)
a + 20
#(the code you posted)

and

a = np.arange(5)
a = a + 20

The point is that in the first case, a new value is never assigned to a. The program sees the instruction and calculates a NEW numpy array that is equivalent to [20, 21, 22, 24, 24] but this array is never assigned to the variable a, like in the second instance. If you try to run print(a) after the first code, its value will still be [0, 1, 2, 3, 4]

a = np.arange(5)
a + 20
print(a)
array([0, 1, 2, 3, 4])

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.