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])):
1 Réponse
+ 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.