Mastering Lists in Python

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

Cette leçon contient 22 diapositives, avec quiz interactifs et diapositives de texte.

Éléments de cette leçon

Mastering Lists in Python
Unit 4 Programming
Said Ahghari

Slide 1 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'instructions

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

Slide 3 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'instructions

What do you already know about using lists in Python?

Slide 6 - Carte mentale

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'instructions

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

Slide 9 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'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 - Diapositive

Cet élément n'a pas d'instructions

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

Slide 19 - Question ouverte

Cet élément n'a pas d'instructions

Write down 3 things you learned in this lesson.

Slide 20 - Question ouverte

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 - Question ouverte

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 - Question ouverte

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.