Monday, 4 November 2013

Python quirks

My old favourite Python quirk

Reversing a list
  l = [1,2,3,4,5]
  print l[::-1]
Output:
[5,4,3,2,1]

My new favourite Python quirk (discovered today)

for j in range(3):
    print j
else:
    print "the last item was", j
Output:
0
1
2
the last item was 2
Yes, after coding in Python for all these years, I had no idea that one could write a "for... else..." loop. Not quite as useful as the list reversal one, but strangely appealing nonetheless.
Now try:
from __future__ import braces
And there are still some people who won't admit that Python is hands-down the best language there is.

No comments:

Post a Comment