I have an array as shown
marks = [1,2,3,5,6,7,8]
I would like to understand the different between the following notations
marks[::-2]
, marks[None:None:-2]
and marks[0:len(marks):-2]
I know there have been some attempts to answer the same question , But am not understanding those.
What i could gather from the other responses is that when the indices are omitted , start would default to 0 , end would default to len(marks) and step size would default to 1 .
Also omission of indices and giving None
would yield same behavior . But i never observed it , especially in the above cases . For example if the above statement is true then
marks[::-2]
= marks[0:len(marks):-2]
which should give an empty list because you can never start from 0 index and reach an index len(marks) - 1 taking a negative step size of 2 but instead marks[::-2]
is giving me [8, 6, 3, 1]
what is the mechanism behind this ? Also please explain what None really means in this context
Edit
Below is the answer to the question which was mentioned as a comment in one of the answers given to a similar question although the answer wasn't accepted
maxint = 10000000 if step is None: step = 1 elif step == 0: raise ValueError('slice step cannot be zero') if start is None: start = maxint if step < 0 else 0 if stop is None: stop = -maxint if step < 0 else maxint