0
Question about negative value
I understand about standard question like this: sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] if I print sqs[2:8:2] (meaning: start:end:step) result : [4, 16, 36] but the problem is negative value sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(sqs[7:5:-1]) result: [49, 36] I don't understand this result, so I try like this print(sqs[7:5] and the result is [] (I guess end index is smaller than start) Could you explain the negative value of step part?
1 Respuesta
+ 6
The step is negative so you go backwards.
sqs[7:5:-1] starts at 49, steps back 1, until it reaches 25
So output is [49,36]
sqs[7:1:-2]
Output: [49,25,9] because you move back to steps each time.