2.3 Numerical Python Arrays
49
The Built-In Len Function It is appropriate here to also mention the built-in
function len, which is useful when you need to find the length (i.e., the number
of elements) of an array. 11
A quick demonstration of zeros and len may go like this,
In [1]: from numpy import zeros
In [2]: x = zeros(3, int)
# get array with integer zeros
In [3]: x
Out[3]: array([ 0, 0, 0])
In [4]: y = zeros(3)
# get array with floating point zeros
In [5]: y
Out[5]: array([ 0., 0., 0.])
In [6]: y[0] = 0.0;
y[1] = 1.0;
y[2] = 2.0
# overwrite
In [7]: y
Out[7]: array([ 0., 1., 2.])
In [8]: len(y)
Out[8]: 3
Note that the line x = zeros(3, int) could, alternatively, have been written a bit
more informative as x = zeros(3, dtype=int), where dtype means data type.
Just like with linspace, the line y = zeros(3) instructs Python to reserve, or
allocate, space in memory for the array produced by zeros. As we see from the
dialogue, the array gets the name y and will have three elements, all with a floating
point zero value. Each element in y is next overwritten by “the numbers we actually
wanted”. Strictly speaking, the assignment y[0] = 0.0 was redundant here, since
the value of y[0] was known to be 0.0 from before. It should be mentioned, though,
that programmers deliberately write such seemingly redundant statements from time
to time, meaning to tell a human code interpreter that the value is not “accidental”.
In our dialogue here, one explicit statement was written for each element value
we changed, i.e. as y[0] = 0.0; y[1] = 1.0; y[2] = 2.0. For arrays with
just a few elements, this is acceptable. However, with many elements, this is
obviously not the way to go. Longer arrays are typically first generated by zeros
(or otherwise), before individual array elements get new values in some loop
arrangement (explained in Chap. 3).
The Array Function Another handy way of creating an array, is by using the
function array, e.g., like this,
In [1]: from numpy import array
In [2]: x = array([0, 1, 2])
# get array with integers
In [3]: x
Out[3]: array([0, 1, 2])
In [4]: x = array([0., 1., 2.])
# get array with real numbers
11 The len function may also be used with other objects, e.g., lists (Sect. 5.1).
49
The Built-In Len Function It is appropriate here to also mention the built-in
function len, which is useful when you need to find the length (i.e., the number
of elements) of an array. 11
A quick demonstration of zeros and len may go like this,
In [1]: from numpy import zeros
In [2]: x = zeros(3, int)
# get array with integer zeros
In [3]: x
Out[3]: array([ 0, 0, 0])
In [4]: y = zeros(3)
# get array with floating point zeros
In [5]: y
Out[5]: array([ 0., 0., 0.])
In [6]: y[0] = 0.0;
y[1] = 1.0;
y[2] = 2.0
# overwrite
In [7]: y
Out[7]: array([ 0., 1., 2.])
In [8]: len(y)
Out[8]: 3
Note that the line x = zeros(3, int) could, alternatively, have been written a bit
more informative as x = zeros(3, dtype=int), where dtype means data type.
Just like with linspace, the line y = zeros(3) instructs Python to reserve, or
allocate, space in memory for the array produced by zeros. As we see from the
dialogue, the array gets the name y and will have three elements, all with a floating
point zero value. Each element in y is next overwritten by “the numbers we actually
wanted”. Strictly speaking, the assignment y[0] = 0.0 was redundant here, since
the value of y[0] was known to be 0.0 from before. It should be mentioned, though,
that programmers deliberately write such seemingly redundant statements from time
to time, meaning to tell a human code interpreter that the value is not “accidental”.
In our dialogue here, one explicit statement was written for each element value
we changed, i.e. as y[0] = 0.0; y[1] = 1.0; y[2] = 2.0. For arrays with
just a few elements, this is acceptable. However, with many elements, this is
obviously not the way to go. Longer arrays are typically first generated by zeros
(or otherwise), before individual array elements get new values in some loop
arrangement (explained in Chap. 3).
The Array Function Another handy way of creating an array, is by using the
function array, e.g., like this,
In [1]: from numpy import array
In [2]: x = array([0, 1, 2])
# get array with integers
In [3]: x
Out[3]: array([0, 1, 2])
In [4]: x = array([0., 1., 2.])
# get array with real numbers
11 The len function may also be used with other objects, e.g., lists (Sect. 5.1).
