- [Craig] In this video, we take a look at using one- and two-dimensional structured data types.
(uplifting piano jingle)
So first of all, what actually is a data structure?
Well it's a specialised format for organising and storing data,
and you've been using a number of them already,
things like arrays, lists files, records and tables.
You've actually been using very basic data structures ever since you first started to program
in the form of characters, strings, integers, reals and Booleans.
You already know that a string is a collection of alphanumeric characters,
whereas a char or character data type is a single character.
Although you use the string data type, behind the scenes,
it's actually a data structure made up of a series of separate character data types.
There are many much more complex, or what we call higher-level data types,
and you'll start to be exposed to lots of these at A Level.
But they all work in fundamentally the same way,
in that they allow you to organise and store data
in a certain way, following certain rules.
So an array can be thought of like a variable which can contain more than one data item.
For example, we could be storing a list of names,
and we can do that by allocating a continuous part of memory to storing that data.
So you can see here we have a number of memory locations in memory,
and starting at memory address five, we've then started storing contiguously a list of names,
which will be part of our variable.
Note that lists, which are probably what you might be familiar with if you're programming in Python,
are different to arrays because they're not contiguous.
But for the purpose of your exam, you can think of arrays as storing contiguous data items.
So our program will know where in memory our array starts; in this case, address five.
And we can therefore use an index relative to the start point
to allow us to easily access the array's contents.
So notice arrays are typically zero-indexed, which means Jane is at index two, not index three.
It is in the third position in the array,
but because we start at zero, it's at index two.
So we're going to use some code example here from Python.
Now, just a quick note.
Python does actually use arrays,
but it has methods that allow them to be used like lists by a programmer.
And that's why most people think Python only supports lists.
But this little extra level of complication is beyond the spec.
For all intents and purposes, at GCSE,
you can effectively think of lists and arrays as the same things,
although that's not strictly true.
So here in this code example, this line sets up a one-dimensional array of strings called countries.
It assigns it some initial values,
Angola at index zero,
Austria index one and Belgium at index two.
And again, note how the indexes typically start at zero and not one.
To output Austria from the array,
we would therefore need to use index one,
as index one represents the second item in our countries array.
Arrays are a static data structure,
and that means you can't change the size of them once you've set them up.
Now this is a good example of where Python is allowing the array to behave like a list.
Although you can't tell because this has been abstracted from you,
Python is actually moving the entire data structure to a new part of the memory
to ensure it's contiguous.
So with Python, we can insert an item into the new location at the end of the array using its index.
So here we've added Canada to the third index or the fourth position in the countries array.
You can also have two-dimensional arrays,
and you can visualise in your head a two-dimensional array as looking like a table,
where we now need two sets of indexes,
one to index the rows and another to index the columns.
So to access Angola, you'd be going to index zero zero, as shown here.
Countries zero one contains the integer 1246700,
and countries one one contains the value 83871.
So let's just recap what we've covered.
An array is a static number of related data items that are stored together in the same memory space.
Each data item has to have the same data type,
and the particular data item, the element, is found using the index for the array.
Indexes usually start at zero for the first data item, known as a zero-indexed array.
And arrays may be single- or multi-dimensional.
You can visualise dimensions as a column when it's a single-dimensional array
or a table where it's a two-dimensional array.
Now a quick note and something which is often asked by students and teachers,
that when we say index one zero, we could be accessing value 112.
It depends how you actually choose to store the data behind the scenes.
There's not a fixed way of referencing the data in an array.
You don't go, for example,
always down the X axis and then the Y axis.
It doesn't matter when you visualise rows or columns first,
as long as you're consistent in your program.
So read the questions carefully in an exam so you are sure which way round the data is being stored.
So let's focus your attention on the actual PLS
and the actual Python commands for lists that you need to know
and be aware of and you could be expected to use in the paper two on-screen exam.
We've got list dot append, we've got delete list, list dot insert, and how to create an empty list.
And we're going to provide some examples of how you could use each of those now.
So first of all, let's look at the bottom one, creating a new, empty list.
There's a couple of ways you can do this, and they're shown in the table.
We've done one on the example in the top left,
we've given a name to our list,
my new list, equals,
and then we've used the word lists with open and closed parentheses.
And that creates a blank, empty list called my new list.
Now let's look at appending to a list.
So lists can grow in size dynamically,
and this is one of the benefits of lists in Python,
You simply write the name of your list.
So in our case, it's my new list,
followed by a dot,
followed by the word append.
In brackets, you then provide the item you want to append to the list.
So in this case, I'm appending the string Craig,
followed by the string Dave.
When I then run the line of code print my new list,
you can see in the output window on the bottom right,
my list now contains those two strings.
You'll notice from the previous example that appending always added items to the end of the list.
So Craig went in first and then Dave went in after it.
You can use the dot insert command to insert an item into any position in a list.
So we already have Craig and Dave in our list,
but by saying my new list dot insert,
we can now specify two parameters in our brackets.
The first is the position, so I'm going to say zero.
Don't forget, lists start at zero.
And then the second is the item I want to insert, which is the string Sam.
That should insert Sam at the beginning of the list,
and obviously, it will then shunt Craig and Dave over to the first and second position.
As you can see from the output window when I print my new list, that's worked as expected.
We can also delete items from a list.
We use the keyword DEL here for delete,
followed by the name of the list.
And then notice now we supply square brackets because we're specifying an index,
and we say which index item in the list we want to delete.
So I want to delete the item at index position zero, which was Sam.
It also then goes ahead and shunts everything else in the list down.
When I print the content of the list, it's now back to reading Craig and then followed by Dave.
Now that's where the specification stops because in the exam,
you only need to know about one-dimensional and two-dimensional arrays.
If you're interested to know about three-dimensional or higher-dimensional arrays,
then you can watch the rest of this video but there's no need to take notes.
And higher-dimensional arrays is something you'll probably cover at A Level.
So you already know that you can visualise, as an abstraction,
a one-dimensional array as a simple column and a two-dimensional array as a table.
Well it doesn't take much of a stretch then for you to realise
that a three-dimensional array in your head could be visualised as a cube.
You could reference any item in a three-dimensional array
by giving three index values,
one for the height, one for the length,
and one for the depth.
So here, we're accessing two two two,
we're going down two, across two and two rows deep into our cube.
Now at that point, people think well surely we can't go higher than a three-dimensional array.
How could we represent a four-dimensional array?
And although that can be a difficult concept for us to wrap our heads around
because we live in a three-dimensional world,
a computer doesn't care about that.
It can easily have a fourth-dimensional or fifth-dimensional or higher-dimensional array.
And actually, it's not that hard for us to visualise, even though we only live in a three-dimensional world.
So this is how you could potentially visualise a four-dimensional array.
We just take a set of cubes.
We'd have to supply four indexes to this array to access any one of the elements.
The first index specifies which cube we look at,
and then the next three specify the width, the depth and the height.
In a similar manner, we could carry on visualising fifth, sixth, seventh and higher-dimensional arrays,
although this does get a little confusing.
So here we have a fifth-dimensional array with one suggestion of how you could visualise it.
You'd have to supply five index values to access any particular element in a five-dimensional array.
The first two here could be telling us the row and the column of the cube to access
and the final three, the row, column and depth of that cube.
Now it's unlikely you're ever going to be dealing with five-dimensional arrays even at A Level,
but it just shows how computers are not really limited
and we can often take concepts to the next level quite easily
once we understand some of the basics.
So that's the theory content of the video covered.
We're just going to spend a little bit of time talking about the programming language subset.
If you've heard this before,
you can skip the rest of this video.
Otherwise, it's definitely worth a quick watch.
So the exam board Edexcel have released a document that they call the programming language subset,
which you can see here on the right.
This document specifies which parts of Python 3 are required
in order that the assessment can be undertaken with confidence.
Students familiar with everything in this document
will be able to access all parts of the paper two on-screen assessment.
The exam board will not go beyond the scope of this document
when setting you any questions in the paper two exam.
Any students who do successfully use more complex methods which are not included in the document
will still, however, be awarded marks.
Further to this, you will see here from the front of the exam paper
that you are actually going to be supplied with a printed and an electronic copy of the PLS
during your paper two exam.
Well, if this document outlines everything you need to know
in order to program in Python during the exam,
and you can be guaranteed that you can answer all the questions
using all the techniques in this document for full marks,
then you might as well get used to using it now.
Ask for a copy from your teacher
and have it as a reference guide at all times throughout your GCSE course.
It will be invaluable.
(uplifting piano jingle)