Question 3 of 16intermediate🔧 ApplyShort Answer2 marks
What will be the output of the following code segment: myList = [1,2,3,4,5,6,7,8,9,10] for i in range(0,len(myList)): if i%2 == 0: print(myList[i])
Correct Answer
1 3 5 7 9
Exercise: EXERCISE | Q: 3 | (Page 17)
For More Understanding
Explanation
The code iterates through the indices of the list 'myList'. The condition 'i % 2 == 0' checks if the index is even. Since list indices start at 0, the elements at even indices (0, 2, 4, 6, 8) are printed. myList[0] is 1, myList[2] is 3, and so on.
Solution Steps
Step 1: The list 'myList' is initialized as [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
Step 2: The loop iterates 'i' from 0 to 9 (length of list).
Step 3: The condition 'if i % 2 == 0' is True for indices 0, 2, 4, 6, 8.
Step 4: The elements at these indices are printed: myList[0] is 1, myList[2] is 3, myList[4] is 5, myList[6] is 7, myList[8] is 9.