Lec 09
Lec 09
Computer Science
Lecture 9
Venkata Koppula
Department of CSE, IIT Delhi
So far in the course
Tuples :
Enclosed in parenthesis ( )
Each element separated by comma ,
t = () # Empty tuple
tuples_ nd_output.py
t = ("Hello", 5, "Students")
Output :
tuples_ nd_output.py
t = ("Hello", 5, “Students")
print(t[0]*5)
print(t[1]*5)
print(t[3])
Output :
H
HelloHelloHelloHelloHello
25
IndexError
fi
Tuples : just like strings
tuples_ nd_output.py
t = ("Hello", 5, "Students")
u = ("2403", "COL100")
print(t + u)
print(t*2 + u)
print(t[0:1] + u + t[2:3])
print(t[0] + u + t[2])
Output :
tuples_ nd_output.py
t = ("Hello", 5, "Students")
u = ("2403", "COL100")
w = t + u
print(len(w))
Output :
5
Hello
5
Students
2403
COL100
fi
Tuples : just like strings but more …
t = () # Empty tuple
v = ((“Hello”), “Hello”, 5)
# Tuple with three objects : two strings and one integer
Tuples : just like strings but more …
tuples_ nd_output.py
t = ("Hello", 5, "Students")
u = ("2403", "COL100")
v = (t, u)
print(len(v))
print(v[1][1])
print(v[0][0][0])
Output :
3
COL100
H
fi
Tuples : just like strings but more …
tuples_ nd_output.py
t = ("Hello", 5, "Students")
u = ("2403", "COL100")
v = (t, u)
w = v + u
print(len(w))
print(w)
Output :
4
(('Hello', 5, 'Students'), ('2403', 'COL100'), '2403', 'COL100')
fi
Tuples can be used to return multiple values
Function that takes as input a tuple of integers,
outputs the minimum and maximum
min_max.py
def min_max(t):
if(len(t) == 0):
return None
t = “Hello”
t[0] = “h” # will give an error
u = 2
v = 3
z = u + v
Memory: 2
u
Mutables vs Non-Mutables
u = 2
v = 3
z = u + v
Memory: 2 3
u v
Mutables vs Non-Mutables
u = 2
v = 3
z = u + v
Memory: 2 3 5
u v z
Mutables vs Non-Mutables
u = 2
v = 3
z = u + v Output
print(id(u)) 4388563576
4388563608
print(id(v)) 4388563672
print(id(z))
Mutables vs Non-Mutables
Consider the following code, what do you expect the output to be?
u = 2 Output
print(id(u))
4388563576
u = 3 4388563608
print(id(u))
Memory: 2
u
Mutables vs Non-Mutables
Consider the following code, what do you expect the output to be?
u = 2 Output
print(id(u))
4388563576
u = 3 4388563608
print(id(u))
Memory: 2 3
Consider the following code, what do you expect the output to be?
u = 1
v = 2
u = 3
Memory: 1 2
u v
Consider the following code, what do you expect the output to be?
Memory: 3 1 2
u v
Lists :
Enclosed in square brackets [ ]
Each element separated by comma ,
t = [] # Empty list
Lists :
Enclosed in square brackets [ ]
Each element separated by comma ,
Lists are mutable
t = [ “Hello” ]
print(id(t))
Lists :
Enclosed in square brackets [ ]
Each element separated by comma ,
Lists are mutable
t = [ “Hello” ]
print(id(t))
Lists :
Can use + and * just like tuples and strings
t = [ “Hello” ]
print(id(t))
t = ["hello"]
print("id of t : ", id(t))
t.append(5)
print("t : ", t)
Output
id of t : 4332533696
['hello', 5]
id of t after append: 4332533696
Adding elements to lists
Lists :
can also add a list M to list L
using L.extend( M )
t = ["hello"]
print("id of t : ", id(t))
u = ["world"]
print("id of u : ", id(u))
t.extend(u)
print("t : ", t)
print("id of t after extend: ", id(t))
Output
id of t : 4332533696
['hello', 5]
id of t after append: 4332533696
Adding elements to lists
Lists :
can also add a list M to list L
using L.extend( M )
t = ["hello"]
print("id of t : ", id(t))
Output
t.append(5)
print("t after append: ", t)
id of t : 4377065408
t after append: ['hello', 5]
print("id of t after append: ", id(t))
id of t after append: 4377065408
id of u : 4376906304
u = ["world"]
t after extend: ['hello', 5, 'world']
print("id of u : ", id(u))
u = ['world']
id of u after extend : 4376906304
t.extend(u)
id of t after extend : 4377065408
print("t after extend: ", t)
print("u = ", u)
print("id of u after extend : ", id(u))
print("id of t after extend : ", id(t))
Adding elements to lists
Lists :
can also insert items at any position in L
using L.insert( index, item )
t = [“hello”, “students”]
print("id of t : ", id(t))
t.insert(1, “COL100”)
print("t after insert: ", t)
Output
id of t : 4377065408
t after insert: ['hello', ‘COL100’, ‘students’]
id of t after append: 4377065408
Removing elements from lists
Removing elements from lists
Both these methods modify the list, and also return the element removed
id of t : 4377065408
print(t.pop(1)) t after pop: ['hello',‘students’]
print("t after pop: ", t) id of t after pop: 4377065408
Given a list L ,
how to arrange them in increasing/decreasing order?
sorted( L ) : sorts elements in increasing order, does not mutate L (returns a di erent list)
sorted( reverse = True) : sorts elements in increasing order, does not mutate L
ff
Summary of list operations
L.append ( e ) : mutates L, appends e to L, returns None
L.extend( M ) : mutates L, appends list M to L, returns None
L.insert( i, e ) : mutates L, inserts e at position i, returns None
sorted( L ) : sorts elements in increasing order, does not mutate L (returns a di erent list)
sorted( reverse = True) : sorts elements in increasing order, does not mutate L
ff
Computations on lists
Searching in sorted lists
left = 0
right = len(L) - 1
if(L[mid] == e):
return True
elif (L[mid] < e):
left = mid + 1
else:
right = mid - 1
return False