Lists in Python

Lists in Python
Cited from W3Schools.com
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

Lists in Python
Cited from W3Schools.com
Said Ahghari

Slide 1 - Tekstslide

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

Slide 2 - Tekstslide

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

Lists
In python one of the most used data structures is a list.
Lists are used to store similar 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 4 - Tekstslide

List Data Type
Lists are a collection of items that can be manipulated and accessed individually. The list data type 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 5 - Tekstslide

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

Slide 6 - Tekstslide

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

Slide 7 - Tekstslide

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

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

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

Slide 9 - Tekstslide

Slide 8: Recap and Summary
In this lesson, we learned about various data types in Python, including numeric, string, boolean, list, tuple, and dictionary data types.

Slide 10 - Tekstslide

How to access individual items in a list?
How to access individual items in a list?
To access the individual items of a list, use the index number.

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

Which item is displayed?

Slide 11 - Tekstslide

Negative Indexing

Negative indexing means start from the end.
-1 refers to the last item, -2 refers to the second last item etc.

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

Which item is displayed?

Slide 12 - Tekstslide

Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the range.

When specifying a range, the return value will not include the the ending range.
myStudents = ["Said", "John", "Iman", "Nour", "Jane"]
print (myStudents [1:4] )

Which items are displayed?
What is the output type?

Slide 13 - Tekstslide

myStudents = ["Said", "John", "Iman", "Nour", "Jane"]
print (myStudents [1:4] )
Which items are displayed?
What is the output type?

Slide 14 - Woordweb

Answers:
['John', 'Iman', 'Nour']

It is a list because of the square brackets ([ ])

Slide 15 - Tekstslide

Slide 16 - Tekstslide

How many items will be outputted using the following code?
fruitList = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruitList[ :4])
A
Three items
B
Four items
C
None
D
Five items

Slide 17 - Quizvraag

Explain the following line of code.
print(fruitList[:4])

Slide 18 - Open vraag

What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
print(mylist[-1])
A
Apple
B
Banana
C
Cherry
D
Error message

Slide 19 - Quizvraag

What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry', 'kiwi', 'melon', 'mango']
print(mylist[-2:])
A
['kiwi', 'melon', 'mango']
B
['melon', 'mango']
C
error message
D
['apple', 'banana']

Slide 20 - Quizvraag

What is the output of the code below:
my_list = [1, 2, 3, 4, 5]
print(my_list[::-1])

Slide 21 - Woordweb

What is the output of the code below:
my_list = [1, 2, 3, 4, 5]
print(my_list[::-1])

Slide 22 - Open vraag