Exam PCAP All QuestionsBrowse all questions from this exam
Question 18

How many elements will the list2 list contain after execution of the following snippet?

    Correct Answer: C

    C

Discussion
AvidulamOption: 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

shilpadkOption: C

-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)

naveenbv80Option: C

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]

macxszOption: C

C. seven

chr000

what it is means list1[-1:1:-1] ???

kauser

range[start:stop(excluding):step]

Efren

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]