Sunday, August 9, 2015

Python on the go - Revision of Tuples and Dictionaries

Tuples are very similar to lists but they are not modifiable and they are not used as often as lists. The items in a tuple are enclosed by round brackets instead of the  square brackets that are used in lists.

days=("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",  "Sunday")

Dictionaries have keys and values. Each value in a dictionary can be obtained by using a key. These keys and values are enclosed in curly brackets.

 phonebook={'Jon':456782,  'Peter':2345233,  'Fred':5678123}

Dictionaries can be useful for finding values of certain keys such as the phone  number in the case above. For example, to find the phone number or value of the key 'Jo' , type in the python shell -

phonebook["Jon"]

An additional key and value can be added to the dictionary by typing in the program-

phonebook["Jack"]=546773


No comments:

Post a Comment