Python / Lists and arrays

Arrays: array and numpy

Create an array of whole numbers with the array library and read its values, and meet numpy arrays that calculate on every value at once. Baccalaureate

A list accepts any mix of values: [2, 2, 1, "Caution", 2]. But when the data should be numbers only, like warning levels, it is better to stop any other type getting in by mistake. That is what an array does: all its values are of one type.

Python
import array

warn_array = array.array('i', [2, 2, 1, 1, 2, 1, 2])
print(warn_array[0])
print(warn_array[5])
print(len(warn_array))
Console
2
1
7
PartMeaning
import arrayimport the array library
array.array(...)create an array
'i'value type: whole numbers (integer)
[2, 2, 1, ...]the values themselves

You use an array just like a list: positions start at 0, len() gives the number of values, and for goes through them:

Python
import array

day_array = array.array('i', [2, 3, 4, 5, 6, 7, 1])
print(day_array)
for day in day_array:
    print(day)
Console
array('i', [2, 3, 4, 5, 6, 7, 1])
2
3
4
5
6
7
1

If you try to put text into an 'i' array, Python refuses with an error:

Python
import array

warn_array = array.array('i', [2, 1, "Caution"])
print(warn_array)
Console
TypeError: 'str' object cannot be interpreted as an integer
Do not forget import array at the top, or the type 'i' before the square brackets. For decimals use the type 'd'.

numpy arrays

The numpy library is built for calculations on lots of numbers. Its big feature: an operation on the array is applied to every value at once, with no loop:

Python
import numpy

score_array = numpy.array([42, 58, 71])
bonus_array = score_array + 5
print(bonus_array)
print(score_array * 2)
Console
[47 63 76]
[ 84 116 142]
Notice numpy prints the array with spaces instead of commas: [47 63 76]. With a plain list, [42, 58, 71] + 5 gives an error; adding a number to every value is a numpy feature.

Practice

1Write the programCurriculum practice

A week starting on Tuesday is numbered 2, 3, 4, 5, 6, 7, 1. Store it in the array day_array (whole numbers), then print element 0, then element 6.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
import array

Console
Press Run to see the result.
2Write the programCurriculum practice

Print the length of warn_array.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
import array

warn_array = array.array('i', [2, 2, 1, 1, 2, 1, 2])
Console
Press Run to see the result.
3Edit the programCurriculum practice

The program has two mistakes: the import is incomplete and so is the array creation. Fix them so it prints 2 then 1.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
import 

day_array = ('i', [2, 3, 4, 5, 6, 7, 1])
print(day_array[0])
print(day_array[6])
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Assign to round_array an integer array with the values 1, 2, 3, 4, 5, so elements 1 and 4 are printed.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
import array

round_array = ___
print(round_array[1])
print(round_array[4])
Console
Press Run to see the result.
5Write the programExtra practice

Your steps for 5 days are stored in steps_array. Add them up with a for loop and print Total: 29700.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
import array

steps_array = array.array('i', [4200, 6500, 3900, 8000, 7100])
Console
Press Run to see the result.
6Write the programExtra practice

Three cafeteria meals cost 120, 80 and 45 pounds, and every price goes up by 15. Create price_array with numpy, then make new_array by adding 15 to the array and print it.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
import numpy

Console
Press Run to see the result.

Key points

  • import array, then array.array('i', [...]) is an array of whole numbers only.
  • Read values by position from 0 and count them with len(), just like lists.
  • With numpy.array, an operation applies to every value at once.