246 Introduction to Computational Models with Python
Executing the Python interpreter and running the program yields the fol-
lowing output. No te that only the first k elements of array p are set to value
5.25.
$ python test2_arrays.py
Creating and manipulate an array
Array p: [ 0. 0. 0. 0. 0. 0. 0. 0.]
Array p:
[ 5.25 5.25 5.25 5.25 5.25 0. 0. 0. ]
Slicing can be used to indicate more selective indexing with the : operato r.
For example, x[0:4] references the elements of array x but only the elements
with indices from 0 up to 3. Most o f the time it is more efficient (time-wise)
to use slicing than using a loop. The following pr ogram is a variation o f the
previous one, but instead of the loop, now slicing is used in line 7 to assign
values to select ...