+ 4

What is the Output of this Code?

sqs = [0,1,4,9,16,25,36,49,64,81] print(sqs[5:7:-1]) The answer is [49,36] and its backwards which makes sense with the "-1". But the positions? In each way i am getting 3 elements in the Output..

27th Nov 2016, 10:42 AM
smallybells
smallybells - avatar
6 Respuestas
+ 4
when it goes backward, the start is the bigger index. sqs[7:5:-1] the program first goes to the start, it is 7, counts backward, it is -1, and goes to the stop, it is 5. syntax for slicing is: list[start:stop:step] i hope it helps
27th Nov 2016, 12:48 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
+ 3
thanks..
27th Dec 2016, 12:36 PM
Dilan Perera
Dilan Perera - avatar
+ 1
Yes but with this Syntax I still get 3 List entries..
1st Dec 2016, 2:22 PM
smallybells
smallybells - avatar
0
As explained, the last value is not included in list slicing
21st Jan 2017, 10:41 AM
Ciciboy 1
Ciciboy 1 - avatar
0
עז3רקדסג =
5th Feb 2019, 11:02 AM
vered shtrauss
0
תודה רבה
5th Feb 2019, 11:03 AM
vered shtrauss