7 ms·
Good list. My only thoughts: Prefer itertools.izip instead of zip to avoid materializing a new list. Appending the comma operator to the print statement suppr
by shadowmatter 14y ago
Good list. My only thoughts:
Prefer itertools.izip instead of zip to avoid materializing a new list.
Appending the comma operator to the print statement suppresses the newline character and appends a space instead, which is how print 1, "world" works.
The only tip I'd add is using mapping keys in string formatting operations:
>>> d = {'cow': 'moo'}
>>> print 'the cow says %(cow)s' % d
the cow says moo
- buster 14y agoFor python beginners i would not mention the print statement. It won't work for python 3.x. So, beginners: Please use print() instead of print, to save you some hazzle when using python3 :)
- ch0wn 14y agoWithout `from __future__ import print_function`, print doesn't work the same way in Py2k, though.
- mahmoudhossam 14y agoIn 2.7, it does.
- buster 14y agoUnfortunately not.. atleast not in my 2.7.3 prompt. Only with the __future__ import :(
- jau 14y agoIn 2.6.6 also works.
- ch0wn 14y agoIf you use print() in py2k, you're still using the print statement and group the argument in parentheses. It falls apart, if you try to use more than one argument, because it's interpreted as a tuple then. Python3: >>> print("hello", "world") hello world Python2: >>> print("hello", "world") ('hello', 'world')
- ch0wn 14y agoIn Python 3, zip() fortunately already returns an iterator.