Lecture 4: Lists, Tuples and StringsΒΆ
Based on the lecture and the readings (R&S Lists, Tuples & Strings) you should be able to:
Understand how to index a list, tuple, or string
Understand how to construct a list in a loop
Explain the difference between
list1 = list2
andlist1 = list2[:]
Below, please find some practice problems for this topic. These problems should be completed by hand, that is, without the use of a computer.
What is the output of the following code fragmemt?
lst = ['a', 'b', 'c', 'd', 'e']
print(lst[0])
print(lst[1])
print(lst[-1])
print(lst[:4])
Write an expression that will extract the substring
"bcd"
froms
:
s = 'abcde'
What will happen if I try to run the following code:
lst = [10, 20, 30]
lst[3]
What is the output of the following code fragment?
x = ['abcde', [1, 2, 3, 4], 8, 9, 100, 'hello']
y = x[0] + x[-1]
z = x[0][:2]
x.append(y)
x.append(z)
print(x)
What is the output of the following code fragment?
z = [0]*9
z[2] = 98
z[3] = 1
z[8] = 4
x = z[3]
z[x] = 3
z[z[8]] = 27
print(z)
What is the output of the following code fragment?
lst = []
for i in range(3,10):
lst.append(i // 3)
print(lst)