- Information
- AI Chat
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.
Was this document helpful?
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.
Grok worksheet 11
Course: Foundations Of Computing (COMP10001)
375 Documents
Students shared 375 documents in this course
University: University of Melbourne
Was this document helpful?
This is a preview
Do you want full access? Go Premium and unlock all 13 pages
Access to all documents
Get Unlimited Downloads
Improve your grades
Already Premium?
Grok 11
Dictionaries
Dictionaries allow you to create mappings from keys to values. For example, you might use
a dictionary to map Australian state names to the names of their corresponding capital cities:
(Yes, there is an intentional mistake in there — Melbie. We'll fix it shortly.)
The keys in the above dictionary are strings which represent the names of the
Australian states. Each key maps to a value, which also happens to be a string in
this case. In the general case, the keys and values can be of different types.
Dictionary values are constructed using the curly brace characters { and }. As a
special case, you can construct an empty dictionary using an opening and closing
brace with nothing in between:
In the above code, an empty dictionary is created and assigned to the
variable example_empty_dict. Currently this dictionary contains no mappings, so it is fairly
uninteresting. As you shall soon see, it is possible to add new mappings to an existing
dictionary, so it can be useful to construct empty dictionaries and later add values as
necessary.
Indexing Dictionaries
Like lists and strings, dictionaries are indexable. However, the indices of dictionaries are
keys of arbitrary value, whereas the indices of lists and strings are always integers. A given
key can only occur once in the dictionary, and is associated with a unique value (but you
can, of course, make the value a list containing multiple objects). You can look up values in
Why is this page out of focus?
This is a Premium document. Become Premium to read the whole document.
Why is this page out of focus?
This is a Premium document. Become Premium to read the whole document.
Why is this page out of focus?
This is a Premium document. Become Premium to read the whole document.
Why is this page out of focus?
This is a Premium document. Become Premium to read the whole document.