+ 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])
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
+ 1
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2453/
Cyan you should correct your example output: [0, 4, 16, 36, 64]
+ 1
visph Thanks!