Python Programming Lecture 3 Sequence Operations
Python Programming Lecture 3 Sequence Operations
Sequences Iterables
▪containers with items ▪objects on which you can
loop
that are accessible by ▪include all containers
indexing or slicing (sequences or not) and
▪include strings, lists, many non-container
and tuples objects (e.g. files and
generators)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Sequence Operations
▪len()
▪returns the number of items in the container
list1 = [1,2,3,4,5]
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Sequence Operations
▪min() and max()
▪ take one argument, a nonempty iterable whose items are
comparable, and return the smallest and largest items, respectively
print(min([5,3,2,1]))
print(max([5,3,2,1]))
▪for strings, used to get the min and max alphabetical character,
respectively print(min("zebra"))
print(max("zebra"))
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Sequence Operations
▪min() and max()
▪ for list of strings, return the first and last alphabetically,
respectively
print(min(["one", "two", "three"]))
print(max(["one", "two", "three"]))
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Sequence Operations
▪sum()
▪takes one argument, an iterable whose items are numbers, and
returns the sum of the numbers
print(sum([5,3,2,1]))
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Sequence Operations
▪index()
▪searches an element in the sequence and returns the
index of the first occurrence
"Hahaha".index("a")
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Sequence Operations
▪count()
▪counts the number of times an element has occurred in
the sequence
"Hahaha".count("a")
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Sequence Operations
▪slicing a sequence
list1 = [1,2,3,4,5]
▪obtains a portion of a print(list1[1:4:2])
sequence from index start, print(list1[1:4])
up to but not including index print(list1[1:])
stop print(list1[:4])
print(list1[:])
▪variable[start:stop:step] print(list1[::])
print(list1[::-1])
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Sequence Operations
▪repeating a sequence
▪can be done using the multiplication operator (*)
list1 = [1,2,3,4,5]
print(list1*2)
str1 = 'ha'
print(str1*3)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
List Methods
Method Description
Nonmutating methods
L.count(x) Returns the number of items of L that are equal to x.
L.index(x) Returns the index of the first occurrence of an item
in L that is equal to x, or raises an exception if L has no
such item.
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
List Methods
Method Description
Mutating methods
L.append(x) Appends item x to the end of L ; e.g., L[len(L):]=[x].
L.extend(s) Appends all the items of iterable s to the end of L ;
e.g., L[len(L):]=s.
L.insert(i, x) Inserts item x in L before the item at index i, moving following items
of L (if any) “rightward” to make space (increases len(L) by one,
does not replace any item, does not raise exceptions: acts just
like L[i:i]=[x]).
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
List Methods
Method Description
Mutating methods
L.pop([i]) Returns the value of the item at index i and removes it
from L; if i is omitted, removes and returns the last item;
raises an exception if L is empty or i is an invalid index in L.
L.reverse( ) Reverses, in place, the items of L.
L.remove(x) Removes from L the first occurrence of an item in L that
is equal to x, or raises an exception if L has no such item.
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Exercises
Write a Python program to print a
specified list after removing all items
with odd indices.
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Write a program to remove the
duplicates from a list
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova