50
2 A Few More Steps
In [5]: x
Out[5]: array([ 0., 1., 2.])
Note the use of “dots” to get floating point numbers and that we call array with
bracketed 12 numbers, i.e., [0, 1, 2] and [0., 1., 2.].
2.3.2 Indexing an Array from the End
By use of a minus sign, the elements of an array may be indexed from the end, rather
from the beginning. That is, with our array x here, we could get hold of the very last
element by writing x[-1], the second last element by writing x[-2], and so on.
Continuing the previous interactive session, we may write
In [6]: x[-1]
Out[6]: 2.0
In [7]: x[-2]
Out[7]: 1.0
In [8]: x[-3]
Out[8]: 0.0
2.3.3 Index Out of Bounds
A typical error to make when working with arrays, is to accidentally use an illegal
index. With the array x in our ongoing dialogue, we may illustrate this by
In [9]: x[3]
Traceback (most recent call last):
File "", line 1, in
x[3]
IndexError: index 3 is out of bounds for axis 0 with size 3
Our index 3 is illegal and we get the error message shown. From the above, it should
be clear that legal indices are 0, 1 and 2. Alternatively, if indexing from the end, legal
indices are −1, −2 and −3. Note that counting is a bit “different” then, causing −3
to be a legal index (as we saw above), while 3 is not.
2.3.4 Copying an Array
Copying arrays requires some care, as can be seen next, when we try to make a copy
of the x array from above:
In [10]: y = x
In [11]: y
Out[11]: array([ 0., 1., 2.])
# ...as expected
12 The arguments to array are two examples of lists, which will be addressed in Sect. 5.1.
2 A Few More Steps
In [5]: x
Out[5]: array([ 0., 1., 2.])
Note the use of “dots” to get floating point numbers and that we call array with
bracketed 12 numbers, i.e., [0, 1, 2] and [0., 1., 2.].
2.3.2 Indexing an Array from the End
By use of a minus sign, the elements of an array may be indexed from the end, rather
from the beginning. That is, with our array x here, we could get hold of the very last
element by writing x[-1], the second last element by writing x[-2], and so on.
Continuing the previous interactive session, we may write
In [6]: x[-1]
Out[6]: 2.0
In [7]: x[-2]
Out[7]: 1.0
In [8]: x[-3]
Out[8]: 0.0
2.3.3 Index Out of Bounds
A typical error to make when working with arrays, is to accidentally use an illegal
index. With the array x in our ongoing dialogue, we may illustrate this by
In [9]: x[3]
Traceback (most recent call last):
File "
x[3]
IndexError: index 3 is out of bounds for axis 0 with size 3
Our index 3 is illegal and we get the error message shown. From the above, it should
be clear that legal indices are 0, 1 and 2. Alternatively, if indexing from the end, legal
indices are −1, −2 and −3. Note that counting is a bit “different” then, causing −3
to be a legal index (as we saw above), while 3 is not.
2.3.4 Copying an Array
Copying arrays requires some care, as can be seen next, when we try to make a copy
of the x array from above:
In [10]: y = x
In [11]: y
Out[11]: array([ 0., 1., 2.])
# ...as expected
12 The arguments to array are two examples of lists, which will be addressed in Sect. 5.1.
