What will be the output of code written below & why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What will be the output of code written below & why?

What will be the output of below code? a=[x for x in range(4)] print(sum(a[1::-2]+a[:3])):

25th Apr 2017, 10:27 AM
Prakhar Jain
Prakhar Jain - avatar
1 Answer
+ 1
The first line is just a list comprehension used to turn the range object into a list object. a[1::-2] is [1], as we start at index 1 and go back 2 steps until we are out of bounds, which happens on the first step we take. a[:3] is just [0,1,2], hence the sum over all the elements in the lists is 4.
25th Apr 2017, 1:11 PM
Tob
Tob - avatar