ICT582 Topic 05
ICT582 Topic 05
§ functions
§ decomposition – create structure
§ abstraction – suppress details
§ from now on will be using functions a lot
6.0001 LECTURE 5 2
TODAY
6.0001 LECTURE 5 3
TUPLES AND LISTS
§ Tuple and list are two frequently used data structures in
Python.
§ They are part of a more general data structure known as
"collection".
§ Tuple and list are language built-in data structures.
§ A tuple is an ordered sequence of elements.
Ø tuples are defined using parentheses, eg, (1, "two", 3)
§ A list is also an ordered sequence of elements.
Ø tuples are defined using square brackets, eg, [1, "two", 3]
§ Both are non-scalar objects
6.0001 LECTURE 5 4
TUPLES AND LISTS
§ Elements in tuples and lists are accessible using indexes
§ Each has its own set of built-in methods
§ Some of the functions and notations apply to both, such as len
function and syntax for indexing
§ Elements in tuples and lists are heterogenous, these elements
do not have to have the same type
§ The main difference between the two:
Ø a tuple is immutable – once created, you cannot change it (similar
to string)
Ø while a list is mutable
6.0001 LECTURE 5 5
TUPLES
§ We can also create a new tuple out of an existing tuple using index
syntax. Note: no change to the existing tuple
§ The syntax is similar to the syntax used to get a new string out of
an existing string.
t = (1, 2, "mit", 4, "True", 6.6, "7", 8, 9, "ten")
t[1:2] -> evaluate to a new tuple(2,). Note the extra
comma at the end
t[1:5] -> evaluate to a new tuple(2,"mit",4,"True")
t[1:5:2] -> evaluate to a new tuple (2,4)
t[1::2] -> evaluate to a new tuple (2,4,6.6,8,"ten")
t[-1::2] -> evaluate to a new tuple ("ten",)
t[-1::-2] -> evaluate 6.0001
to LECTURE
a new5 tuple ("ten",8,6.6,4,2)
EXAMPLE: SWAP TWO VARUES
6.0001 LECTURE 5 10
LISTS
6.0001 LECTURE 5 11
INDICES AND SLICING
a_list = []
L = [2, 'a', 4, True]
L1 = [1] -> L1 is a list with one element,
note: no comma at the end
len(L) -> evaluate to 4
L[0] -> evaluate to 2
L[2]+1 -> evaluate to 5
L[3] -> evaluate to True
L[1:3] -> evaluate to a new list ['a', 4]
L[-1:1:-1] -> evaluate to a new list [True, 4]
L[4] -> gives an error
i=2 6.0001 LECTURE 5 12
6.0001 LECTURE 5 13
CHANGING ELEMENTS
[2,5,3]
14
L
ITERATING OVER A LIST
print(total) print(total)
Notice:
• list elements are indexed from 0 to len(L)-1
• range(n)goes from 0 to n-1
15
OPERATION ON LISTS –
append method
§ add elements to end of list with L.append(element)
§ mutates the list!
L = [2,1,3]
L.append(5) -> L is now [2,1,3,5]
L1 = [2,1,3]
L2 = [4,5,6]
L3 = L1 + L2 -> L3 is [2,1,3,4,5,6]
L1, L2 unchanged
§ sorted function
§ sort method
§ reverse method
§ and many more!
https://docs.python.org/3/tutorial/datastructures.html
L=[9,6,0,3]
sorted(L) ̶˃ returns sorted list, does not mutate L
L.sort() ̶˃ mutates L=[0,3,6,9]
L.reverse() ̶˃ mutates L=[9,6,3,0]
6.0001 LECTURE 5 20
MUTATION, ALIASING, CLONING
6.0001 LECTURE 5 21
LISTS IN MEMORY
6.0001 LECTURE 5 22
ALIASES
§ If variable contains a scalar object, the value is stored in the
variable.
a = 1 # here a contains scalar object 1
b = a # copy the value 1 stored in a to variable b
§ AHer the assignment, b iniIally contains the same value as a,
but the two variables are independent of each other. Change
the value stored in b would not affect the value stored in a.
§ However, if variable is a non-scalar object, such as the following
variable warm:
warm = [1,2,3]
§ what is stored in the variable is the start address of the object
[1,2,3]. The object [1,2,3] is actually stored somewhere
else, not in variable warm. 23
ALIASES
§ When assigning such an non-scalar variable to another variable hot,
hot = warm
§ what is copied to variable hot is not object [1,2,3], rather, it is the
start address of object [1,2,3].
Ø This means both warm and hot contain the same address, which point to the same
object [1,2,3]. We call hot an alias of warm, because they point to the same
object.
Ø change the object pointed to by hot also changing the object pointed by warm. This is
called "side effect".
r
s cala
a
a is bject
o
-
a non
s t
mi jec
war lar ob
sca
6.0001 LECTURE 5 24
CLONING A LIST
6.0001 LECTURE 5 25
SORTING LISTS
6.0001 LECTURE 5 26
LISTS OF LISTS OF LISTS OF….
6.0001 LECTURE 5 27
MUTATION AND ITERATION
L1 = [1, 2, 3, 4]
L2 = [1, 2, 5, 6]
remove_dups(L1, L2)
• Python Tutor
• Programiz
• MIT OCW