Exercises · Q3
Q.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])
Odisha ChseTextbookSubjective· 2mImportance★★★★★est
37% · 11/30 Questions
✓ Free question
The loop prints the elements at even indexes 0, 2, 4, 6, 8. Because the list is [1..10], the element at index i is i+1, so the even positions hold the odd values: 1 3 5 7 9.
The code:
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])
Step-by-step trace — i runs 0…9; the condition tests the index, and only even indexes pass:
| i | i % 2 == 0? | myList[i] | printed? |
|---|---|---|---|
| 0 | yes | 1 | 1 |
| 1 | no | 2 | — |
| 2 | yes | 3 | 3 |
| 3 | no | 4 | — |
| 4 | yes | 5 | 5 |
| 5 | no | 6 | — |
| 6 | yes | 7 | 7 |
| 7 | no | 8 | — |
| 8 | yes | 9 | 9 |
| 9 | no | 10 | — |
Output:
1
3
5
7
9
Why it comes out this way
range(0, len(myList))generates the indexes 0 to 9, not the elements — soiis a position, andmyList[i]fetches the element at that position.if i % 2 == 0:filters on the index, not the value. That is the whole trick of this question: it looks like it might print even numbers, but it prints the elements sitting at even positions.- Here
myList[i] == i + 1(the list is just 1…10 in order), so even indexes 0,2,4,6,8 hold the odd values 1,3,5,7,9 — each printed on its own line, because everyprint()call ends with a newline.
✓Final answer
The output is 1 3 5 7 9, one per line — the condition i % 2 == 0 selects the elements at even indexes (0, 2, 4, 6, 8), and in this list those positions contain the odd values.
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.