Arrays#

Arrays are used to store data much like lists. Arrays are defined by the Numpy module which needs to be imported to work with arrays. Once a Numpy array is defined there are many powerful functions that the Numpy package provides. Some of these can quickly create arrays such as the numpy.arange() function.

import numpy as np

Creating our first array. Above we import numpy and label all numpy functionality with the np prefix. Below our array is stored in the variable, firstarray. Note that the np.array() takes a list as an argument as made clear by the [0,1,2,3,4].

firstarray = np.array([0,1,2,3,4])
firstarray
array([0, 1, 2, 3, 4])

We can make arrays using numbers generated from other functions or packages such as the math package.

import math as m
np.array([ 1, -1, m.pi, m.e, max(0,21.5,-4.4,19)])
array([ 1.        , -1.        ,  3.14159265,  2.71828183, 21.5       ])

Accessing elements of an array#

firstarray
array([0, 1, 2, 3, 4])

Now to access an individual element (value) we can use an index where the first element is zero as demonstrated below

firstarray[2]
2

We can also slice the array using ranges of indices such as firstarray[1:3] which takes the 2nd amd 3rd but not 4th element

firstarray[1:3]
array([1, 2])
interesting_array = np.array([ 1, -1, m.pi, m.e, max(0,21.5,-4.4,19)])
interesting_array
array([ 1.        , -1.        ,  3.14159265,  2.71828183, 21.5       ])
interesting_array[2:4]
array([3.14159265, 2.71828183])

A blank after the colon includes everything to the end or begining of the array

rightofarray = interesting_array[2:]
rightofarray
array([ 3.14159265,  2.71828183, 21.5       ])
leftofarray = interesting_array[:2]
leftofarray
array([ 1., -1.])

np.arange()#

A very powerful way to create custom arrays is np.arange() which takes up to three arguments, np.arange(start, stop, step), where the defaults for start and step are 0 and 1 by default.

array2 = np.arange(5)
array2
array([0, 1, 2, 3, 4])

Defaults of 0 and 1 are assumed above

array3 = np.arange(0,5,1)
array3
array([0, 1, 2, 3, 4])

Odd numbers using np.arange()

odd = np.arange(1,10,2)
odd
array([1, 3, 5, 7, 9])

We can carry out operations on arrays. In this case raise 2 to the power of each element in odd.

2**odd
array([  2,   8,  32, 128, 512])

We can create arrays with mixed data types but many of the numpy operations can not be performed on these arrays

np.array(["one",2,3,4])
array(['one', '2', '3', '4'], dtype='<U21')
2*np.array(["one",2,3,4]) # Causes an error
---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
/tmp/ipykernel_2015/2127543694.py in <module>
----> 1 2*np.array(["one",2,3,4]) # Causes an error

UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('int64'), dtype('<U21')) -> None

When we apply operations on list such as multiply there is no error but the effect is very different

2*["one",2,3,4]
['one', 2, 3, 4, 'one', 2, 3, 4]