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`` and ``list1 = 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. 1. What is the output of the following code fragmemt? .. code-block:: python lst = ['a', 'b', 'c', 'd', 'e'] print(lst[0]) print(lst[1]) print(lst[-1]) print(lst[:4]) 2. Write an expression that will extract the substring ``"bcd"`` from ``s``: .. code-block:: python s = 'abcde' 3. What will happen if I try to run the following code: .. code-block:: python lst = [10, 20, 30] lst[3] 4. What is the output of the following code fragment? .. code-block:: python 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) 5. What is the output of the following code fragment? .. code-block:: python z = [0]*9 z[2] = 98 z[3] = 1 z[8] = 4 x = z[3] z[x] = 3 z[z[8]] = 27 print(z) 6. What is the output of the following code fragment? .. code-block:: python lst = [] for i in range(3,10): lst.append(i // 3) print(lst)