📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!

  • google app store apple app store
  • âś–

      Question

      You have a Python list of fruits and want to extract a

      specific sub-list using slicing.  Complete the missing part of the code to obtain the sub-list `['banana', 'cherry']`. fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'] # Extract the sub-list ['banana', 'cherry'] selected_fruits = fruits[___] print(selected_fruits) Which of the following options correctly completes the code to produce `['banana', 'cherry']`?
      A `1:3` Correct Answer Incorrect Answer
      B `1:2` Correct Answer Incorrect Answer
      C `2:4` Correct Answer Incorrect Answer
      D `0:2` Correct Answer Incorrect Answer

      Solution

      In Python slicing, `[start:end]` extracts elements from the `start` index up to, but *not including*, the `end` index. Apple is at index 0. Banana is at index 1. Cherry is at index 2. date'` is at index 3. To get `'banana'` (index 1) and `'cherry'` (index 2), you need to start at index 1 and go up to (but not include) index 3. Therefore, `fruits[1:3]` is the correct slice.

      Practice Next
      ask-question