Another method that I find isn't used enough: dict.setdefault. Instead of doing this:<p><pre><code> if 'key' in d:
val = d['key']
else:
val = d['key'] = 'some default value'
</code></pre>
You can do this:<p><pre><code> val = d.setdefault('key', 'some default value')
</code></pre>
This is super-useful for configuration in frameworks, for example - it's a one-liner to set a default config value if the user didn't provide one.
Nice list. I only wish the author warned the readers that using the "get" method to get an item from the dictionary is not 100% equivalent to using try/except.<p>When using the method, the default value argument will always be evaluated, regardless of whether the dictionary key exists. If the expression that produces the default value is an expensive one or has side effects, the programmer might want to use the try/except variant instead.
Nice tips. One nitpick: the "inline if statement" is handy because it works as an expression, not just as a statement.<p>A few more useful things:<p>frozenset: immutable set values. these can be hashed, so you can use them as keys in dicts or put them in sets.<p>poor-man's transpose via zip:<p><pre><code> >>> x = [('a', 'b'), ('c', 'd')]
>>> zip(*x)
[('a', 'c'), ('b', 'd')]
</code></pre>
slices can be constructed with the "slice" function and passed around as values:<p><pre><code> >>> f = lambda x, s : x[s]
>>> first3 = slice(3)
>>> last3 = slice(-3, None)
>>> rev = slice(None, None, -1)
>>> f('hello', first3)
'hel'
>>> f('hello', last3)
'llo'
>>> f('hello', rev)
'olleh'</code></pre>
Good list. My only thoughts:<p>Prefer itertools.izip instead of zip to avoid materializing a new list.<p>Appending the comma operator to the print statement suppresses the newline character and appends a space instead, which is how print 1, "world" works.<p>The only tip I'd add is using mapping keys in string formatting operations:<p><pre><code> >>> d = {'cow': 'moo'}
>>> print 'the cow says %(cow)s' % d
the cow says moo</code></pre>
I actually would prefer people on my team NOT use these shortcuts. For example:<p><pre><code> numbers = [1, 2, 3, 4, 5, 6]
even = []
for number in numbers:
if number % 2 == 0:
even.append(number)
</code></pre>
is much easier to read and maintain imho then<p><pre><code> numbers = [1, 2, 3, 4, 5, 6]
even = [number for number in numbers if number % 2 == 0]
</code></pre>
Also:<p><pre><code> for x in range(1,101):print"Fizz"[x%3*4:]+"Buzz"[x%5*4:]or x
</code></pre>
is a piece of code I never want someone else to write that I may someday have to maintain. Write it out the long way and make your teams lives easier.
Great post! For those who know Python, this may be common knowledge, but for the uninitiated, this is a great showcase of some simple ways that Python makes coding more intuitive and enjoyable.<p>You might also consider mentioning both dictionary and set comprehensions along with list comprehensions, as they are the exact same concept, yet even a lot of intermediate Python programmers don't know about them!
The last one is terrible ... I have seen<p>#define TRUE 0
#define FALSE 1<p>back in the day.<p>Too bad there was no source control in the company then, so I could not find out who was responsible for that travesty and "talk calmly" some sense into them.
Shouldn't "the last 3" be [-3:] instead of [3:] ? Inyour case it gives the correct result, but that's because your list has 6 elements i think. ( note : typing on my phone so i can't verify).
Could it be possible to explain what does<p><pre><code> Print "Fizz" [x%3*4::]
</code></pre>
Since range is going from 0 to 101 I have the impression the result won't match the directive.
nice list, agree with the other comments that the Fizzbuzz example is cool, but can be confusing to work out what it does, especially since this post is aimed at beginners.<p>I would also mention `getattr` similarly to `get`.
It's confusing that output is on the >>> lines while input is not -- backwards from the REPL.<p>More things to consider: dict.iteritems, collections.defaultdict, enumerate(seq, 1)
Somethings's up with your site. All I see are black boxes where your examples should be: <a href="http://imgur.com/N8vdzGX" rel="nofollow">http://imgur.com/N8vdzGX</a>
Expression `False = True` reminds me of <a href="http://stackoverflow.com/a/771974/458610" rel="nofollow">http://stackoverflow.com/a/771974/458610</a>.