KnowledgeBoat Logo
|

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]

Python List Manipulation

4 Likes

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".

Answered By

3 Likes


Related Questions