Python Experiment No: 5 Study and Implementation Dictionary.

Study and Implementation Dictionary.
Aim:- Implementation of Dictionary
Theory:-

All of the compound data types we have studied in detail so far—strings, lists, and tuples—are sequence types, which use integers as indices to access the multiple values they contain.
Dictionaries are a different kind of compound type. They are Python’s mapping type. They map keys which can be of any immutable type to values which can be of any type, just like the values of a list or tuple.
As an example, we will create a dictionary to translate English words into Spanish. For this dictionary, the keys are strings. One way to create a dictionary is to start with the empty dictionary and add key-value pairs . The empty dictionary is denoted fg:

>>> eng2sp = {}
>>> eng2sp[’one’] = ’uno’
>>> eng2sp[’two’] = ’dos’

 
The first assignment creates a dictionary named eng2sp; the other assignments add new key- value pairs to the dictionary. We can print the current value of the dictionary in the usual way:
 
>>> print eng2sp
{’two’: ’dos’, ’one’: ’uno’}
 
The key-value pairs of the dictionary are separated by commas. Each pair contains a key and a value separated by a colon. The order of the pairs may not be what you expected. Python uses complex algorithms to determine where the key-value pairs are stored in a dictionary. For our purposes we can think of this ordering as unpredictable. Another way to create a dictionary is to provide a list of key-value pairs using the same syntax as the previous output:
 
>>> eng2sp = {’one’: ’uno’, ’two’: ’dos’, ’three’: ’tres’}
 
It doesn’t matter what order we write the pairs. The values in a dictionary are accessed with keys, not with indices, so there is no need to care about ordering. Here is how we use a key to look up the corresponding value:
 
>>> print eng2sp[’two’]
’dos’
 
The key ’two’ yields the value ’dos’.
Dictionary operations:-
The del statement removes a key-value pair from a dictionary. For example, the following dictionary contains the names of various fruits and the number of each fruit in stock:
 
>>> inventory = {’apples’: 430, ’bananas’: 312, ’oranges’: 525, ’pears’: 217}>>> print inventory
 {’oranges’: 525, ’apples’: 430, ’pears’: 217, ’bananas’: 312}
 
If someone buys all of the pears, we can remove the entry from the dictionary:
 
>>> del inventory[’pears’]
>>> print inventory
{’oranges’: 525, ’apples’: 430, ’bananas’: 312}
 
Or if we’re expecting more pears soon, we might just change the value associated with pears:
 
>>> inventory[’pears’] = 0
>>> print inventory
{’oranges’: 525, ’apples’: 430, ’pears’: 0, ’bananas’: 312}
 
The len function also works on dictionaries; it returns the number of key-value pairs:

>>> len(inventory)
4

 
Dictionary methods:-
Dictionaries have a number of useful built-in methods The keys method takes a dictionary and returns a list of the keys that appear, but instead of the function syntax keys(eng2sp), we use the method syntax eng2sp.keys().
 
>>> eng2sp.keys()
[’one’, ’three’, ’two’]
 
This form of dot notation specifies the name of the method, keys, and the name of the object on
which to apply the method, eng2sp. The parentheses indicate that this method takes no parameters. A method call is called an invocation; in this case, we would say that we are invoking the keys method on the object eng2sp. The values method is similar; it returns a list of the values in the dictionary:
 
>>> eng2sp.values()
[’uno’, ’tres’, ’dos’]

 
The items method returns both, in the form of a list of tuples—one for each key-value pair:
 
>>> eng2sp.items()
[(’one’,’uno’), (’three’, ’tres’), (’two’, ’dos’)]
 
The syntax provides useful type information. The square brackets indicate that this is a list. The parentheses indicate that the elements of the list are tuples.
If a method takes an argument, it uses the same syntax as a function call. For example, the method has key takes a key and returns true if the key appears in the dictionary:
 
>>> eng2sp.has_key(’one’)
True>>> eng2sp.has_key(’deux’)
False
 
If you try to call a method without specifying an object, you get an error. In this case, the error message is not very helpful:

>>> has_key(’one’)
NameError: name ’has_key’ is not defined


Q1) What is the difference between a list and a dictionary?
Q2) What would I use a dictionary for?
Q3) What would I use a list for?
Q4) What if I need a dictionary, but I need it to be in order?

Comments