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])
explain what will be the output and how that output came??????
Answers
Answered by
14
Answer:
The output will be
1
3
5
7
9
Explanation:
the for loop goes through every element in myList 1,2,3 till 9. It goes till 9 but not 10 because range does not include the last number. It goes till there but never includes it that's the reason why it stops at 9. Now, the if i%2 == 0 checks if the number is even. if it is, then it returns the index of the element. For example, 6 the number six is the 7th element in the list (because list index starts from 0) so 1 is element no.0, 2 is no.3 and so on. Now, since 6 is an even number and it's index is 7 we see the 7 in the output print(myList[i]) returns the index.
Hope you got the logic!
Similar questions