Table of contents

## Tutorial Video

## Scalar

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

Diagrammatically we can look at this number as:

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

`TypeError: object of type 'int' has no len()`

This returns an error stating that an integer has no length. If we try and index into it we will likewise get a similar error.

`TypeError: 'int' object is not subscriptable`

## Scalar within a List

We can instead create a scalar within a list by using square brackets.

We can compare i and s in the variable explorer and we see that i is an int and s is a list.

Double clicking on s gives:

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:

`1`

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

`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.

## Row Vector (A List of Individual Values)

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:

The row vector is shown in the variable explorer. Note because it is a list it shows as yellow.

Opening up rv in the variable explorer we can see each individual value in each index. Note each individual index contains an integer and these are colour-coded purple. For convenience the row vector rv is displayed as a column when expanded.

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

`[90, 92, 94]`

`3`

`90`

`92`

`94`

`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:

`94`

`92`

`90`

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

`IndexError: list index out of range`

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

`[90,94]`

## Column Vector (A List of Lists with Individual Values)

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.

Note that this is a nested list and shows up with the colour grey.

If expanded each index now contains a list (a list with a single value).

To get the 0th element we can use:

`[90]`

This returns a list with a single value. However because this is a list itself, it can be indexed into again to give the numerical value:

`90`

This returns the value of 90 as expected.

To get the 1st element we can use:

`[80]`

This returns the list with a single value.

`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 selecting an individual element within this column vector, i.e. a single row we get 1

```
2
1
```

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

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

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

## Matrix (A List of Lists with Multiple Values)

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

We can type the Matrix above in Python using:

We can see m1, m2 and m in the variable explorer:

Let's expand m, we can see that index 0 is a list and index 1 is also a list.

These can be expanded, here is index 0, this contains 3 elements (index 0, index 1 and index 2)

Here is index 1, this once again contains 3 elements (index 0, index 1 and index 2):

m can be created in a single line. We will however use another variable name n, so we can view m and n side by side:

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

`2`

However if we index into the 0th index, and get the length of that we can see that it is 3:

`3`

If we try to index into the 0th index of the 0th index which gives us an integer and measure the length we get an error:

`len(m[0][0])`

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:

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

```
[[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 (A List of Lists of Lists)

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:

This can be created by creating 3 individual matrices, where each individual matrix is a single page. Then creating a list of these pages gives the book:

Let's now look at the variable explorer.

We can see that b looks quite complicated. However we can expand it:

Let's look at the 0th index, it is a list of size 2 (for the length we are only interested in the outside brackets and the number of elements between the outside brackets). Think of it as:

`[[...],[...]]`

Each value is of the type list. This can be accessed using:

This gives the following:

In this level we are now presented with two more indexes. Let's look at the 0th index, here we can see that is a list of size 4. Let's once again have a look at the 0th index. Recall that this is:

In this level we are now presented with four more indexes, and each of these is an int.

We can once again look at index 0 and access it using:

`90`

Because this is an int, the following will yield an error:

`TypeError: 'int' object is not subscriptable`

b can be created in an individual line using the following. We can use the variable name c so we can compare it side by side to b:

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

```
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:

`54`

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