Saturday 20 January 2018

syntax - What does :-1 mean in python?

It is list indexing, it returns all elements
[:] except the last one -1. Similar
question href="https://stackoverflow.com/questions/509211/the-python-slice-notation">here



For example,



>>> a =
[1,2,3,4,5,6]
>>> a[:-1]
[1, 2, 3, 4,
5]


It works like this




a[start:end]



>>>
a[1:2]
[2]


a[start:]



>>>
a[1:]

[2, 3, 4, 5,
6]


a[:end] />Your case



>>> a =
[1,2,3,4,5,6]
>>> a[:-1]
[1, 2, 3, 4,
5]



a[:]



>>>
a[:]
[1, 2, 3, 4, 5, 6]

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...