This is simply a dictionary of Python keywords, implemented as – of course – a Python dictionary! Dictionaries are key:value pairs. The value can be any object, such as a tuple of strings. The first item in the tuple is part of the URL to documentation at Python.org, and the second item is a brief description of the keyword’s purpose. In a perpetual loop, we: prompt the user for the word to lookup; quit if required; try to lookup the word. If successful we print the results, otherwise give the user a list of the keywords in the dictionary.
Note that we’re using Python 2 print instead of Python print() due to trinket limitations.
Another useful data type built into Python is the dictionary (see Mapping Types — dict). Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().
[welcomewikilite wikiurl=”http://en.wikipedia.org/wiki/Associative_array” sections=”Short description” settings=””]