list slices | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

list slices

sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(sqs[7:2:-1]) >>>[49, 36, 25, 16, 9] sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(sqs[7:3:-1]) >>>[49, 36, 25, 16] sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(sqs[6:2:-1]) >>>[36, 25, 16, 9] my question is why it goes to 9 if i choose 2 in 2nd position , what ever the value either 7 or 6 in 1st position

20th Feb 2017, 9:41 AM
Saswat saubhagya Rout
Saswat saubhagya Rout - avatar
4 Réponses
+ 6
you get the entries from index6 to index3 in descending order because index 6 ....which is 36 ... is your beginning index 2 ... which is 4... you stop before the step is -1 ... you go left
20th Feb 2017, 10:04 AM
Oma Falk
Oma Falk - avatar
+ 3
whenever you type a range(a,b,c) a is where to start from, and is included, b is where to stop at, and is excluded. when you ask your program to show the number from the indexes 6 to 2, the 6th index is included (the number 36), but the 2nd index is excluded (the number 4), so it stops at the 3rd index (which is 9)
20th Feb 2017, 9:19 PM
ramzi
ramzi - avatar
+ 1
thank you
22nd Feb 2017, 10:36 AM
kraim Said
kraim Said - avatar
0
thank you
20th Feb 2017, 10:10 AM
Saswat saubhagya Rout
Saswat saubhagya Rout - avatar