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.
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))2 1 7
| Part | Meaning |
|---|---|
| import array | import 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:
import array
day_array = array.array('i', [2, 3, 4, 5, 6, 7, 1])
print(day_array)
for day in day_array:
print(day)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:
import array
warn_array = array.array('i', [2, 1, "Caution"])
print(warn_array)TypeError: 'str' object cannot be interpreted as an integer
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:
import numpy score_array = numpy.array([42, 58, 71]) bonus_array = score_array + 5 print(bonus_array) print(score_array * 2)
[47 63 76] [ 84 116 142]
[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
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.
import array
Press Run to see the result. Print the length of warn_array.
import array
warn_array = array.array('i', [2, 2, 1, 1, 2, 1, 2])
Press Run to see the result. The program has two mistakes: the import is incomplete and so is the array creation. Fix them so it prints 2 then 1.
import
day_array = ('i', [2, 3, 4, 5, 6, 7, 1])
print(day_array[0])
print(day_array[6])Press Run to see the result. Assign to round_array an integer array with the values 1, 2, 3, 4, 5, so elements 1 and 4 are printed.
import array round_array = ___ print(round_array[1]) print(round_array[4])
Press Run to see the result. Your steps for 5 days are stored in steps_array. Add them up with a for loop and print Total: 29700.
import array
steps_array = array.array('i', [4200, 6500, 3900, 8000, 7100])
Press Run to see the result. 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.
import numpy
Press Run to see the result. Key points
import array, thenarray.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.