CBSE Class 11 Computer Science
Question 61 of 104
List Manipulation — Question 14
Back to all questions 14
Question Question 14
An index out of bounds given with a list name causes error, but not with list slices. Why?
Answer
When we use an index, we are accessing a constituent element of the list. If the index is out of bounds there is no element to return from the given index hence Python throws list index out of range error whereas list slicing always returns a subsequence and empty subsequence is a valid sequence. Thus, when a list is sliced outside the bounds, it still can return empty subsequence and hence Python gives no errors and returns empty subsequence.