๐ What does index() do?
list.index(value, start)
It returns the index of the first occurrence of value,
starting the search from position start.
Step-by-step execution:
-
arr = [10, 20, 30]
Index positions: arr.index(20, 1) means:
๐ Find the value 20, but start searching from index 1.-
At index 1, the value is 20 → match found.
✅ Output:
1⚠️ Tricky Case (important for quizzes)
arr.index(10, 1)This will raise:
ValueError: 10 is not in listBecause it starts searching from index 1, and 10 is only at index 0.
Mental Rule (easy to remember):
index(x, i)= "Find x after index i-1"


0 Comments:
Post a Comment