Mastering Lists in Python

Mastering Lists in Python
Unit 4 Programming
Said Ahghari
1 / 22
next
Slide 1: Slide
ComputingLower Secondary (Key Stage 3)

This lesson contains 22 slides, with interactive quizzes and text slides.

Items in this lesson

Mastering Lists in Python
Unit 4 Programming
Said Ahghari

Slide 1 - Slide

This item has no instructions

Learning Objective
By the end of the lesson, you will be able to understand how to declare, access, iterate through, and modify lists in Python.

Slide 2 - Slide

This item has no instructions

Data Types
Python supports various data types such as integers, floats, strings, and Boolean.

Slide 3 - Slide

This item has no instructions

Data Types for Variables
In Python, variables can hold different types of data, such as numbers, strings, boolean, and lists. Each data type has its own characteristics and usage.

Slide 4 - Slide

This item has no instructions

Lists
In python one of the most used data structures is a list.
Lists are used to store data, one item after another.
For example:
  • items you need to buy from a shopping list
  • names of students in a class

A list is an example of a 1 Dimensional array.

Slide 5 - Slide

This item has no instructions

What do you already know about using lists in Python?

Slide 6 - Mind map

This item has no instructions

List Data Type
Lists are a collection of items that can be manipulated and accessed individually. The list in Python is used to store multiple values in a single variable. It is denoted by square brackets ([]).
marks = [25, 65, 95, 33]    #a list of 4 numbers  
names = []                              #an empty list

Slide 7 - Slide

This item has no instructions

Coding Activity: creating and outputting a list
In Python, create a list of 5 students.  Name the list myStudents. Output the list. 
timer
5:00

Slide 8 - Slide

This item has no instructions

Solution
myStudents = ["Said", "John", "Iman", "Nour", "Jane"]
print(myStudents)

Slide 9 - Slide

This item has no instructions

List items' position
Each list item is held in a position (index) in the list.
The list index starts from zero (0).

0
1
2
3
4
Said
John
Iman
Nour
Jane
Position (index)
List item

Slide 10 - Slide

This item has no instructions

Accessing Items in a List
List items have a position that is called index.  Individual items in a list are accessed using their index. Indexing starts at 0, and negative indexing can be used to access items from the end of the list.
students = ["Said", "John", "Ayesha", "Mohammed", "Jane"]
Index
0
1
2
3
4
Items
Said
John
Ayesha
Mohammed
Jane

Slide 11 - Slide

This item has no instructions

Accessing Items in a List

Individual items in a list are accessed using their index. Indexing starts at 0, and negative indexing can be used to access items from the end of the list.
print (students [1]) 
print (students [-1])
What is the output?
Index
0
1
2
3
4
Items
Said
John
Ayesha
Mohammed
Jane

Slide 12 - Slide

This item has no instructions

Finding the number of items in a list
To find the number of items in a list use the len() function.
myStudents = ["Said", "John", "Iman", "Nour", "Jane"]
print(len(myStudents))
Output:
5

Slide 13 - Slide

This item has no instructions

Iterating Through a List
You can iterate through all the items in a list using a for loop. This allows you to perform operations on each item in the list.
students = ["Said", "John", "Ayesha", "Mohammed", "Jane"]
for x in students:
     print (x)

Slide 14 - Slide

This item has no instructions

Adding Items to a List
New items can be added to a list using the append() method. This adds the new item to the end of the list.
students = ["Said", "John", "Ayesha", "Mohammed", "Jane"]
students.append("Ali")
students.append("Hassan")
print (students)

Slide 15 - Slide

This item has no instructions

Replacing Items in a List
You can replace items in a list by directly assigning a new value to the specific index of the item.

students[0] = "Ali"
print (students)

Slide 16 - Slide

This item has no instructions

Removing Items from a List
Items can be removed from a list using the remove() method or by using the del keyword followed by the index of the item to be deleted.

students.remove("Jane")

Slide 17 - Slide

This item has no instructions

Practice and Questions
Now that you've learned about using lists in Python, it's time to practice and ask any questions you may have.

Slide 18 - Slide

This item has no instructions

Write the code to print the last item in the following list:
students = ["Said", "John", "Ayesha", "Mohammed", "Jane"]

Slide 19 - Open question

This item has no instructions

Write down 3 things you learned in this lesson.

Slide 20 - Open question

Have students enter three things they learned in this lesson. With this they can indicate their own learning efficiency of this lesson.
Write down 2 things you want to know more about.

Slide 21 - Open question

Here, students enter two things they would like to know more about. This not only increases involvement, but also gives them more ownership.
Ask 1 question about something you haven't quite understood yet.

Slide 22 - Open question

The students indicate here (in question form) with which part of the material they still have difficulty. For the teacher, this not only provides insight into the extent to which the students understand/master the material, but also a good starting point for the next lesson.