On-screen text
Python DictionariesnChapter 9nPython for Everybodynwww.py4e.comnWhat is a Collection?nA collection is nice because we can put more than one value in it and carry them all around in one convenient packagenWe have a bunch of values in a single "variable"nWe do this by having more than one place "in" the variablenWe have ways of finding the different places in the variablenWhat is Not a "Collection"?nMost of our variables have one value in them - when we put a new value in the variable - the old value is overwrittenn$ pythonn>>> x = 2n>>> x = 4n>>> print(x)n4nA Story of Two Collections..nListnA linear collection of valuesnLookup by position 0.. length-1nDictionarynA linear collection of key-value pairsnLookup by "tag" or "key"nDictionariesnDictionaries are Python's most powerful data collectionnDictionaries allow us to do fast database-like operations in PythonnSimilar concepts in different programming languagesn- Associative Arrays - Perl / PHPn- Properties or Map or HashMap - Javan- Property Bag - C# / .NetnDictionaries over time in PythonnPrior to Python 3.7 dictionaries did not keep entries in the order of insertionnPython 3.7 (2018) and later dictionaries keep entries in the order they were insertedn"insertion order" is not "always sorted order"nBelow the AbstractionnPython lists, dictionaries, and tuples are "abstract objects" designed to be easy to usenFor now we will just understand them and use them and thank the creators of Python for making them easy for usnUsing Python collections is easy. Creating the code to support them is tricky and uses Computer Science concepts like dynamic memory, arrays, linked lists, hash maps and trees.nBut that implementation detail is for a later course...nLists (Review)nWe append values to the end of a List and look them up by positionnWe insert values into a Dictionary using a key and retrieve them using a keynComparing Lists and DictionariesnDictionaries are like lists except that they use keys instead of positions to look up valuesnDictionary Literals (Constants)nDictionary literals use curly braces and have key : value pairsnYou can make an empty dictionary using empty curly bracesnAdditional online coursesn(including a C course)nare available atnonline.dr-chuck.comnMusic: Soul and Mind by E's Jammy JamsnPYTHONnFOR EVERYBODYnC ProgrammingnA Historical PerspectivenCHARLES SEVERANCE