Shakespeare: Difference between revisions

imported>Zanwenhuahao
imported>Zanwenhuahao
Line 119:
* <b>keys</b> for each entry in the dictionary must be unique, <b> values </b> do not have to be unique.
* For our simplicity, we will use a string as keys. For more information on dictionary: https://docs.python.org/2/tutorial/datastructures.html#dictionaries
 
* Creating a dictionary:
<pre>
>>> myDict = {"Python":9, "Workshop": 27, "Interesting":1}
>>> myDict
{'Python': 9, 'Interesting': 1, 'Workshop': 27}
 
# notice that printing them does not necessarily give the entries in the order you entered them.
</pre>
 
* The following basic operations works with entries in a dictionary (remove an entry, add an entry, check if a key is in the dictionary, print the value corresponding to a given key):
<pre>
>>> del myDict["Interesting"]
>>> myDict
{'Python': 9, 'Workshop': 27}
>>> myDict["Interesting"]=1
>>> myDict
{'Python': 9, 'Interesting': 1, 'Workshop': 27}
>>> "Interesting" in myDict
True
>>> myDict["Interesting"]
1
</pre>
Anonymous user