[Tutor] is the the two style of writting the same?
Steven D'Aprano
steve at pearwood.info
Wed Aug 21 07:54:11 CEST 2013
On Tue, Aug 20, 2013 at 05:20:23PM +0800, sikonai sikonai wrote:
> >>> list=[1,2,3,4,5,6,7,8,9]
> >>> list[9:0:-2]
> [9, 7, 5, 3]
> >>> list[10:0:-2]
> [9, 7, 5, 3]
>
> I want to know whether they have some difference.
Consider:
2+2
2*2
2**2
All three give the same result, 4, and yet they are different. You can
see that they are different, because if you change the numbers from 2 to
something else, the results will no longer be the same:
3+3
3*3
3**3
No longer equal! This proves that + * and ** are not the same thing.
Go back to your example:
L = [1, 2, 3, 4, 5, 6, 7, 8, 9]
It turns out that L[9:0:-2] happens to be the same as L[10:0:-2], but
that is only because the list only has 9 items. But if you do this:
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
for example, and then compare L[9:0:-2] and L[10:0:-2], you will see
they are quite different:
py> L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
py> L[9:0:-2]
[10, 8, 6, 4, 2]
py> L[10:0:-2]
[11, 9, 7, 5, 3]
--
Steven
More information about the Tutor
mailing list