0% found this document useful (0 votes)
9 views

Python - Lesson 5 Python List_new

Uploaded by

David Lei
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python - Lesson 5 Python List_new

Uploaded by

David Lei
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Python – Lesson 5

List
Form 4
PYTHON LIST AND TUPLES
The most basic data structure in Python is the sequence.
Each element of a sequence is assigned a number - its
position or index. The first index is zero, the second index
is one, and so forth.

Python has six built-in types of sequences, but the most


common ones are lists and tuples.
PYTHON LIST
PYTHON LIST
The following are the properties of a list.
Mutable: The elements of the list can be modified. We can add or remove
items to the list after it has been created.

Ordered: The items in the lists are ordered. Each item has a unique index
value. The new items will be added to the end of the list.

Heterogeneous: The list can contain different kinds of elements i.e; they can
contain elements of string, integer, Boolean, or any type.

Duplicates: The list can contain duplicates i.e., lists can have two items with
the same values.
CREATING A PYTHON LIST
Creating a list is as simple as putting different comma-separated
values between square brackets [ ]. For example:

list1= ['physics', 'chemistry', 1997, 2000]


list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]

Similar to string indices, list indices start at 0, and lists can be


sliced, concatenated and so on.
LENGTH OF A LIST
In order to find the number of items present in a list, we can use
the len( ) function:

list1= ['physics', 'chemistry', 1997, 2000]


Print(len(list1))
ACCESSING ITEMS OF A LIST
The items in a list can be accessed through indexing and
slicing. This section will guide you by accessing the list
using the following two ways

Using indexing, we can access any item from a list using


its index number

Using slicing, we can access a range of items from a list


INDEXING
The list elements can be accessed using the “indexing”
technique. Lists are ordered collections with unique
indexes for each item. We can access the items in the list
using this index number.
NEGATIVE INDEXING
The elements in the list can be accessed from right to left
by using negative indexing. The negative value starts from
-1 to -length of the list. It indicates that the list is indexed
from the reverse/backward.
Accessing Values in Lists
To access values in lists, use the square brackets for slicing along with the
index or indices to obtain the value available at that index. For example:
list1= ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5,6,7 ]
print (list1, list2)
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])
print ("list1[-2]: ", list1[-2])
Updating Lists
You can update single or multiple elements of lists by giving the slice on the
left-hand side of the assignment operator, and you can add to elements in a
list with the append( ) method. For example:
list = ['physics', 'chemistry', 1997, 2000]
print ("Value available at index 2 : ", list[2])
list[2] = 2001
print ("New value available at index 2 : ", list[2])

When the above code is executed, it produces the following result:


Value available at index 2 : 1997
New value available at index 2 : 2001
LIST SLICING
Slicing a list implies, accessing a range of elements in a list.
For example, if we want to get the elements in the position
from 3 to 7, we can use the slicing method. We can even
modify the values in a range by using this slicing
technique. Below is the syntax for list slicing.
LIST SLICING
Slicing a list such as:
• Extract a portion of the list
• Reverse a list
• Slicing with a step
• Slice without specifying start or end position
LIST SLICING - EXAMPLE
Given:
my_list= [5, 8, ’Tom’, 7.50, ‘Emma’]

1. Slice first four items


2. print every second element with a skip count 2
3. Reversing the list
4. Without end value, starting the list from 3rd item to last item
LIST SLICING – EXAMPLE RESULT
ITERATING A LIST
The objects in the list can be iterated one by one, using a For Loop.
my_list= [5, 8, ’Tom’, 7.50, ‘Emma’]
ITERATE ALONG WITH AN INDEX NUMBER
The index value starts from 0 to (length of the list -1).
Hence using the function range() is ideal for this scenario.

The range function returns a sequence of numbers. By


default, it returns starting from 0 to the specified number
(increments by 1). The starting and ending values can be
passed according to our needs.
ITERATE ALONG WITH AN INDEX NUMBER
The objects in the list can be iterated one by one, using a For Loop.
my_list= [5, 8, ’Tom’, 7.50, ‘Emma’]
ADDING ELEMENTS TO THE LIST
We can add a new element/list of elements to the list
using the list methods such as append(), insert(),
and extend()

Append the item at the end of the list


The append() method will accept only one parameter and
add it at the end of the list.

Let’s see the example to add the element ‘Emma’ at the


end of the list.
ADDING ELEMENTS TO THE LIST
ADD ITEM AT THE SPECIFIED POSITION IN THE LIST
Use the insert() method to add the object/item at the
specified position in the list. The insert method accepts
two parameters position and object.

It will insert the object in the specified index. Let us see


this with an example.
ADD ITEM AT THE SPECIFIED POSITION IN THE LIST
LIST COMPREHENSION

List comprehension is an easy to read, compact, and


elegant way of creating a list from any existing iterable
object.
Exercise 5a - Reverse a list in Python
Write your program with two different methods.

Type these two lines


comments at the beginning
of your file.

Submit the file


F4@##Ex5a.py to
teacher.
Exercise 5b - Turn every item of a list into its square
Hint:
•Create an empty result list
•Iterate a numbers list using a loop
•In each iteration, calculate the square of a current number and
add it to the result list using the append() method.

Submit the file


F4@##Ex5b.py to
teacher.
Exercise 5c - Replace list’s item with new value if found
You have given a Python list. Write a program to find value 20 in the list, and if it is present, replace
it with 200. Only update the first occurrence of an item.

Hint:
•Use list method index(20) to get the index number of a 20
•Next, update the item present at the location using the index number

Submit the file


F4@##Ex5c.py to
teacher.

You might also like