By David “Prooffreader — with two f’s, that’s the joke!” Taylor
This also appears as a blog post.
If you’d like to know an easy way to turn an IPython Notebook into a blog post, I wrote a blog post about that too!
I’ve been programming all my life, but never been a programmer. Most of my work was done in Visual Basic because it’s what I was most comfortable with, plus a smattering of other languages (R, C, JavaScript, etc.). A couple of years ago, I decided to use Python exclusively so that I could improve my coding. I ended up re-inventing many wheels — which I didn’t mind too much, because I enjoy solving puzzles. But sometimes it’s good to have a more efficient, Pythonesque approach, and time after time I found myself having “aha!” moments, realizing I’d been doing things the hard, excessively verbose way for no reason. Here is a list of ten Python idioms that would have made my life much easier if I’d thought to search for them early on.
Missing from this list are some idioms such as list comprehensions and lambda functions, which are very Pythonesque and very efficient and very cool, but also very difficult to miss because they’re mentioned on StackOverflow every other answer! Also ternary x if y else z constructions, decorators and generators, because I don’t use them very often.
########################################
1. Python 3-style printing in Python 2
One of the things that kept me from concentrating on Python was this whole version 2 – version 3 debacle. Finally I went with Python 2 because all the libraries I wanted were not 3-compatible, and I figured if I needed to, I would laboriously adjust my code later.
But really, the biggest differences in everyday programming are printing and division, and now I just import from future.
mynumber = 5
print "Python 2:"
print "The number is %d" % (mynumber)
print mynumber / 2,
print mynumber // 2
from __future__ import print_function
from __future__ import division
print('nPython 3:')
print("The number is {}".format(mynumber))
print(mynumber / 2, end=' ')
print(mynumber // 2)
Oh, and here’s an easter egg for C programmers:
from __future__ import braces
mylist = ["It's", 'only', 'a', 'model']
for index, item in enumerate(mylist):
print(index, item)
3. Chained comparison operators
Because I was so used to statically typed languages (where this idiom would be ambiguous), it never occurred to me to put two operators in the same expression. In many languages, 4 > 3 > 2 would return as False, because (4 > 3) would be evaluated as a boolean, and then True > 2 would be evaluated as False.
mynumber = 3
if 4 > mynumber > 2:
print("Chained comparison operators work! n" * 3)
from collections import Counter
from random import randrange
import pprint
mycounter = Counter()
for i in range(100):
random_number = randrange(10)
mycounter[random_number] += 1
for i in range(10):
print(i, mycounter[i])
my_phrase = ["No", "one", "expects", "the", "Spanish", "Inquisition"]
my_dict = {key: value for value, key in enumerate(my_phrase)}
print(my_dict)
reversed_dict = {value: key for key, value in my_dict.items()}
print(reversed_dict)
import subprocess
output = subprocess.check_output('dir', shell=True)
print(output)
my_dict = {'name': 'Lancelot', 'quest': 'Holy Grail', 'favourite_color': 'blue'}
print(my_dict.get('airspeed velocity of an unladen swallow', 'African or European?n'))
for key, value in my_dict.iteritems():
print(key, value, sep=": ")
a = 'Spam'
b = 'Eggs'
print(a, b)
a, b = b, a
print(a, b)
my_dict = {'That': 'an ex-parrot!'}
help(my_dict)
PEP8 is the style guide for Python code. Among other things, it directs that lines not be over 80 characters long and that indenting by consistent over line breaks.
This can be accomplished with a combination of backslashes ; parentheses () with commas , ; and addition operators +; but every one of these solutions is awkward for multiline strings. There is a multiline string signifier, the triple quote, but it does not allow consistent indenting over line breaks.
There is a solution: parentheses without commas. I don’t know why this works, but I’m glad it does.
my_long_text = ("We are no longer the knights who say Ni! "
"We are now the knights who say ekki-ekki-"
"ekki-p'tang-zoom-boing-z'nourrwringmm!")
print(my_long_text)
By David “Prooffreader — with two f’s, that’s the joke!” Taylor
This also appears as a blog post.
If you’d like to know an easy way to turn an IPython Notebook into a blog post, I wrote a blog post about that too!
I’ve been programming all my life, but never been a programmer. Most of my work was done in Visual Basic because it’s what I was most comfortable with, plus a smattering of other languages (R, C, JavaScript, etc.). A couple of years ago, I decided to use Python exclusively so that I could improve my coding. I ended up re-inventing many wheels — which I didn’t mind too much, because I enjoy solving puzzles. But sometimes it’s good to have a more efficient, Pythonesque approach, and time after time I found myself having “aha!” moments, realizing I’d been doing things the hard, excessively verbose way for no reason. Here is a list of ten Python idioms that would have made my life much easier if I’d thought to search for them early on.
Missing from this list are some idioms such as list comprehensions and lambda functions, which are very Pythonesque and very efficient and very cool, but also very difficult to miss because they’re mentioned on StackOverflow every other answer! Also ternary x if y else z constructions, decorators and generators, because I don’t use them very often.
########################################
1. Python 3-style printing in Python 2
One of the things that kept me from concentrating on Python was this whole version 2 – version 3 debacle. Finally I went with Python 2 because all the libraries I wanted were not 3-compatible, and I figured if I needed to, I would laboriously adjust my code later.
But really, the biggest differences in everyday programming are printing and division, and now I just import from future.
mynumber = 5
print "Python 2:"
print "The number is %d" % (mynumber)
print mynumber / 2,
print mynumber // 2
from __future__ import print_function
from __future__ import division
print('nPython 3:')
print("The number is {}".format(mynumber))
print(mynumber / 2, end=' ')
print(mynumber // 2)
Oh, and here’s an easter egg for C programmers:
from __future__ import braces
mylist = ["It's", 'only', 'a', 'model']
for index, item in enumerate(mylist):
print(index, item)
3. Chained comparison operators
Because I was so used to statically typed languages (where this idiom would be ambiguous), it never occurred to me to put two operators in the same expression. In many languages, 4 > 3 > 2 would return as False, because (4 > 3) would be evaluated as a boolean, and then True > 2 would be evaluated as False.
mynumber = 3
if 4 > mynumber > 2:
print("Chained comparison operators work! n" * 3)
from collections import Counter
from random import randrange
import pprint
mycounter = Counter()
for i in range(100):
random_number = randrange(10)
mycounter[random_number] += 1
for i in range(10):
print(i, mycounter[i])
my_phrase = ["No", "one", "expects", "the", "Spanish", "Inquisition"]
my_dict = {key: value for value, key in enumerate(my_phrase)}
print(my_dict)
reversed_dict = {value: key for key, value in my_dict.items()}
print(reversed_dict)
import subprocess
output = subprocess.check_output('dir', shell=True)
print(output)
my_dict = {'name': 'Lancelot', 'quest': 'Holy Grail', 'favourite_color': 'blue'}
print(my_dict.get('airspeed velocity of an unladen swallow', 'African or European?n'))
for key, value in my_dict.iteritems():
print(key, value, sep=": ")
a = 'Spam'
b = 'Eggs'
print(a, b)
a, b = b, a
print(a, b)
my_dict = {'That': 'an ex-parrot!'}
help(my_dict)
PEP8 is the style guide for Python code. Among other things, it directs that lines not be over 80 characters long and that indenting by consistent over line breaks.
This can be accomplished with a combination of backslashes ; parentheses () with commas , ; and addition operators +; but every one of these solutions is awkward for multiline strings. There is a multiline string signifier, the triple quote, but it does not allow consistent indenting over line breaks.
There is a solution: parentheses without commas. I don’t know why this works, but I’m glad it does.
my_long_text = ("We are no longer the knights who say Ni! "
"We are now the knights who say ekki-ekki-"
"ekki-p'tang-zoom-boing-z'nourrwringmm!")
print(my_long_text)
Similar Notebooks