mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
some simple example
This commit is contained in:
parent
c3d315eb4f
commit
ac4d1152c6
@ -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)
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user