Lesson video

In progress...

Loading...

Hello, my name is Mrs. Jones, and I'm really pleased that you're here to learn with me today.

Today's lesson is called Arrays and Lists, and we're going to look at the difference between an array and a list and how this can be used and created in Python.

So let's get started.

Welcome to today's lesson.

Today's lesson is called Arrays and Lists from the unit Programming: Strings and Lists.

And by the end of this lesson, you'll be able to describe the differences between lists and arrays and use a list in a programme.

There are four key words to today's lesson.

Static.

Static is a data structure that has a fixed size in memory.

Dynamic.

Dynamic is a data structure that can grow or shrink to accommodate items. Array.

Array is a static data structure that contains items of the same data type.

And list.

List is a dynamic data structure that can contain items from different data types.

There are two sections to today's lesson.

The first is describe the differences between lists and arrays, and the second is use a list in a programme.

So let's start with describe the differences between lists and arrays.

In computer science, you often need to manage large amounts of data.

You need to know how this data is stored and the types of operations that you can perform on it.

Data structures are used to store data in an organised and accessible way.

Data structures can be static or dynamic.

Static data structures reserve memory locations for a set amount of data.

Their size cannot change.

Dynamic data structures are more flexible.

The memory capacity is not fixed.

So characteristics of arrays, is an array holds multiple items under one name.

An array is a static data structure.

An array is fixed in size and can only contain data of the same type.

The contents can be changed, but items cannot be added or deleted.

And the size of the array must remain fixed.

Characteristics of lists are that a list holds multiple items under one name.

A list is a dynamic data structure.

The size of a list can change, which means that you can add and delete items. A list can also contain data of different types.

Let's have a quick check.

True or false: lists are dynamic data structures? Is that true or false? Pause the video to consider your answer and then we'll check.

Let's check your answer.

The answer was true.

Lists are dynamic data structures.

Well done if you got that correct.

Let's have another check.

Which of these is a characteristic of an array? Is it A, it can change size, B, its items can have different data types, or C, it has a fixed size? Pause the video to consider your answer, and then we'll check it.

Let's check your answer.

The answer was C, it has a fixed size.

Well done if you've got that correct, let's have a look at an activity, and you'll need your worksheet for this.

And in this activity, you need to match each characteristic into the corresponding data structure type, and you can see there you have different little snippets of information and different characteristics that need to be put into the section for arrays or the section for lists.

Pause the video, go back through the slides, and use your worksheet to have a go at completing this activity, and then we'll go through the answers.

Let's check your answers.

In the arrays characteristics is static data structure, fixed size, all items are the same data type, and cannot add or delete items. In the lists are dynamic data structure, size is not fixed, items don't have to be the same data type, and can add or delete items. Well done if you've got that correct.

Let's have a look at a second activity.

For the two scenarios below, decide which data structure would be most suitable and describe why.

On the left, we have, "Alex wants to store his shopping list.

He's still thinking about what he wants to buy.

He's looking at what he has in his cupboard and he's adding and removing items from his list." And on the right, we have, "Sofia wants to store the 12 scores of her netball team, which plays once a month over the year." For each, you need to choose which data structure is appropriate, and also justify why you've chosen that data structure.

Pause the video to consider your answer and then we'll go through the solutions.

Let's check your answer.

For the first one, which is, "Alex wants to store his shopping list.

He's still thinking about what he wants to buy.

He's looking at what he has in his cupboards and is adding and removing items from his list." The chosen data structure is a list because the size of the list is unknown at the start, he needs to be able to add and remove items, and there might be different data types.

Well done if you've got that one correct.

And the other one was, "Sofia wants to store the 12 scores of her netball team, which plays once a month over the year." And the chosen data structure for this one is an array because the size of the list is known at the start, she doesn't need to add to or remove any items, and the data type is the same.

They're all gonna be integers.

Well done if you got that one correct.

Let's move on to the second part of today's lesson, use a list in a programme.

A list is a dynamic data structure, which means that it can change size as elements are added or removed.

Each item within the list is referred to by index, which is position in the list.

Lists used zero-based indexing, which means they start at zero.

You can see on this table here we have the index zero, one, two, three, four, five, six with the planets underneath, and zero is the first position.

Alex says, "But I need an array data structure.

How do I do that in Python?" In Python, you can use a list as an array if you need to.

You must make sure that each element is of the same data type and you don't add or delete items from the list.

In Python, the syntax for a list is a comma-separated list of values, items, in square brackets.

You can see here, we've got planets, which is the name that's been given to the list, the identifier, equals, and then inside the square brackets, we have the planets, all separated by commas.

We have the identifier and we have the list items. In this example, the list items are string literals, pieces of text, so they need to be in quotation marks.

When the programme is run, this is what the list will look like in memory.

You can see there we have the planets, which is the identifier, and we have the positions, zero, one, two, three, four, five, six, with the corresponding planet next to it.

"Notice that the index of the first item in the list is zero, not one," says Izzy.

Let's have a quick check.

In this programme, what planet is held in index position one? is it A, Mercury, B, Venus, or C, Earth? Pause the video to consider your answer and then we'll check it.

Let's check your answer.

The answer is B, Venus, because remember that the first position is zero, which is Mercury, which means that index one is Venus.

Well done if you've got that correct.

To access an item in a list, you need to specify the index, the position, of the element that holds the item.

So here, we have our planets list again with the items stored inside those square brackets, followed by print, and inside the brackets, planets, and inside the square brackets, one.

Andeep says, "So this will print Mercury," right? And Izzy says, "No.

Remember, lists use zero-based indexing, so this will print Venus." Expressions can also be used to access list items as long as they evaluate to an interject.

Here we've got the planets list again, we have home equals two and we have print, and inside, we have planets, and then in the square brackets, we have home plus one.

Andeep says, "So this will print Mars, right? "Yes," Izzy says, "Because home plus one evaluates to three and Mars is in index position three in the planet's list." Let's have a quick check.

What code could you add to line five if you wanted to print the text Neptune? is it A, print, and then inside the brackets, planets, and inside the square brackets, three, is it, B, print, inside the brackets, planets, and inside the square brackets, five, or is it C, print, inside the brackets, planets, and inside the square bracket, four? Pause the video to consider your answer and then we'll check.

Let's check your answer.

The answer was B because it was in position five.

Well done if you've got that correct.

To access each item in a list, you can use a for loop, and there are two methods for doing this.

The first one is to use a for item in item list syntax.

In this example, the variable planet is being assigned to each item in the planets list in turn.

You can see there, we have our planets list, and on line five, we have for planet in planets, and then underneath, we have print planet.

Planet is a chosen variable within that for loop, and make sense link to the items inside that list, and the output on the right will be each of those items in the list in turn As it iterates through.

We have Mercury, Venus, Earth, Mars, Jupiter, Neptune, and Saturn.

The second way is use a for index in range, and then use the length of the item list syntax.

In this example, the variable R is being assigned to the value zero, one, two, three, four, five, six on each loop, which is used as the index to the list planets.

So again, here in the example, we've got the planets list.

On line five we have for i in range,, and then we're using len, and inside the brackets, planets.

So we're using the length of the planets list, and then output, print planets, in the square brackets, i, and we get the same output, Mercury, Venus, Earth, Mars, Jupiter, Neptune, and Saturn.

Let's have a quick check.

Choose the missing line to print out each pet on a new line.

In this one, we have a pet's list being set up with cat, dog, hamster, rabbit, and goldfish, all stored in that list.

For i in range len of pets, what's the missing line on line four? Is it A, print, inside the brackets, pets, inside the square brackets, i, is it B, print, inside the bracket, pets, inside the square bracket, one, or is it C, print, inside the brackets, pets, inside the brackets, i? Pause the video to consider your answer, and then we'll go through the solution.

Let's check your answer.

The answer was A, print, pets, and i is used in the square brackets, linking to the i that is used in the for loop.

Well done if you've got that correct.

In this example, a list is iterated through and each item is concatenated to a string called my string, which is printed out in each loop.

And we have my list equals, and inside the square brackets, we have cat, dog, mouse, goat.

We have my string equals, and that is initialised as empty, just the quote marks there with nothing inside.

And then we have for item in my list, my string equals my string plus item, print, my string, and the output would be cat, catdog, catdogmouse, catdogmousegoat, as each time it iterates over the list, it adds a new item to the string.

Alex asks, "How would you add spaces between the words?" We would do that by adding that on line four, and we've got my string equals my string plus, and then inside those quote marks, we have a space, plus item.

We are concatenating those together with a space as well.

And that time we have cat, cat dog, and you can see the space is now visible as each output occurs.

You can use the random choice method to pick a random item from a list.

The programme below picks a random item from two lists and concatenates them before printing it out.

And notice in here, we've got the use of comments, that is the hashtag, so that is ignored by the programme when it's run, it is there just for us and for the programmer to look at to understand what's happening.

So we have import random, we have a list that is set up called flavours, storing chocolate, cherry, cheese, and toffee.

We have types, another list set up storing cake, pie, muffin, pancake, and crumble.

We have chosen flavour equals random dot choice, and then in the brackets, flavours, chosen type equals random choice, and then inside the brackets, types, generated dish equals the chosen flavour, plus a space in between those quote marks, plus chosen type, print generated dish.

And an example is on the right there, where we have cherry crumble.

And because it's a random programme, every time it runs, you'll get a different output.

Let's have a look at an activity.

I want you to write a programme to generate a random superhero name, and you'll need to create two lists.

The first is a list of at least five superhero prefixes, for example, prefixes, and we've got that set up storing Captain, Mighty, Invisible.

These are just examples.

And the other list is created with five superhero suffixes, and there we've got it storing Falcon, Titan, Blaze.

You choose which ones go in there.

Pick a random prefix and a random suffix.

Create a name by concatenating these with a space in between and print out the final name.

Pause the video and have a go at creating that programme, and then we'll go through a solution.

Let's have a look at a solution.

Here, we've got import random, prefixes, and we have a list storing different prefixes, we have suffixes, and we've got a list storing those, and then we have chosen prefix equals random dot choice, and inside the brackets, prefixes, chosen suffix equals random dot choice, and inside the brackets, suffixes, superhero name equals chosen prefix plus a space inside those quote marks, plus chosen suffix, and print superhero name.

And an example on the right there is Shadow Siren, remembering that it'll be random each time you run it.

Well done if you created that programme.

Let's have a look at the next activity, and this time, I want you to modify the previous programme to print out 10 random superhero names.

You'll need to add a for loop to repeat the previous programme 10 times to create a list of 10 random superhero names.

Add more suffixes and prefixes to make the output more interesting.

Pause the video and have a go at modifying your programme, and then we'll go through the answers.

Let's have a look at a solution.

Here, we've got the same as before with the import random and the two lists set up for prefixes and suffixes, and then from line eight we have for i in range 10 chosen prefix equals random choice prefixes, chosen suffix equals random choice suffixes, superhero name equals chosen prefix plus the space in the quote marks plus chosen suffix, and print superhero name.

And you can see on the right, we have 10 outputs because that loop is running 10 times, so it's iterating over those instructions that are indented underneath and outputting 10 random combinations of prefixes and suffixes.

Well done if you completed that programme.

In summary, data structures are used to store data in an organised and accessible way.

Two data structures are arrays and lists.

Arrays are static data structures, meaning they cannot have items added or removed, and they must contain items of the same data type.

Lists are dynamic data structures, meaning they can have items added or removed and can store different data types in the same list.

Well done for completing this lesson on arrays and lists.