Loading...
Hello, my name is Mrs. Holborow, and welcome to computing.
I'm so pleased that you've been able to join me for the lesson today.
Today's lesson is all about one of my favorite topics, programming.
In this lesson, we're going to be exploring how we can create a list in Python to store data.
Welcome to today's lesson from the unit, "Python programming with sequences of data." This lesson is called, "Creating lists in Python." And by the end of today's lesson, you'll be able to create a list in Python and use selection to access and display list items. Shall we make a start? We will be exploring these keywords during today's lesson.
Selection.
Selection, used when there is more than one possible path for a program to follow.
List.
List, a collection of data in a particular sequence.
Data structure.
Data structure, an organized way to store data for easy use.
Index.
Index, the location of an item or element in a list or string.
Today's lesson is broken down into three sections.
We'll start by using selection statements to control program flow.
We'll then look at how we store and access items held in a list.
And then we'll finish by using selection to display items from a list.
Let's make a start by using selection statements to control program flow.
Selection is used when there is more than one possible path for a program to follow.
It allows the program to run sequences of code depending on whether a condition evaluates to either true or false.
A program checks the condition and then selects the path of action.
Let's have a look at the example of the flow chart I've got on the slide.
I've got a decision or selection statement which says, "Is age greater than or equal to 18?" If that's true, it's going to display an adult ticket.
If it's false, then it's a child ticket.
So the path of action is different depending on the selection statement.
In programming, selection is carried out using "if" statements.
So you can see on the left-hand side I've got the syntax of an "if" statement.
So "if," "condition," and then colon and a block of code which is nested inside that "if" statement.
And then on the right-hand side, I've got some sample code.
The conditions would be replaced with actual conditions, and the blocks of code would be replaced with actual lines of code.
The "if" statement checks the condition.
If true, it runs the code within its block representing one path of the program's flow.
If false, the block of code under the condition is skipped, and the program continues.
For each possible path through a program, you will need a selection statement.
So here, I've got an example with an "if" statement, two "else", "if" or "elif" statements, and then an "else." You can have as many "elif" statements as you need, depending on how many different paths you need through your program.
Time to check your understanding.
You can only have one "elif" statement in a selection statement.
Is that true or false? Pause the video whilst you have a think.
Did you select false? Well done.
It's false because you can have as many "elif" statements as you need depending on how many paths you need for your program.
Take a look at the code that's on my screen.
What would you do to extend this program to print the message, "It's a weekday" if the day is between 0 and 4.
And "It's a weekend," if the day is 5 or 6.
Note the program uses an integer for each day of the week ranging from 0 for Monday, to 6 for Sunday.
Maybe pause the video whilst you have a think.
Ah, Andeep's got a great idea.
Andeep says, "I could add some 'if' statements." Here, we've extended the code to include some "if" statements.
If the variable "day" is less than or equal to 4, the message, "It's a weekday" is printed.
Else the message, "It's the weekend!" is printed.
Time to check your understanding.
What would be printed if the user enters 4 when this program is run? Pause the video whilst you have a think.
The program will print, "It's a weekday," as the selection statement is less than or equal to 4.
Did you have that as your answer? Great work.
In Python, if you want to join text and the value held by a variable together in a print statement, you can use a formatted string or f-string.
So this code here that I've got has a print statement which asks the user, "What day is it today?" And then on line two, it stores the user's input in a variable called, "day." On line four, I have my print statement which uses an f-string to combine some text and the value held in the variable, "day." The format of this is, I've written the word "print" and I've opened the brackets.
I've then typed in f to represent the f-string.
I've then opened my speech marks and started writing any text that I want to have.
But if I want to display anything other than text, I need to make sure I put the curly brackets or braces around it.
So you can see here the variable, "day," is surrounded by curly brackets.
I then finish off my print statement by closing my speech mark and closing my bracket like normal.
Okay, you've done a fantastic job so far in today's lesson, so well done.
We're moving on to the first set of tasks for today's lesson.
I'd like you to start up by opening the starter program, oak.
link/days-of-week.
Complete line five so that the value of remaining is the number of days left until the weekend, including the current day.
Then insert the line below into your program, wherever you believe it's appropriate to display the number of remaining days to the user.
So the line of code is print(f"{remaining} days until the weekend").
Pause the video whilst you complete the activity.
For part four, I'd like you to extend the program so that Friday, day four, is treated differently with a different message displayed than for the rest of the weekdays.
The message that should be displayed for Friday is, "It's Friday, just a day left until the weekend." As a tip, you'll need to use "if," "elif," "else," since there will now be three possible paths in your program.
Pause the video whilst you complete the activity.
How did you get on? Did you manage to complete your code? Great work.
So, I've got my sample code on the screen here.
So I've got my print statement on line one which asks the user what day is it today.
On line two, I'm storing that value in a variable called "day" as an integer.
On line three, I have my selection statement which says, if day <= 3, it's going to print, "It's a weekday," and then it's going to take the variable "remaining" and it's going to set that to 5-day.
Remember "day" is the variable that holds the value of the current day.
I'm then using a print statement with an f-string, which is going to say how many days are remaining until the weekend.
I then have an "elif" statement on line seven which checks if the day is equal to 4, remember that's a Friday, and if this is the case, it's going to print the message, "It's Friday, just a day left the weekend." And then I've got my "else" statement on line nine with the print statement on line 10, which is going to print, "It's the weekend." Okay, we're now moving on to the second part of today's lesson where we're going to store and access items held in a list.
A list is a data structure.
Data structures are organized collections of data.
This means it holds a collection of data, items, or elements, in a sequence one after another.
A list is a dynamic data structure that can grow or shrink as you add or remove items. Each item in the list has a position called an index which starts at zero.
So here's an example.
We're holding the first three days of the week.
So index 0 holds the value Monday, index 1 holds the value Tuesday, and index 2 holds the value Wednesday.
In Python, the syntax for a list is a common separated list of values or items in square brackets.
So I've got my identifier here, which is "days," which is basically the variables storing the list, and then I've got equals, open the square brackets, and then I have the list items separated by a comma followed by closing the square brackets at the end.
So you can see the list items and the fact that there's a comma separating each one.
You'll notice in this example, my list spans over multiple lines of code.
This is just done to stop having a really long line of code that may be difficult to read or you need to scroll across.
So you can separate a list over multiple lines.
In this example, the list items are string literals, which are pieces of text, so they need to be in quotation marks.
However, I could have a list which stores integers and then the speech marks would not be required.
When the program is run, this is what the list will look like when stored in memory.
Sam's got a really good point.
"The first item in the list is at position 0, not 1." You may think it would make sense for Monday, which is the first item in the list, to be in position 1.
But in lists, index start at 0.
So actually, Monday is in index position 0.
Time to check your understanding.
In the program, what day is held in index position 1.
Is it A, Monday, B, Tuesday, or C, Wednesday? Pause the video whilst you have a think.
That's correct.
The answer is B, Tuesday.
Because remember, list index start at 0.
To access an item in a list, you need to specify the index position that holds that item.
So here, on lines one to four, I have my list which holds the days of the week.
On line five, I have a print statement to print out the value that is held in list position 1.
Andeep says, "So this will print Monday." Is Andeep correct? Maybe pause the video and have a think.
"No." Sam's right.
"Remember, lists use zero-based indexing, so this will actually print Tuesday." Well done, Sam.
Here's another example.
This time, my print statement on line seven says, print(days[7]).
Andeep says, "What would this print?" Do you have any ideas? Maybe pause the video again and have a think.
This is a bit of a tricky one, but Sam's correct.
"This will actually return an IndexError, and that's because there is not an item held in position days 7 of the list." The last item held in this list is in position 6.
Expressions can also be used to access list items as long as they evaluate to an integer.
Have a look at the updated pieces of code.
What day of the week will be printed? Maybe pause the video whilst you look at the code and have a think.
That's correct.
Wednesday will be printed.
That's because day was initially set to 3, which would have been Thursday, but in the print statement, we're saying "days," print day minus one.
So we're taking one away from three, which would be two, which holds the value Wednesday.
Time to check your understanding.
What code could you add to line five of this program if you wanted to print the text, "Friday"? Is it A, B, or C? Pause the video here whilst you have a think.
That's right.
The correct line of code was C, "print(days[4])." Remember about our indexing values and the fact that a list starts at index position 0.
What would print(days[7]) display? A, Sunday, B, IndexError, or C, print(days[7])? Pause the video here whilst you have a think.
That's correct.
B, an IndexError.
That's because at the moment, this list does not hold an item in index position 7.
Now time for you to have a go.
Open the starter program, oak.
link/seasons.
Assign a list to months on line one and populate the list with the 12 months of the year.
This list may go over multiple lines.
Then, complete lines seven, eight, and nine with an integer so that the program displays the names of the summer months.
The expected output is on the screen for you so you can check that your program works correctly.
Pause the video here whilst you complete the activity.
Now open the starter program, oak.
link/months.
Complete line eight with an expression so that the program displays the name of the current month.
Pause the video whilst you complete the activity.
How did you get on? Did you manage to create a list and then display items from it? Great work.
Let's have a look at some code together.
So, on lines one to five, I've populated the list with each month of the year.
Remember, as these are string literals, they need to be surrounded by speech marks, and each list item should be separated by a comma.
On lines seven, eight, and nine, I had to add an integer to print out the summer months.
So I've added five, six, and seven, because that holds the summer months, which are June, July, and August.
For the next part, you were asked to add a selection statement to print out the current month.
So, here, we've used [month-1], and that's used because the user is entering the month number starting from 1, whereas the list index starts at 0.
You're doing a fantastic job so far, so well done.
We're now moving on to the final part of today's lesson, where we're going to use selection to display the items from the list.
You've seen how to use selection using "if" statements.
You've also seen how you can store and access items in the list, and we're now going to combine these two skills.
How many seasons are there in a year? Sam's correct.
"There are four seasons in the year, winter, spring, summer, and autumn." Time to check your understanding.
A program uses selection to print a different message for each season.
How many "elif" statements are needed? Is it A, one, B, two, or C3, three? Pause the video whilst you have a think.
That's right.
The correct answer is B, two.
Two "elif" statements are needed because there are four seasons, and the selection statement will also include an "if" and an "else" statement.
A program asks the user to enter the number of the month from one to 12, with one being January and so on.
The winter months are December, January, and February.
Which selection statement would correctly check if the user has entered a winter month? Is it A, B, or C? Pause the video whilst you have a think.
That's right.
The correct answer is C, if month is less than or equal to 2, or month is equal to 12.
The winter months are not numbered consecutively, so you have to check months 1, 2, and then 12.
We're moving on to our last set of tasks for today's lesson, and you're doing a great job so far, so well done.
I'd like you to open the starter program oak.
link/season-selection.
Complete the program so that the program checks the value of the month variable and assigns an appropriate value to the season.
The program should then print, "It is" followed by the name of the season, which will be either winter, spring, summer, or autumn.
Pause the video whilst you complete the activity.
For the last part of Task C, instead of asking the user to type the current month, the program can retrieve the current month using the datetime module that's available in Python.
Replace the lines that request and receive user input, marked with a red minus below, with the lines that retrieve the current month, marked with a green plus below.
Make the alterations to your program and pause the video now.
How did you get on? Did you manage to combine some selection statements with a list? Great work.
Let's have a look at some code together.
So, on line one and two I have my seasons, winter, spring, summer, and autumn held in a list.
On line three, I have a print statement to ask the user what month it is.
On line four, I store that value as an integer in the variable month.
On line five, I start my selection statement.
So, if month is less than or equal to 2, or month is equal to 12, then the season is going to be winter, which is 0 in the list.
"Elif" month is less than or equal to 5, then it's going to be spring.
"Elif" month less than or equal to 8, it's going to be summer which is held in the list index 2.
And then lastly, "else" the season is 3, which is autumn 'cause that's the only one left.
And then lastly, on line 13, I've got my print statement which combines the text "It is" with the value held by season.
To extend the program, remember you were asked to replace the user input with the datetime module.
So you can see here, lines three and four have been replaced with the code provided.
Remember, if you need to make any corrections to your programs, you can pause the video now and do that.
We've come to the end of today's lesson, and you've done a great job, so well done.
Let's summarize what we have learned together.
Selection is used when there is more than one possible path for a program to follow.
Lists are data structures that can be used to store multiple related items in Python.
Values held in a list are ordered by index number, with the first item in a list being stored at index position 0.
List items can be directly accessed by specifying the index value.
I really hope you enjoyed today's lesson, and I hope to see you again soon.
Bye.