Informatics Practices
Suppose L = [10, ["few", "facts", "fun"], 3, 'Good']
Consider the above list and answer the following:
(a) L[3:]
(b) L[::2]
(c) L[1:2]
(d) L[1] [1]
Answer
(a) L[3:] — ['Good']
Explanation
The slice L[3:] will include the element at index 3 and any elements that follow it. Since index 3 is the last element, L[3:] will include only ['Good'].
(b) L[::2] — [10, 3]
Explanation
The slice L[::2] means to include every second element from the list, starting from the beginning. This results in selecting the elements at indices 0 and 2, which are 10 and 3.
(c) L[1:2] — [['few', 'facts', 'fun']]
Explanation
The slice L[1:2] selects elements from index 1 up to, but not including, index 2. This means it includes only the element at index 1, which is ["few", "facts", "fun"].
(d) L[1] [1] — facts
Explanation
The expression L[1][1] first accesses the sub-list at index 1, which is ["few", "facts", "fun"], and then accesses the element at index 1 of that sub-list, which is "facts". Therefore, L[1][1] evaluates to "facts".
Related Questions
Define the following functions:
(a) len()
(b) append()
(c) extend()
(d) pop()
(e) remove()
(f) del
(g) sort()
(h) sorted()
(i) copy()
(j) count()
(k) max()
(I) min()
(m) sum()
What is the difference between insert() and append() methods of a list?
Find the output of the following:
L1=[1, 2, 3] L2=[4, 5, 6] print(L1+list ("45") ) print(L1.pop()) L1.remove (2) print(L1) L1.extend (L2) print(L2)What will be the output of the following statement?
list1 = [12, 32, 65, 26, 80, 10] list1.sort() print(list1)