Introduction
Hi guyz today I am going to writing a tutorial on dictionaries in Python....As per as getting comments from my readers I'll try to make this tutorial short....
Layout
1.What are dictionaries?
2.Why dictionaries?
3.How to declare/make dictionaries in Python?
4.What all you can do with a dictionary?
5.Interesting Functions?
6.Dictionary Methods?
What are dictionaries?
Dictionaries are simply another type of Sequence in Python..
Like in English dictionaries you search for a word and its meaning … Same happens in python...
Dictionaries in Python have a “key” and a “value of that key”... That may seem strange for the first time but I promise I'll make it easy in the next few topics...
Why Dictionaries?
A common question which comes into ones mind is that why we dictionaries in python..
The answer is pretty simply “When indexing and slicing doesn't do” ….If you are not familiar with that I suggest you to read my previous tutorials on this topic...
Now , That we have a simple picture of these dictionaries ...Let's know how to use them...
How to: Dictionaries in Python?
Declaring dictionaries in python is pretty damn! Simple... I found them easier than any of other sequences I have learn t about..
To declare a dictionary in python We enclose them in curly brasses “{“ and “}”. And rest I'll explain after showing you this piece of code....
dic = { "Aneesh" : 15, "Sonal" : 40 }
print dic
Output:-
{'Aneesh': 15, 'Sonal': 40}
Yupi , we just declared a dictionary....That wasn't difficult...Was that...
Explanation:
As I told you above that a Dictionaries in Python have a “key” and a “value of that key” Here above we declared 2 keys and two values...
1.Aneesh with a value of 15
2.Sonal with a value of 40
In python we separate a key and its value by a colon [“:”]...
That's pretty simple....
What you can do With a dictionary?
To demonstrate what we can do with a simple dictionary...Lets build a interesting code...
dic = { "Aneesh" : "Programmer" , "Sonal" : "Teacher" , "Sachin" : "Buisnessman" }
print dic["Aneesh"] # It'll print out this keys value...In this case Programmer.... Basic syntax :- d[k]
dic["Aneesh"] = "Writter" # Assigns the value "Writter to the key "Aneesh" in the dictionary "dict" .....Basic syntax:- dictionary[key] = value
String Formatting with Dictionaries:
For this you should know what is string formatting in Python.
dic = { "Aneesh" : "Programmer" , "Sonal" : "Teacher" , "Sachin" : "Buisnessman" }
print "Hey do you know Aneesh is a %(Aneesh)s and Sonal is a %(Sonal)s " % dic
Output:
Hey do you know Aneesh is a Programmer and Sonal is a Teacher
Explanation:
Pay close attention to the last line of the Code... … when we write “%(Aneesh)s” % dic
the code says to that Search for this key in this dictionary and print it out to me....We use a 's' at the end because we want Python to know that its a String...
INTERESTING FUNCTIONS:
The dict function:
We can use this dict function to construct a dictionary from other mappings eg:- other dictionaries or from sequences of key,value pairs...
tup = ( ('key','value') , ('key2',