Lecture 9

Lecture 9 - 3/27/11 ECE 364: Software Tools Laboratory...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
3/27/11 1 ECE 364: Software Tools Laboratory Lecture 9 March 28, 2011 Lecturer: Michael Goldfarb 1 Lecture 9 § Dictionaries § Classes § Lecture examples can be found in § ~ee364/examples/LEC-09 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3/27/11 2 Dictionaries § A Python dictionary is an associative container § A key is associated with a value § Unlike lists, dictionaries are NOT ordered § A key can be any Python type § Integers, strings, tuples, lists, etc. § Not limited to built-in types § A value can also be any Python type § Just like lists and tuples 3 Creating a Dictionary § Dictionaries use {} and have comma separated values consisting of key:value pairs § Example A = { "Foobar" : 100, 2 : “Big Deal”, (1, 2, 34) : [[1, 2], ”Baz”]} 4
Background image of page 2
3/27/11 3 Accessing Items § Dictionary values are accessed using square brackets and indexing with a key A[Key] # Gets the value associated with Key § If a key:value pair is not present a KeyError exception is raised A[Key] # Raises KeyError if not found 5 Adding Items § Add a value to a dictionary: A[Key] = Value § Merge two dictionaries: >>> A = {1:20, -5:7, 8.2:31} >>> B = {1:'foobar', 9:0} >>> A.update(B) >>> print A {1: 'foobar', -5: 7, 9: 0, 8.2: 31} 6
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3/27/11 4 Removing Items § Delete a value from a dictionary: del A[Key] § Pop a value from a dictionary A.pop(Key) § Pop a key/value pair from a dictionary key, val = A.popitem() 7 Checking for Items § The following methods are equivalent: Key in A A.has_key(Key) § Each returns True if A contains the key Key § To negate the test: Key not in A not A.has_key(Key) 8
Background image of page 4
3/27/11 5 Keys § To obtain a list of the keys in a dictionary, the built-in method keys() may be used: >>> A = { "Foobar" : 100, ... 2 : "Big Deal", ... (1, 2, 34) : [[1, 2], "Yuk"]} >>> print A.keys() [(1, 2, 34), 2, 'Foobar'] 9 Values § To obtain a list of the values in a dictionary, the built-in method values() may be used: >>> A = { "Foobar" : 100, ... 2 : "Big Deal", ... (1, 2, 34) : [[1, 2], "Yuk"]} >>> print A.values() [[[1, 2], 'Yuk'], 100, 'Big Deal'] 10
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3/27/11 6 Items § A list of key:value pairs can be obtained using the items() member function § Returns a list of tuples of the form (key, value) § Example >>> print A.items() [((1, 2, 34), [[1, 2], ’Baz’]), ('Foobar', 100), (2, 'Big Deal')] 11 Iterating § In a for loop, a dictionary returns its keys: for key in Dictionary: ...
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

Lecture 9 - 3/27/11 ECE 364: Software Tools Laboratory...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online