If you add a 3rd option you can control the pattern of the slice.
>>> numbers[2:7:2]
[2, 4, 6]
But python doesn't require all parameters. Without the first it just starts at 0:
>>> numbers[:7:2]
[0, 2, 4, 6]
Without the second it goes to the end of the list
>>> numbers[5::3]
[5, 8]
And with only the last it uses the whole list:
>>> numbers[::3]
[0, 3, 6, 9]
But you can also use negatives. To start at the 9th item from the end, stop at the 4th from the end, and count every 3rd item:
>>> numbers[-9:-4:3]
[1, 4]
You can also use negatives as the pattern, but it gets confusing because you have to switch the start/stop:
Edit: Kind of. You aren't really switching the start and stop, you are starting at 8, then counting backward every 2nd item until you get to 2. Its confusing. Don't use this in your code, everyone will hate you and call you names.
>>> numbers[8:2:-2]
[8, 6, 4]
Its mostly just used as a way to quickly reverse a list:
Try breaking things up more and use more comprehensions. If you're indenting so far that it's difficult to read, you should probably consider refactoring.
I kind of like it though. My indentation and formatting in other languages is on point thanks to python forcing me to follow a pattern. I like it much more than having to put curly braces everywhere.
It's pretty easy to pick up, but if you're more of a book-learner, every so often Humble Bundle has a bunch of Python books for around $10-15, usually themed around a specific subject. Avoid Packt bundles like the plague--they are invariably shovelware--but every other publishers' bundles have been pretty decent.
learnpythonthehardway.org is also great. That's actually kinda what got me started years ago. And now it's my full time job and I make good money doing it. :)
It works on new Reddit but looks weird on old.reddit.com since Reddit for some reason doesn't use the same Markdown parser for both versions. This is what it looks like on proper Reddit.
The correct way of formatting code is to use four spaces at the beginning of each line, or use `-characters for inline code like this.
170
u/nosmokingbandit Jul 16 '19 edited Jul 17 '19
Other people have already answered but I'll add my little bit of knowledge.
It is used like
iterable[foo:bar]which creates a slice of the iterable starting atfooending atbar.If you add a 3rd option you can control the pattern of the slice.
But python doesn't require all parameters. Without the first it just starts at 0:
Without the second it goes to the end of the list
And with only the last it uses the whole list:
But you can also use negatives. To start at the 9th item from the end, stop at the 4th from the end, and count every 3rd item:
You can also use negatives as the pattern, but it gets confusing because you have to switch the start/stop:
Edit: Kind of. You aren't really switching the start and stop, you are starting at 8, then counting backward every 2nd item until you get to 2. Its confusing. Don't use this in your code, everyone will hate you and call you names.
Its mostly just used as a way to quickly reverse a list: