I am a newbie for Python and have come in the following examples in my book that have not been explained very well. Here's my print out with the interpreter:
& gt; & Gt; & Gt; S = 'spam' & gt; & Gt; & Gt; S [: - 1] 'spa'
why there is no beginning and the action of slicing with one '- 1'
Does it back? Is calling s [0: -1]
logically similar to calling s [: - 1]
? Both of them return the same results but I'm not sure what the dragon is doing exactly any help would be greatly appreciated.
Yes, calling s [0: -1]
exactly like calling Is: s [: - 1]
.
In the dragon, using the negative number as a pointer gives the nth element with the right hand to the list (unlike the left hand side).
So if you have any such list:
myList = ['a', 'b', 'c', 'd', 'e'] print MyList [-1] # Print 'E'
Print Statement "E" will print.
Once you understand that (which you already have, it is not completely clear if it is one of the things that is confused about you or not) we You can begin to talk about pieces. Understand the basics of a piece with lines of
myList [2: 4]
(which will return ['c', 'd']
) And jump straight into the skiing, where there is left blank on one side.
As you suspect in your post, myList [: index]
is same as myList [0: index]
.
This also works in another way, the way ... myList [index:]
as myList [index: len (myList)] is < Will return a list of all elements from the list starting with / code> and
). index
and going to the end (like print myList [2:]
will print < Code> ['c', 'd', 'e']
As a third thing, you are prompted to print myList [:]
Em> No index, which is basically a copy of the complete list (< Code> myList [[a ',' b ',' c ',' d ',' e ']) will return equal to [len (myList)] This can be useful if you think my list is changing in a few moments, but you want to keep a copy of it in its current state.
If you are not doing this already, then I just roam the bus. A Python interpreter, a whole bunch, will help a great deal to understand these things. I suggest.
Comments
Post a Comment