array
population
.
Notice that we wrote
.item(0)
, not
.item(1)
, to get the first element. This is a weird convention in computer
science. 0 is called the
index
of the first item. It's the number of elements that appear
before
that item. So 3 is
the index of the 4th item.
Here are some more examples. In the examples, we've given names to the things we get out of
population
.
Read and run each cell.
In [52]:
# The third element in the array is the population
# in 1952.
population_1952 = population.item(2)
population_1952
In [53]:
# The thirteenth element in the array is the population
# in 1962 (which is 1950 + 12).
population_1962 = population.item(12)
population_1962
In [54]:
# The 66th element is the population in 2015.
population_2015 = population.item(65)
population_2015
In [55]:
# The array has only 66 elements, so this doesn't work.
# (There's no element with 66 other elements before it.)
population_2016 = population.item(66)
population_2016
In [ ]:
# Since make_array returns an array, we can call .item(3)
# on its output to get its 4th element, just like we
# "chained" together calls to the method "replace" earlier.
make_array(-1, -3, 4, -2).item(3)
Out[52]:
2636772306
Out[53]:
3140093217
Out[54]:
7256490011
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-55-7cf1bbebe489> in <module>()
1
# The array has only 66 elements, so this doesn't work.
2
# (There's no element with 66 other elements before it.)
----> 3 population_2016 = population.item(66)
4
population_2016
IndexError: index 66 is out of bounds for axis 0 with size 66

Question 4.2.1.
Set
population_1973
to the world population in 1973, by getting the appropriate element from
population
using
item
.
In [56]:
population_1973 = population.item(23)
#SOLUTION
population_1973
In [57]:
_ = ok.grade('q421')
4.3. Doing something to every element of an array
Arrays are primarily useful for doing the same operation many times, so we don't often have to use
.item
and
work with single elements.
Logarithms
Here is one simple question we might ask about world population:
How big was the population in
orders of magnitude
in each year?
The logarithm function is one way of measuring how big a number is. The logarithm (base 10) of a number
increases by 1 every time we multiply the number by 10. It's like a measure of how many decimal digits the
number has, or how big it is in orders of magnitude.
We could try to answer our question like this, using the
log10
function from the
math
module and the
item
method you just saw:
In [58]:
population_1950_magnitude = math.log10(population.item(0))
population_1951_magnitude = math.log10(population.item(1))
population_1952_magnitude = math.log10(population.item(2))
population_1953_magnitude = math.log10(population.item(3))
...
Out[56]:
3942096442
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
Passed: 1
Failed: 0
[ooooooooook] 100.0% passed
Out[58]:
Ellipsis

But this is tedious and doesn't really take advantage of the fact that we are using a computer.
Instead, NumPy provides its own version of
log10
that takes the logarithm of each element of an array. It takes a
single array of numbers as its argument. It returns an array of the same length, where the first element of the
result is the logarithm of the first element of the argument, and so on.