Python Programming Tutorial - 10 - Slicing
Top Comments
All Comments (294)
-
example=[1,2,4,7,5]
example[-1:0:-1]
Output
[5, 7, 4, 2]
What is wrong here?
the way I see it, count from -1(last element) to 0(first element) using -1(backwords with 1 increment) .. but then why first element gets skipped?
This code example[::-1] works perfectly fine and gives out all numbers in rev order
-
[10:0:02]
[4:]
anything like that doesnt work for me.
-
ur a better teacher than google
-
hi, nice tut's but i dont find them usefull... plz could yu make a tutorial of how to make a bot that could maybe link to a flash game? ty for reading.
-
I have a question: when you create a variable or a list, it is stored on your hard drive, correct? For how long is it stored/where is it stored? does it go away when you close python or will it stay there forever until it is deleted?
-
You could use
>>>example[4:]
this would give you all the numbers to the end of your list as well without using '-'
-
example[-5:10] WHy not?
-
1:13 :|]
-
Java programmer here... PYTHON IS SUPER COOL! 6:05
-
@rednut416 Actually, you can just type example, and it'll do the same thing. Even faster
@bubbleheadft Wrong. You probably just forgot to add "0" to the first section of your list.
"Typing [4:9]..." This will give you what's inside index 4 and up to (but does not include) 9. The index number 4 is actually the 5th index in the list. This is because the starting index is 0 on most programming languages.
I use 2.7.2 and everything works perfectly just perfect - so far.
JaffZe123 8 months ago 17
@bubbleheadft I'm using python 2.7.2 and typing [4:9] gives me the result: [4,5,6,7] as he says. Before vote up this comment check which result you have.
dariodcr 7 months ago 10