>>> >>> D = {'one':1, 'two':2, 'three':3} >>> D.keys() ['three', 'two', 'one'] >>> D.values() [3, 2, 1] >>> D.items() [('three', 3), ('two', 2), ('one', 1)] >>> D.has_key('one') True >>> D.has_key['four'] Traceback (most recent call last): File "", line 1, in -toplevel- D.has_key['four'] TypeError: unsubscriptable object >>> if D.has_key('four'): print D['four'] >>> D.clear() >>> D {} >>> D = {'one':1, 'two':2, 'three':3} >>> E = D.copy() >>> D {'three': 3, 'two': 2, 'one': 1} >>> E {'one': 1, 'three': 3, 'two': 2} >>> E['one'] = '111' >>> E {'one': '111', 'three': 3, 'two': 2} >>> D {'three': 3, 'two': 2, 'one': 1} >>> D['one'] 1 >>> D['four'] Traceback (most recent call last): File "", line 1, in -toplevel- D['four'] KeyError: 'four' >>> D.get('one') 1 >>> D.get('four') >>> print D.get('four') None >>> D.get('four', 4) 4 >>> D {'three': 3, 'two': 2, 'one': 1} >>> D.setdefault('four') >>> D {'four': None, 'three': 3, 'two': 2, 'one': 1} >>> D.setdefault('five', 5) 5 >>> D {'four': None, 'three': 3, 'five': 5, 'two': 2, 'one': 1} >>> E {'one': '111', 'three': 3, 'two': 2} >>> E.update(D) >>> E {'three': 3, 'one': 1, 'four': None, 'five': 5, 'two': 2} >>> E is D False >>> E == D True >>> D.popitem() ('four', None) >>> D {'three': 3, 'five': 5, 'two': 2, 'one': 1} >>> key, value = D.popitem() >>> key 'three' >>> value 3 >>> D {'five': 5, 'two': 2, 'one': 1} >>> 'fine' in D False >>> 'five' in D True >>> D.has_key('five') True >>> >>> >>> >>> globals() {'key': 'three', 'sub': , 'E': {'three': 3, 'one': 1, 'four': None, 'five': 5, 'two': 2}, 'multiple_lines': '\nfirst line\nsecond line\nthird line\n', '__builtins__': , 'str2': "That's right.", 'str1': 'Python is simple!', 'j': 99, 'L': [1, 2, 3, 'spam', 'ham'], 'value': 3, 'str3': "Don't just play.", 's': 'spam and ham', 'add': , 'long_str': 'long linecontinued..', 'action': {0: , 1: }, '__name__': '__main__', 'i': 98, '__doc__': None, 'D': {'five': 5, 'two': 2, 'one': 1}} >>> >>> locals() {'key': 'three', 'sub': , 'E': {'three': 3, 'one': 1, 'four': None, 'five': 5, 'two': 2}, 'multiple_lines': '\nfirst line\nsecond line\nthird line\n', '__builtins__': , 'str2': "That's right.", 'str1': 'Python is simple!', 'j': 99, 'L': [1, 2, 3, 'spam', 'ham'], 'value': 3, 'str3': "Don't just play.", 's': 'spam and ham', 'add': , 'long_str': 'long linecontinued..', 'action': {0: , 1: }, '__name__': '__main__', 'i': 98, '__doc__': None, 'D': {'five': 5, 'two': 2, 'one': 1}} >>> >>> def f(a,b): x = a + b y = a - b print locals() >>> f(1,2) {'y': -1, 'x': 3, 'b': 2, 'a': 1} >>> >>> def f(a,b): x = a + b y = a - b print locals() print globals() >>> f(1,2) {'y': -1, 'x': 3, 'b': 2, 'a': 1} {'key': 'three', 'sub': , 'E': {'three': 3, 'one': 1, 'four': None, 'five': 5, 'two': 2}, 'multiple_lines': '\nfirst line\nsecond line\nthird line\n', 'f': , '__builtins__': , 'str2': "That's right.", 'str1': 'Python is simple!', 'j': 99, 'L': [1, 2, 3, 'spam', 'ham'], 'value': 3, 'str3': "Don't just play.", 's': 'spam and ham', 'add': , 'long_str': 'long linecontinued..', 'action': {0: , 1: }, '__name__': '__main__', 'i': 98, '__doc__': None, 'D': {'five': 5, 'two': 2, 'one': 1}} >>> >>> >>> class C: a = 1 b = 2 >>> C.__dict__ {'a': 1, '__module__': '__main__', 'b': 2, '__doc__': None} >>> cc = C() >>> cc.x = 1 >>> cc.y = 2 >>> cc.__dict__ {'y': 2, 'x': 1} >>> cc.a 1 >>> cc.b 2 >>> dir(cc) ['__doc__', '__module__', 'a', 'b', 'x', 'y'] >>> >>> def f(): pass >>> f.a = 2 >>> f.b = 3 >>> f.a 2 >>> f.b 3 >>> f.__dict__ {'a': 2, 'b': 3} >>> >>> >>> >>> >>> D {'five': 5, 'two': 2, 'one': 1} >>> for key in D.keys(): print key, D[key] five 5 two 2 one 1 >>> for key in D: print key, D[key] five 5 two 2 one 1 >>> >>> items = D.items() >>> items [('five', 5), ('two', 2), ('one', 1)] >>> items.sort() >>> for k, v in items: print k, v five 5 one 1 two 2 >>>