+ 1

In list slicing, in the following description, why is semi colon used twice?

squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(squares[::2])

9th Feb 2021, 3:22 AM
PHILOMATH PRATIK
PHILOMATH PRATIK - avatar
3 Antworten
+ 9
That is called colon. semicolon ; colon : iterable [ start : end : steps ] Default Values: start ---> 0 end ---> last element steps ---> 1 In your example, we did not sepcify the 'start' and 'end' index, therefore it is 0 and last element by default. And on the third part, which is 2 that describes steps. In words, from start to end with step of 2. ________________________________________ squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(squares[::2]) >> [0, 4, 16, 36, 64] ________________________________________ Try to experiment in code playground to understand index slicing better. If this is still unclear, feel free to ask. Thanks
9th Feb 2021, 3:43 AM
noteve
noteve - avatar
+ 1
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2453/ Cyan you should correct your example output: [0, 4, 16, 36, 64]
9th Feb 2021, 5:25 AM
visph
visph - avatar
+ 1
visph Thanks!
9th Feb 2021, 5:27 AM
noteve
noteve - avatar