October 2019
Beginner to intermediate
674 pages
16h 43m
English
Array indices in NumPy start at 0 as in languages such as Python, Java, and C++ and unlike in Fortran, Matlab, and Octave, which start at 1. Arrays can be indexed in the standard way as we would index into any other Python sequences:
# print entire array, element 0, element 1, last element.
In [36]: ar = np.arange(5); print ar; ar[0], ar[1], ar[-1]
[0 1 2 3 4]
Out[36]: (0, 1, 4)
# 2nd, last and 1st elements
In [65]: ar=np.arange(5); ar[1], ar[-1], ar[0]
Out[65]: (1, 4, 0)
Arrays can be reversed using the ::-1 idiom as follows:
In [24]: ar=np.arange(5); ar[::-1]
Out[24]: array([4, 3, 2, 1, 0])
Multidimensional arrays are indexed using tuples of integers:
In [71]: ar = np.array([[2,3,4],[9,8,7],[11,12,13]]); ar ...
Read now
Unlock full access