This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'lmnop' in 'abcdefghijklmnopqrstuvwxyz' | |
index = 'python'.find('on') # 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
f = open(filename, 'r') | |
except IOError as e: | |
print 'cannot open ', filename | |
print "I/O error({0}): {1}".format(e.errno, e.strerror) | |
except: | |
log.exception("Unexpected error") | |
# print "Unexpected error:", sys.exc_info()[0] | |
raise | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[x**2 for x in (2,4,6)] | |
{x: x**2 for x in (2, 4, 6)} | |
{x for x in 'abracadabra' if x not in 'abc'} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matrix = [[1,2,3]] * 4 | |
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] | |
# FLATTEN THE MATRIX | |
flatten = [] | |
for row in matrix: | |
for i in range(3): | |
flatten.append(row[i]) | |
[row[i] for row in matrix for i in range(3)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in range(len(colors)): | |
print i, colors[i] | |
for i, colors in enumerate(colors): | |
print i, colors | |
# retuns enumerate object, not a list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = min(len(names), len(colors)) | |
for i in range(n): | |
print names[i], colors[i] | |
for names, color in zip(names, colors): | |
print name, color | |
#collections.izip is the iterator version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d = {} | |
for name in names: | |
key = len(name) | |
if key not in d: | |
d[key] = [] | |
d[key].append(name) | |
# slightly better | |
d = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
colors = ['green', 'blue', 'green', 'red', 'blue', 'green'] | |
d = {} | |
for color in colors: | |
if color not in d: | |
d[color] = 0 | |
d[color] += 1 | |
# slighlty better |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d = defaults.copy() | |
d.update(os.environ) | |
d.update(command_line_arguments) | |
# PYTHON3 ONLY!!! | |
from collections import ChainMap | |
d = ChainMap(command_line_arguments, os.environ, defaults) | |
# it's view, not a real dictionary. if you want something equivalent to a new one | |
d = ChainMap({}, command_line_arguments, os.environ, defaults) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibonacci(n): | |
x = 0 | |
y = 1 | |
for i in range(n): | |
print x | |
tmp = y | |
y = x + y | |
x = tmp | |