Python: Creating Numerical Lists

Scalar

So far we have looked at inputting a single number as a Variable, this is known as a Scalar. Lets examine this in more detail:

Python

Diagrammatically we can look at this number as:

We can use the length function to measure the length of the scalar variable s:

Python
1

This returns the value of 1. In other words the scalar can be measured to have a length of 1 as shown. Diagrammatically this means that the length of the scalar can be measured, starting from 0 to an end length of 1.

Thus the value of the scalar at the 0th position can be called using the following line:

Python
90

If we try to get the value of a at the 1st position we get an error:

Python
IndexError: list index out of range

This is because there is no value at position 1. The position of the number 90 is at 0 and this spans up to 1 but doesn’t include 1.

Vector

For many applications we require a list of numbers. These can be arranged in many layouts depending on the application, let us look at the case of a simple row vector. This has 3 numbers, 90 corresponding to position 0, 92 corresponding to position 1 and 94 corresponding to position 3. The length is therefore 3. Note that a row Vector has 1 row and multiple in this case 3 columns:

We can write these numeric values using square brackets but we will need to have a character to separate out the three numbers entered. This character is known as a delimiter and in Python we use the comma:

Python

We can then look at rv, the length of rv and the values in the 0th, 1st and 2nd index using:

Python
[90, 92, 94]
Python
3
Python
90
Python
92
Python
94
Python
IndexError: list index out of range

Once again when indexing (selecting the value from the variable rv at a specified position) we start from the value 0 and go up to the length of the variable without including the last value. So we start from 0 and go up to 3 but don’t include 3 itself.

We can also index from the end of rv, for instance:

Python
94
Python
92
Python
90

Once again if we go over the limit, we’ll get the error:

Python
IndexError: list index out of range

We can index multiple elements of the row vector for instance, position 0 and 2 using:

Python
[90,94]

Column Vector

In order to express data as a Column Vector, that is a Vector that has multiple rows in this case 2 and a single column we can use additional square brackets.

Python

To get the 0th element we can use:

Python
90

This returns the value of 90 as expected.

To get the 1st element we can use:

Python
80

This returns the value of 80 as expected. We can look at the length of this and get a value of 2. However if select an individual element within this column vector, i.e. a single row we get 1

Python
2
1

This can be used to determine the number of rows and columns in this column vector.

Python

The first command, returns the number of rows, in this case simply think of it as acting on the outside square brackets.

Python

The second command, returns the number of columns, in this case 1 think of it as acting on the inside square brackets.

Matrix

Now we can look at Matrices which have multiple rows and multiple columns.

We can type the Matrix above in Python using:

Python
r1=[90,92,94]
r2=[80,82,84]
m=[[r1],[r2]]
m_v2=[[90,92,94],[80,82,84]]

If we attempt to get the dimensions however we only get a single number:

Python
2

Which is the number of rows. This is specified from the outside brackets. Looking at m which is of the form:

Python

We can see where the value comes from. i.e. Python only looks at the length of the data with regards to the outside brackets. We can get the dimensions of the other axis by selecting only the oth row or 1st row and then querying their size:

This is:

Python
[90,92,94]
[90,92,94]
Python
[80,82,84]
[80,82,84]

Querying the length of either row gives:

Python
3
3

To index a value, in this case 92 we know it is on the 0th row and 1st column:

Python
[[90,92,94],[80,82,84]]

[90,92,94]

92

For practice try to index the number 84, 90, 94, 80 and 82 from the matrix above.

3D Array: Book

It is also possible to create a Book. In this case we will look at creating the following 3 page book which has Page 0, Page 1 and Page 2:

This can also be visualised as:

HTML

To look at the length of pv2 we can look at:

Python
3
2
4

This gives us the values of 3 Pages, 2 Rows and 4 Columns respectively.

To index a value, in this case 54 we know it is on the 2nd page, oth row and 2nd column:

Python
54

For practice try to index the value 40, 70, 72, 92 and 84 from the data above.

Advertisements