CS 130R: Programming in Python - Emory University

CS 130R: Programming in Python #10: Dictionary

Reading: Chapter 5

? Dictionary

Contents

Dictionary

Dictionary - a reference source in print or electronic form containing words usually alphabetically arranged along with information about their forms, pronunciations, functions, etymologies, meanings, and syntactical and idiomatic uses.

Electronic dictionary: Cat --- Scientific name: felis catus. A cat is a small,

meat-eating animal that is often kept as a pet.

word ~ definition, picture

Python --- Scientific name: genus: python. Pythons are nonvenomous snakes that usually kill their prey by wrapping around them and suffocating them.

Dictionary

Dictionary ? is a list of pairs (word ~ definition) Python dictionary ? is a list of pairs (key : value) ? The Python dictionary works the same way as the

actual dictionary - you look up a key and get its value(s). ? The idea is to organize information for fast retrieval. ? Very efficient implementation.

Dictionary

? A Python dictionary is an extremely useful data storage construct for organizing information in pairs (key: value). The association of a key and a value is called a key-value pair or an item.

? Analog to a hashtable in Java. A hash is a function that takes a value (of any kind) and returns an integer (that is the memory location of the key). Dictionaries use these integers, called hash values, to store and look up key-value pairs.

? The difference is that in Python the key can be any immutable type (string, number, tuple).

? Any key is a unique string, number or tuple. A key can have associated one or more values in a list. The values of a dictionary can be any Python data type.

? Since dictionaries are mutable, they can't be used as keys, but they can be used as values.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download