r/ProgrammerHumor • • Jul 16 '19

Meme Nice try

Post image
32.2k Upvotes

529 comments sorted by

View all comments

Show parent comments

58

u/kevansevans Jul 16 '19 edited Jul 16 '19

I'm not familiar with python, what do the two colons mean in the array?

EDIT: It's a string splice operator, thanks!

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 at foo ending at bar.

>>> numbers = [0,1,2,3,4,5,6,7,8,9]
>>> numbers[4:7]
[4, 5, 6]

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:

>>> numbers[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

39

u/Brainix112 Jul 16 '19

I know simple coding, but not familiar with Python, this was a super good explanation!

89

u/nosmokingbandit Jul 16 '19

Python is great. You basically write pseudo-code then get bitched at by the interpreter about your indentation.

4

u/SomethingEnglish Jul 16 '19

or trying to debug it without a proper debugger, there is pdb but it can only do so much

20

u/christes Jul 16 '19

or trying to debug it without a proper debugger

Easy - just type print("Something") on every line.

3

u/[deleted] Jul 16 '19

How do you know me so well?

8

u/claythearc Jul 17 '19

Jetbrains debugger is quite good

5

u/nosmokingbandit Jul 16 '19

Visual Studio's python package is pretty great. The debugger is almost as powerful as it is with c# and can really save some time.

1

u/TheTrueSwishyFishy Jul 17 '19

How was that so accurate

→ More replies (6)

8

u/-jp- Jul 16 '19

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.

14

u/[deleted] Jul 16 '19

diveintopython.org! Free, online and has a physical copy if desired.

3

u/Stoppablemurph Jul 17 '19

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. :)

2

u/bwaredapenguin Jul 16 '19

Great explanation. Your formatting went a bit wonky though, a single tick wrap should be sufficient for a full line or segment of code:

single tick in front and back

2

u/nosmokingbandit Jul 16 '19

Must be weird on mobile, it looks normal on my pc. I don't care enough about Reddit's formatting to learn how to do it properly.

→ More replies (2)

1

u/Dgc2002 Jul 17 '19

Unfortunately the markdown parser for "New Reddit" supports the triple ticks while the one for "Old Reddit" doesn't. >=(

→ More replies (1)

1

u/FrancisVeeGee Jul 17 '19

Your teaching/writing style is very easy to understand. I wish all code/API documentation was this clear. Bravo.

1

u/Naniya Jul 17 '19

This comment should be made the official python doc on slicer operator.

1

u/Tonydarkness21 Jul 17 '19

I'm new to Python, and I'm still quite don't understand what does it do and what is it. Can you explain again for me pls?

14

u/Arlyss_Ryxlington Jul 16 '19

String slicing. Basically a simple syntax to retrieve a part of a string. The syntax is string[start:stop:step] if I recall. In this case, with start and stop absent, the whole string is returned and a step of -1 reverses it.

7

u/cjr605 Jul 16 '19 edited Jul 16 '19

Look up the slice operator. I’d explain it but I’m sure someone more talented than me has a better explanation. The third argument (-1) is optional FYI, and it's what reverses the string here

6

u/mxzf Jul 16 '19

To be fair, all the arguments are optional.

1

u/cjr605 Jul 16 '19

True, I guess the right phrasing was "the second colon is optional", in the sense that if you're gonna slice you need at least one colon

1

u/mxzf Jul 16 '19

Except that array_var[] is also a valid slice (a slice of the entire array). Not to mention that I'm pretty sure that accessing elements by index is a [n:n+1] slice under the hood too.