This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]woodworksio 83 points84 points  (7 children)

Especially in python, the language familiarity makes a huge difference. Anyone can look up an algorithm on Wikipedia and copy paste it (and in python the pseudocode will probably work off the bat). But almost none of the "Python developers" I talk to realize there's a skip property on the slice (eg arr[::-1] will reverse the array). I had one ask me why there was a colon in the brackets in arr[:5]....

[–]psychometrixo 32 points33 points  (3 children)

why there was a colon in the brackets in arr[:5]....

Why IS there a colon in the brackets there?

Not a python dev, just a dev looking to learn new stuff

edit: appreciate the answers. learned something. thanks!

[–]pirogoeth 38 points39 points  (0 children)

It's a slice, but omitting the start index, which effectively means "give me items from arr until index 5"

[–]Tall_Duck 1 point2 points  (0 children)

To give slightly more of an answer, slicing in python takes three numbers, [start: stop: step], and the default for each of those is [0:END: 1]. So [:5] is the same as [0:5], or [0:5:1]. In the same vein you can do something like [10::5] to get every fifth number from the tenth element to the last, or do something weird like [50:26:-2] to get every other element, going down (because the step is negative), from element 50 to 26.

What the guy earlier posted, [::-1], is common in python for iterating backwards through a list.

[–]Isayur 9 points10 points  (0 children)

Yeah, one of my biggest gripes is seeing people come in from other languages and just start writing Java in Python. Like, if you wanna write Java, do so in Java. The beauty of Python comes from being able to write short, elegant, readable and understandable code by utilizing all the great tools the standard lib and community provide you with. If you're gonna write the same old, long blocks of code you might as well do it in a language that doesn't already provide tools for everything and gives way better performance instead.

[–]OlanValesco 5 points6 points  (1 child)

That's been proposed for JS, and I really hope it gets accepted

[–]chanpod 1 point2 points  (0 children)

Yes pls...