Lecture 4 - Python Sequences
Lecture 4 - Python Sequences
Python Sequences
List Introduction
sazzad@diucse
List Operations Creating List
List can be created by placing the list elements inside square brackets
separated by comma. Anything inside square bracket is considered as list
elements. They can be number, string, object and another list.
lst=[1,3,4.5,'python']
lst=list()
sazzad@diucse
List Operations Iterating List
List can be iterated using index number. It also allow iteration
using negative index. Let’s consider a list named c.
normal iteration c[0] c[1] c[2] c[3] c[4] c[5]
c[-6] c[-5] c[-4] c[-3] c[-2] c[-1] iteration using negative index
sazzad@diucse
List Operations Inserting into List
New elements can be inserted into list. This can be done using
append( ) method. This method incerts the new value at the end of
the list. c.append(19)
c[0] c[1] c[2] c[3] c[4] c[5] c[6]
sazzad@diucse
List Operations List are mutable
sazzad@diucse
Tuple Introduction
● Tuple is a sequence. Like as list, tuple is also known as
collection.
● It stores values of same data type or different data types.
● Tuple is iterable. Each index can be iterated using index. It
also allow iteration using negative index.
● Tuple is immutable. Elements of a tuple can not be
changed.
sazzad@diucse
Tuple Operations Creating Tuple
tp=tuple()
sazzad@diucse
Tuple Operations Iterating Tuple
t[-6] t[-5] t[-4] t[-3] t[-2] t[-1] iteration using negative index
sazzad@diucse
Tuple Operations Inserting into Tuple
New elements can be inserted into tuple. This can be done using
+= operat. This incerts the new value at the end of the tuple.
sazzad@diucse
Unpacking Sequence
sazzad@diucse
Sequence Slicing
sazzad@diucse
Sequence Slicing
1. Slicing with starting index: It slices from starting index to the last index.
2. Slicing with ending index: It slices from zero index to the previous of ending index.
sazzad@diucse
Sorting List
Sorting a list can be done in two ways. sort( ) method does the sorting but it rearranges the
elements of the list in ascending order. That means in this method, the unordered list will be lost if
not stored separately. To sort in descending order, we can set reverse parameter True.
A list can be sorted using built-in function sorted( ). This function creates a new sorted list. So, we do
not need to store the unordered list separately.
sazzad@diucse
Searching Sequence
We can search for an element inside a list. index( ) method does this work.It returns the index of
first occurrence of search keyword.
We can specify the search area that means starting position and ending position of search inside a
list.
When we did not specify the search range, it starts the search from index 0 and returns the first
occurence of 3 at index 0. But in the second code, when we specify the search range from 2 to 8, it
search from index 2 to index 7 and returns the first occurence of 3 at index 4.
sazzad@diucse
Searching Sequence
We can check if an element exists in a list or not with in and not in operator. In operator
returns True if the element exists, otherwise it returns False.
not in operator returns True if the element does not exist in the list, otherwise it returns False.
sazzad@diucse
Thank You
sazzad@diucse