+ 1

lists slicing

sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(sqs[7:5:-1]) please tell me how this works

14th Mar 2017, 9:41 AM
shivam
shivam - avatar
2 odpowiedzi
+ 7
The formula says: return the list's elements from 7 to (but not including) 5, stepping backwards by 1. Element index 7 is 49, next (backwards, so index 6) is 36 and that's it.
14th Mar 2017, 8:43 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
The code will output [49, 36] this is because the -1 says that the list should be read through backwards
14th Mar 2017, 1:48 PM
Marcus Søndergaard
Marcus Søndergaard - avatar