Can you explain more about list slices please? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

Can you explain more about list slices please?

squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(squares[:7]) print(squares[7:]) i dont understand this code☹️

29th Aug 2020, 1:28 PM
Sadat Rahman
Sadat Rahman - avatar
2 Réponses
+ 3
Python Lists work like this: sqaures[start:stop:step] -- where "start" is the starting index, "stop" is the stopping index and "step" is the direction and indices to include (this example does not have "step" therefore it defaults to 1) squares[:7] or squares[0:7] means the first 7 objects in the list, so 0-6. This does not include 7. squares[7:] or squares[7:-1] means to start listing the objects starting at the 7th position and continue to list all the objects until completion. I hope this helps. Good Luck
29th Aug 2020, 1:40 PM
Steven M
Steven M - avatar
+ 3
thanks
29th Aug 2020, 1:47 PM
Sadat Rahman
Sadat Rahman - avatar