+ 5
Can you please explain the output of this code?
import numpy as np a = np.arrange(0,8,2) b = np.arrange(1,8,3) print(a[2] == b[1]) Answer is True
1 Answer
+ 1
np.arange(start, stop, step):
Create an array with integer value starting from 'start', to 'stop' (not included), with a step of 'step'.
e.g. this creates an array with int values from 1 - 5 (Excluding 5) with a step of 1.
>>> np.arange(1, 5, 1)
array([1, 2, 3, 4])
Your code has:
>>> a = np.arange(0, 8, 2) # Make array from 0-8 with step of 2
>>> a # Inspect contents
array([0, 2, 4, 6]) # Notice the 8 is excluded.
The other array you've created using arange:
>>> b = np.arange(1, 8, 3) # Make array from 1-8 with step of 3
>>> b # Inspect contents
array([1, 4, 7])
Notice the 4 that both arrays have as value stored somewhere?
In your code it is a[2] (Index starting from zero) and b[1].
That is why a[2] == b[1].
I hope this explains it.



