Loading...
Hello, my name is Mrs. Holbrook and welcome to Computing.
I'm so pleased that you've decided to join me for the lesson today.
This is one of my favorite topics, programming.
We're going to be exploring how we can use list operations to alter the contents of a list.
Welcome to today's lesson from the unit, Python programming with sequences of data.
This lesson is called Python List Operations and by the end of today's lesson you'll be able to add and remove items and sort data held in a list.
It would be useful if you have access to a device that you can create and amend code for this lesson.
Shall we make a start? We will be exploring these key words throughout today's lesson.
Append, append, adding to an existing data structure.
Remove, remove, taking an item away from an existing data structure.
List method, list method, a built-in function that will perform an operation on a list.
Sort, sort, putting items in a particular order.
There are two parts to today's lesson.
We'll start by using methods to alter the contents of a list and then we'll move on to order items in the list by using a sort.
Let's make a start by using methods to alter the contents of a list.
What would be displayed when this program is run? Maybe pause the video here and have a think.
The program will print one because this is the index position that holds the item Venus.
Remember in Python, list indexing starts at zero.
A list is a data structure that can be used to hold multiple items of data.
Once a list is created though, you may want to change the list by adding, removing or updating the items held in the list.
In Python, a list is a dynamic data structure, which means that it can change size as elements are appended or removed.
There are a number of list methods that can be used to alter the contents of a list and perform operations.
In this table, I've included some of the more common ones.
So the first one is list.
append.
This is used to add an item to the end of a list.
So you put the item you want to add to the end of the list in the brackets after the word append.
So here's an example.
I've got a list called planets, so I'm going to put planets.
apend, open brackets, and then because I'm adding a string, I've surrounded it by speech marks and I'm adding Neptune.
The next one is list.
insert.
Rather than just adding an item to the end of the list, this can be used to add an item at a set index position.
So this method has two arguments.
Firstly the index position where we want the item to go, and then secondly, the item itself.
So my example is planets.
insert into position three and then Mars.
List.
pop is used to remove a item at a set index position.
So planets.
pop2 would remove the item held in index position two.
List.
removeitem will remove a specific item, so it will search through the list until that item is found and then it will remove it.
So planets.
remove, and then I've got Venus in my speech marks.
List.
index and then item in brackets will search for the item number of an index.
My example is planets.
index Saturn.
So this will return the index position where the item Saturn is held in the list.
And then the last one is list.
count and then item.
This will count the number of item appearances.
So for example, if something appears more than once in my list and I want to count them.
So here's an example.
Planets.
count, and then I've used Earth.
Jun's got a great question.
"What's the difference between list.
append and list.
insert, don't they just both add a new item?" Ah, Sam's got a great response.
"They do both add an item to the list, but list.
append will only append an item to the end of the list, whereas list.
insert lets you specify where in the list the item is added.
Time to check your understanding.
Which list method would you use to add an item to the end of a list? Is it A, list.
append.
B, list.
insert, or C, list.
add? Pause the video whilst you have a think.
That's correct, A, list.
append could be used to add an item to the end of the list.
We'd probably only use list.
insert if we wanted to specify a particular location for the item.
Jun has a question, "What if I want to remove the last item from a list, but I don't know how long the list is or what value is held in the last position." That's a really good question.
Maybe pause the video and have a think about a solution.
Ah, Sam's got a great response.
"You can use list.
pop and just not specify an index number.
This will automatically remove the last item in the list.
For example, planets.
pop.
You don't need to know how long the list is or what value is held in the last position." Time to check your understanding.
Which list method would you use to remove the last item of a list if you don't know how long the list is? Is it A, List.
pop with four in brackets, B, list.
pop with empty brackets or C, list.
remove item in brackets, pause the video whilst you have a think.
Did you select B? Well done, list.
pop with empty brackets will automatically remove the last item in the list.
Okay, we've come to our first set of tasks for this lesson and you're doing a fantastic job so far.
So well done, I'd like you to start by opening the starter program at Oak.
Link/planet-lists.
For part two, complete line six, so that Uranus is added to the end of the list of planets.
For part three, run the program to make sure that Uranus is included in the list of planets known by 1930.
For part four, complete line seven and eight so that Neptune and Pluto are also added to the end of the list of planets.
A sample output is provided at the bottom of the screen to show you what the program should look like when you run it.
Pause the video here whilst you complete the activity.
For part five, in 2006, Pluto was declassified.
Complete line 11 to remove Pluto from the list of planets.
And then for part six, run the program to make sure that Pluto is not included in the list of planets after 2006.
Again, a sample output has been provided for you.
Pause the video whilst you complete the activity.
How did you get on? Did you manage to correctly complete your programs? Great work, let's have a look at the code together and fill in the gaps that you may have had.
So on line six I've added the code planets.
append, and then I've opened brackets and in speech marks I've added Uranus and then I've closed my brackets.
On line seven and eight, I've used the same code, but this time I've added Neptune and Pluto.
On line 11, I've then put planets.
remove, and then I've put Pluto in speech marks to remove Pluto from the list so that it will then print out planets after 2006 with Pluto removed from the list.
Remember, if you need to make any corrections to your code or go back and have another look, you can pause the video now and do that.
We're now moving on to the second part of today's lesson where we are going to order items in a list by using a sort.
You're doing a fantastic job so far, so keep going.
The list you've seen so far have not been in any specific order.
Sometimes it's useful to sort items held in a list into an order for use within a program.
For example, you may have a list of scores from a game and want to sort them in order to easily find the highest score.
So here I have my list with index zero through to four, and then I have my values.
So index position zero holds five, one holds two, two holds 10, three holds seven and four holds four.
This list is currently not sorted.
Jun says, "I could use a sort, put a list of names in order alphabetically." That's a great idea.
Sam says "I could use a sort to put the scores from my game into order from highest and lowest and then remove the lowest score from the list using score.
pop." That's another great idea.
Well done Jun and Sam.
There are two list operations that you can use to sort values held in a list.
List.
sort can be used to sort a list from lowest to highest, which is what we call ascending order.
So here I've got my original list of scores and after I've used scores.
sort, you can see that the order of the list has changed.
Position zero now holds two and position four holds 10.
If the list contained text instead of numbers, the list would be sorted alphabetically from A to Z.
List.
sort open brackets reverse equals true can be used to sort a list from highest to lowest or what we call descending.
So here's the example of my scores list again.
If I write scores.
sort reverse equals true, you can see that in position zero I now have 10, and in position four I have two.
If the list contain text instead of numbers, the list would be sorted alphabetically from Z to A.
Time to check your understanding.
Which list operation has been used to sort this list? Is it A, scores.
sort, open close brackets.
B, scores.
sort open brackets reverse equals true close bracket, or C scores.
append open close bracket.
Pause the video whilst you have a think.
Did you select A, well done.
Scores.
sort has been used to sort this list because the list is sorted in ascending order from lowest to highest.
What list operation has been used to sort this list? Is it A, scores.
sort open brackets, close brackets.
B, scores.
sort open brackets, reverse equals true, close brackets.
Or C, scores.
append open close brackets.
Pause the video whilst you have a think.
That's right, it's B, scores.
sort reverse equals true has been used because this list has been sorted in descending order, from highest to lowest.
We're now moving on to our final set of activities for today's lesson and in this activity, you will develop a two player dice game.
One player is the attacker and the other player is the defender.
The attacker rolls three dice and the defender rolls two dice, each player's dice are sorted in descending order, highest first.
The attacker's highest role is compared to the defender's highest role.
The player with the lowest role loses a point.
If the two roles are equal, the attacker loses a point.
Then the attacker's second highest role is compared to the defender's second highest role and the player with the lowest of the two loses a point.
If the two rolls equal, the attacker loses the point.
Let's have a look at an example.
Here, the attacker has rolled three dice, a six, a four and a three.
The defender has rolled two dice, which are a four and a two.
So the roles have been sorted in descending order.
In the first game, the defender loses a point because their dice has a lower value than the attacker's.
In the second game, the defender loses a point as well because their dice is a value of two compared to four.
Let's have a look at another example.
This time the attacker has rolled a six, a three and a two and the defender has rolled a six and a four.
Again, the rolls have been sorted in descending order.
In the first game, the attacker loses a point because it's a draw.
In the second game, the attacker loses a point because the defender has four and the attacker has three.
For part one, open the starter program Oak.
link/dice-roll-game.
For part two, complete line nine so that the attacker list containing the attacker's dice rolls is sorted.
For part three, complete line 10, so that the defender list containing the defender's dice roll is sorted.
And then for part four, run the program and check that the lists are printed correctly.
There's a sample output here to show you what this should look like.
Pause the video whilst you complete the activity.
For part five, add an if statement to your program that compares the highest dice roll in the attacker list to the highest dice roll in the defender list.
If the attacker wins, decrease defender points by one.
If defender wins, decrease attacker points by one.
For part C, remember that if the highest rolls are equal, the defender wins.
Finally, for part six, add another if statement to your program to compare the second highest dice roll, pause the video whilst you complete your program.
How did you get on? Did you manage to complete your game? Great work, well done.
So here's the code, I know it's a little bit small, so there is a full solution at Oak.
link/dice-roll-game-solution, but let's pick out some key points.
So on line nine and 10, I've sorted both the attacker and defender lists.
And remember, we wanted these to go from highest to lowest.
So I've used sort open brackets reverse equals true close brackets.
Starting on line 14, I have my if statements, which are used to compare the dice rolls.
So if attacker position zero is greater than defender position zero, the defender underscore points is equal to defender underscore points minus one.
Taking a point away from the defender.
Else attacker underscore points is equal to attacker points minus one, taking a point away from the attacker.
And then I've got a second if statement on line 18, which compares the second dice roll, which is held in the list item position one.
You have done an absolutely fantastic job today and I hope you have enjoyed this lesson.
Let's summarize what we've learned together.
Operations can be performed on lists using list methods such as appending, removing and inserting items. Appending an item will add it to the end of the list, whereas inserting we'll place an item at a particular index location, which you can specify.
A list can be sorted in either ascending or descending order to help with the processing of data.
I hope to see you again soon, bye.