How many elements will the list2 list contain after execution of the following snippet?
How many elements will the list2 list contain after execution of the following snippet?
C
Negative step changes a way, slice notation works. It makes the slice be built from the tail of the list. So, it goes from the last element to the first element. So [-1:1:-1] will start from last element of the list and will end end at 2nd element of list, thus as 0th and 1st are sliced we will be left with 7 elements
-1:1:-1 first => start of string (-1 is last index) second => end of string (1 is second index from start) last -1 => step (negative for reverse)
Answer is C list1 = [False for i in range(1, 10)] print(list1) list2 = list1[-1:1:-1] print(list2) [False, False, False, False, False, False, False, False, False] [False, False, False, False, False, False, False]
C. seven
what it is means list1[-1:1:-1] ???
range[start:stop(excluding):step]
YOu maybe understand better with this example: >>> list1=[i for i in range(1,10)] >>> list1 [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list2=list1[-1:1:-1] >>> list2 [9, 8, 7, 6, 5, 4, 3]