Mastering Lists in Python

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

In deze les zitten 22 slides, met interactieve quizzen en tekstslides.

Onderdelen in deze les

Mastering Lists in Python
Unit 4 Programming
Said Ahghari

Slide 1 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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

Slide 3 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

What do you already know about using lists in Python?

Slide 6 - Woordweb

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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

Slide 9 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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 - Tekstslide

Deze slide heeft geen instructies

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

Slide 19 - Open vraag

Deze slide heeft geen instructies

Write down 3 things you learned in this lesson.

Slide 20 - Open vraag

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 vraag

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 vraag

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.