some simple example

This commit is contained in:
Mari Wahl 2015-01-01 13:06:36 -05:00
parent c3d315eb4f
commit ac4d1152c6
2 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,26 @@
Bad:
# Filter elements greater than 4
a = [3, 4, 5]
b = []
for i in a:
if i > 4:
b.append(i)
Good:
a = [3, 4, 5]
b = [i for i in a if i > 4]
# Or:
b = filter(lambda x: x > 4, a)
Bad:
# Add three to all list members.
a = [3, 4, 5]
for i in range(len(a)):
a[i] += 3
Good:
a = [3, 4, 5]
a = [i + 3 for i in a]
# Or:
a = map(lambda i: i + 3, a)

View File

@ -0,0 +1,6 @@
foo = 'foo'
bar = 'bar'
print '%s%s' % (foo, bar) # It is OK
print '{0}{1}'.format(foo, bar) # It is better
print '{foo}{bar}'.format(foo=foo, bar=bar) # It is best