import sys; print('%s %s' % (sys.executable or sys.platform, sys.version)) /Library/Frameworks/Python.framework/Versions/7.3/Resources/Python.app/Contents/MacOS/Python 2.7.3 |EPD 7.3-2 (32-bit)| (default, Apr 12 2012, 11:28:34) [GCC 4.0.1 (Apple Inc. build 5493)] x = ['apple','pear','cherry','guava'] 'orange' in x Out[1]: False 'apple' in x Out[1]: True xx = x + x xx Out[1]: ['apple', 'pear', 'cherry', 'guava', 'apple', 'pear', 'cherry', 'guava'] xx = xx + xx xx Out[1]: ['apple', 'pear', 'cherry', 'guava', 'apple', 'pear', 'cherry', 'guava', 'apple', 'pear', 'cherry', 'guava', 'apple', 'pear', 'cherry', 'guava'] len(xx) Out[1]: 16 for i in range(10): xx = xx + xx len(xx) Out[1]: 16384 xx.count('apple') Out[1]: 4096 for i in range(10): xx = xx + xx len(xx) Out[1]: 16777216 xx.count('apple') Out[1]: 4194304 xx.count("orange") Out[1]: 0 for i in range(10): xx = xx + x len(xx) Out[1]: 16777256 for i in range(10): xx = xx + xx Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2538, in run_code exec code_obj in self.user_global_ns, self.user_ns File "", line 2, in xx = xx + xx MemoryError len(xx) Out[1]: 268436096 xx.count("orange") Out[1]: 0 import random import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! x = ['apple', 'xylophone', 'quince', 'orange'] x Out[1]: ['apple', 'xylophone', 'quince', 'orange'] sorted(x) Out[1]: ['apple', 'orange', 'quince', 'xylophone'] x Out[1]: ['apple', 'xylophone', 'quince', 'orange'] x.sort() x Out[1]: ['apple', 'orange', 'quince', 'xylophone'] sorted(x, reverse=True) Out[1]: ['xylophone', 'quince', 'orange', 'apple'] sorted(x, reverse=False) Out[1]: ['apple', 'orange', 'quince', 'xylophone'] y = [("apple",5), ("bean", 7), ("quince", 6)] import operator sorted(y) Out[1]: [('apple', 5), ('bean', 7), ('quince', 6)] y = sorted(x,reverse=True) y Out[1]: ['xylophone', 'quince', 'orange', 'apple'] y = [("apple",5), ("bean", 7), ("quince", 6)] z = sorted(y, reverse=True) z Out[1]: [('quince', 6), ('bean', 7), ('apple', 5)] sorted(z) Out[1]: [('apple', 5), ('bean', 7), ('quince', 6)] sorted(z, key=operator.itemgetter(1)) Out[1]: [('apple', 5), ('quince', 6), ('bean', 7)] sorted(z, key=operator.itemgetter(1),reverse=True) Out[1]: [('bean', 7), ('quince', 6), ('apple', 5)] [tup[0] for tup in sorted(z, key=operator.itemgetter(1),reverse=True)] Out[1]: ['bean', 'quince', 'apple'] thing = sorted(z, key=operator.itemgetter(1),reverse=True) thing Out[1]: [('bean', 7), ('quince', 6), ('apple', 5)] [tup[0] for tup in thing] Out[1]: ['bean', 'quince', 'apple'] d = {'apple':3,'bear':7,'radio':3} d.keys() Out[1]: ['radio', 'apple', 'bear'] d['apple'] Out[1]: 3 d['elephant'] Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2538, in run_code exec code_obj in self.user_global_ns, self.user_ns File "", line 1, in d['elephant'] KeyError: 'elephant' d.get('elephant',77) Out[1]: 77 w = 'tiger' if not w in d: d[w] = 0 d[w] += 1 d['tiger'] Out[1]: 1 d[w] = d.get(w,0)+1 d[w] Out[1]: 2 w = 'fox' d[w] = d.get(w,0)+1 d['fox'] Out[1]: 1