>>> wins = {} >>> wins["duke"] = 3 >>> wins["unc"] = 2 >>> wins["nc state"] = 1 >>> wins["wake"] = 1 >>> wins.items() [('duke', 3), ('unc', 2), ('wake', 1), ('nc state', 1)] >>> wins.keys() ['duke', 'unc', 'wake', 'nc state'] >>> wins.values() [3, 2, 1, 1] >>> x = wins.items() >>> sorted(x) [('duke', 3), ('nc state', 1), ('unc', 2), ('wake', 1)] >>> import operator >>> sorted(x,operator.itemgetter(1)) Traceback (most recent call last): File "", line 1, in TypeError: itemgetter expected 1 arguments, got 2 >>> sorted(x,key=operator.itemgetter(1)) [('wake', 1), ('nc state', 1), ('unc', 2), ('duke', 3)] >>> sorted(x,key=operator.itemgetter(1),reverse=True) [('duke', 3), ('unc', 2), ('wake', 1), ('nc state', 1)] >>> sorted(x,key=operator.itemgetter(1),reverse=true) Traceback (most recent call last): File "", line 1, in NameError: name 'true' is not defined >>> sorted(x,key=operator.itemgetter(1),reverse=True) [('duke', 3), ('unc', 2), ('wake', 1), ('nc state', 1)] >>> [x[1] for x in sorted(x,key=operator.itemgetter(1),reverse=True)] [3, 2, 1, 1] >>> [x[0] for x in sorted(x,key=operator.itemgetter(1),reverse=True)] Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not subscriptable >>> sorted(x,key=operator.itemgetter(1),reverse=True) Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not subscriptable >>> x = wins.items() >>> x [('duke', 3), ('unc', 2), ('wake', 1), ('nc state', 1)] >>> [y[0] for y in sorted(x,key=operator.itemgetter(1),reverse=True)] ['duke', 'unc', 'wake', 'nc state'] >>> x [('duke', 3), ('unc', 2), ('wake', 1), ('nc state', 1)] >>> x = ["a", "b", "c", "d"] >>> for i in range(len(x)): ... print x[i] ... a b c d >>> for i in range(len(x)): ... for j in range(i+1): ... print x[i],x[j] ... a a b a b b c a c b c c d a d b d c d d >>> x ['a', 'b', 'c', 'd'] >>> >>> [x for x in range(500,601) if x % 7 == 0] [504, 511, 518, 525, 532, 539, 546, 553, 560, 567, 574, 581, 588, 595] >>> >>> [x*x for x in range(200)) File "", line 1 [x*x for x in range(200)) ^ SyntaxError: invalid syntax >>> [x*x for x in range(200))] File "", line 1 [x*x for x in range(200))] ^ SyntaxError: invalid syntax >>> [x*x for x in range(200)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801, 10000, 10201, 10404, 10609, 10816, 11025, 11236, 11449, 11664, 11881, 12100, 12321, 12544, 12769, 12996, 13225, 13456, 13689, 13924, 14161, 14400, 14641, 14884, 15129, 15376, 15625, 15876, 16129, 16384, 16641, 16900, 17161, 17424, 17689, 17956, 18225, 18496, 18769, 19044, 19321, 19600, 19881, 20164, 20449, 20736, 21025, 21316, 21609, 21904, 22201, 22500, 22801, 23104, 23409, 23716, 24025, 24336, 24649, 24964, 25281, 25600, 25921, 26244, 26569, 26896, 27225, 27556, 27889, 28224, 28561, 28900, 29241, 29584, 29929, 30276, 30625, 30976, 31329, 31684, 32041, 32400, 32761, 33124, 33489, 33856, 34225, 34596, 34969, 35344, 35721, 36100, 36481, 36864, 37249, 37636, 38025, 38416, 38809, 39204, 39601] >>> [x*x for x in range(22)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441] >>> [x*x for x in range(23)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484] >>> [x*x for x in range(math.sqrt(500))] Traceback (most recent call last): File "", line 1, in NameError: name 'math' is not defined >>> import math >>> [x*x for x in range(math.sqrt(500))] Traceback (most recent call last): File "", line 1, in TypeError: range() integer end argument expected, got float. >>> [x*x for x in range(int(math.sqrt(500)))] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441] >>> [x*x for x in range(int(math.sqrt(500))+1)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484] >>> sorted("string") ['g', 'i', 'n', 'r', 's', 't'] >>> x = [1,2,3] >>> x.sort() >>> x [1, 2, 3] >>> x [3,2,1] Traceback (most recent call last): File "", line 1, in TypeError: list indices must be integers, not tuple >>> x = [3,2,1] >>> sorted(x) [1, 2, 3] >>> x [3, 2, 1] >>> x.sort() >>> x [1, 2, 3] >>> x = sorted(x) >>> x [1, 2, 3] >>> x = [("a",3), ("a", 2),("b",5)] >>> sorted(x) [('a', 2), ('a', 3), ('b', 5)] >>> x.sort() >>> x [('a', 2), ('a', 3), ('b', 5)] >>>