12.9) Array Indexing
Array indexing and slicing follows the same rules as for lists.
>>> a=np.array([3.3,2.4,5,-6.7,3]) >>> a[0] # remember indexing starts at 0 in Python 3.3 >>> a[2] 5.0 >>> b=a[1:3] # in slicing, the upper bound is excluded >>> b array([ 2.4, 5. ])
You can assign a new value to an element of an array, like you do with lists (i.e. arrays are mutable):
>>> a[3]=67.8 >>> a array([ 3. , 2.4, 5. , 67.8, 3.3])
How do you get the second from last element (-6.7) of a, where:
a=np.array([3.3,2.4,5,-6.7,3])
Which of the following answers is correct?
- a[2]
- a[-2]
- a[4]
- a[3]
Print out their values to see which ones are correct.