--- a/dictionary.org Tue May 04 16:52:38 2010 +0530
+++ b/dictionary.org Tue May 04 17:21:12 2010 +0530
@@ -1,114 +1,121 @@
-* Lists
+* Dictionaries
*** Outline
-***** Lists
-***** Tuples
+***** Dictionaries
+***** Sets
***** Arsenal Required
*** Script
Welcome friends.
- In previous tutorial we covered Lists and Tuples and related
- functions. In this session we will continue with Python supported
+ In previous tutorial we covered Lists, Tuples and related
+ functions. In this session we shall continue with Python
data structures and cover Dictionaries and sets. We have already
covered some basics of Dictionaries in session on Statistics. Here
- we will revisit those concepts and some new ones.
+ we shall revisit those concepts and some new ones.
- We give it a name and it should return a corresponding number.
+ We give it a name and it returns a corresponding number.
Dictionaries are just key-value pair. For each 'key' there is
- corresponding 'value'. In lists we have indexes to access elements,
- here we have 'key'.
+ corresponding 'value' associated with it. In lists we use indexes
+ to access elements, here we use the 'key'.
- '{}' are used to create Python dictionaries. Lets start by opening
- IPython interpreter. Lets create a dictionary say
+ Lets start by opening IPython interpreter.
+ '{}' are used to create Python dictionaries. Lets create a dictionary say
player = {'Mat': 134,'Inn': 233,
'Runs': 10823, 'Avg': 52.53}
- Its dictionary storing statistics of a cricket player.
- Now to get the 'average' of this player we have to simply write
+ Its a dictionary storing statistics of a cricket player.
+ Here 'Mat', 'Inn' etc are the keys. Now in order to get the 'average' of
+ this player we simply type
print player['Avg']
52.53
- To add a new key-value to this dictionary we have to something like
+ To add a new key-value pair to this dictionary we type
player['Name'] = 'Rahul Dravid'
- print player
- Please remember that Python dictionaries dont maintain the order
- in which the key-value pair are stored it might change as we add new
- entries.
+ print player
+ Please note that Python dictionaries don't maintain the order
+ in which the key-value pairs are stored. The order might change
+ as we add new entries.
In dictionaries Duplicate keys are overwritten, that is when we do
player['Mat'] = 139
It wont create a new entry, rather it will simply overwrite previous
- value with new one. So
+ value with the new one. So
print player
will have updated value
- As we covered in one of previous sessions to iterate through lists
- we use 'for'. In case of dictionaries we can iterate over them using
- 'keys' for example
+ As we covered in one of previous sessions 'for' can be used to iterate
+ through lists. The same is possible in case of dictionaries too. We can
+ iterate over them using the 'keys', for example:
for key in player:
print key, player[key]
- We saw how containership works in lists, there we can check if a
- value is present in list or not, in case of Dictionaries it works
- only for keys. so
+ This prints the keys in the dictionary along with their corresponding
+ values. Notice that the order is not the same as we entered it.
+
+ We saw how containership works in lists. There we can check if a
+ value is present in a list or not but in case of Dictionaries we
+ can only check for the containership of the keys. so
'Inn' in player
returns True
'Econ' in Player
returns False as there is no such 'key'
- If you try to look or search 'value' it wont work.
- Dictionaries supports some functions to retrieve keys and values
- like
+ If you try to look or search for a 'value' it will not work.
+ Dictionaries support functions to retrieve keys and values
+ such as
player.keys()
returns the list of all 'keys'
player.values()
return list of all 'values'
- Next we shall look at 'sets'. Sets in Python are unordered
- collection of unique elements. This data structure comes handy in
+ Now we shall move on to 'sets'. Sets in Python are an unordered
+ collection of unique elements. This data structure comes in handy in
situations while removing duplicates from a sequence, and computing
standard math operations on sets such as intersection, union,
difference, and symmetric difference.
Lets start by creating a set
f10 = set([1,2,3,5,8])
- f10 is set of Fibonacci numbers less then 10
- lets print value of f10
+ And thats how a set is created.
+ f10 is the set of Fibonacci numbers less than 10
+ lets print the value of f10
print f10
As we mentioned earlier, these are unordered structure so order of
- elements are not maintained, and output order is different than
- input order. Lets create one more set of all prime numbers less than
- 10
+ elements is not maintained, and output order is different than
+ input order, just as in dictionaries. Lets create one more set, a set of
+ all prime numbers less than 10
p10 = set([2,3,5,7])
print p10.
- To get union of these two sets we use '|' operator
+ To get union of these two sets we use the or '|' operator
f10 | p10
- For intersection & operator is used:
+ For intersection we use the and '&' operator:
f10 & p10
- f10 - p10 gives difference between f10 and p10, which is, elements
- present in f10 but not present in p10.
- ^ operator gives us symmetric difference that is p10 union f10 minus
- f10 intersection p10
+ f10 - p10 gives difference between f10 and p10, that is, the set of all elements
+ present in f10 but not in p10.
+ The carat '^' operator gives us the symmetric difference of 2 sets. That is
+ f10 union p10 minus f10 intersection p10
f10 ^ p10
- To check if a set is super set or subset greater than and lesser than
- operators can be used
+ To check if a set is the super set or a subset of another set, the greater than
+ and the lesser than operators are used
set([2,3]) < p10
returns True as p10 is superset of given set
- Similar to lists and dictionaries sets also supports containership so
+ Similar to lists and dictionaries, sets also supports containership so
2 in p10
returns True as 2 is part of set p10 and
4 in p10
returns False.
- len function works with sets also:
- len(f10) returns the length, that is 5
- We can also use 'for' loops to iterate through a set.
+ The 'len' function works with sets also:
+ len(f10) returns the length, which is 5 in this case.
+ We can also use 'for' loops to iterate through a set just as with dictionaries and lists.
With this we come to the end of this tutorial on Dictionaries and
- sets. We have covered some of properties of both data types and
- functions supported by them. Thank you.
+ sets. We have seen how to initialize dictionaries, how to index them using keys
+ and a few functions supported by dictionaries. We then saw how to initialize
+ sets, perform various set operations and a few functions supported
+ by sets. Hope you have enjoyed it, Thank you.
*** Notes