Hello guys!
This is the 12th part of the article series about Python. In this article series you will learn Python step-by-step and easily.
Getting Theme
For getting the theme of Python, kindly go through my previous articles:
Diving Into Python : Chapter 1
Diving Into Python : Chapter 2
Diving Into Python : Chapter 3
Diving Into Python : Chapter 4
Diving Into Python : Chapter 5
Diving Into Python : Chapter 6
Diving Into Python : Chapter 7
Diving Into Python : Chapter 8
Diving Into Python : Chapter 9
Diving Into Python : Chapter 10
Diving Into Python : Chapter 11
Dictionary
Dictionaries are like containers that can be used for holding a value or set of values (elements). The concept of a dictionary in Python is the same as the concept of a hash in several other programming languages.
The entire concept of dictionary rotates around the following two essential things:
The Keys are for accessing a specific value at an address and the Values are the stuff that is at that address. This might sound a bit confusing but there is no need to worry, one example will smash it all.
Example
- EmpDetails = {‘Mr.X’ : ‘Dev’ , \
- ‘Mr. Y’ : ‘DB Admin’, \
- ‘Mr. Z’ : ‘UI Designer’, \ }
Explanation: Consider the preceding example here:
The preceding description clearly states the what the keys are, the values and dictionary name in the example shown above.
I hope now you guys are a bit clear.
Creating a DictionaryA simple dictionary can be created in the following two ways.
Using Multiline statementsI already discussed about multiline statements in my previous article, so for a better understanding I suggest you guys go through the previous parts.
Example
- EmpDetails = {‘Mr.X’ : ‘Dev’ , \
- ‘Mr. Y’ : ‘DB Admin’, \
- ‘Mr. Z’ : ‘UI Designer’, \ }
Using "{}" brackets
I already explained that these types of statements use braces in my previous article, so for a better understanding I suggest you guys go through the previous parts.
Example
- EmpDetails = {‘Mr.X’ : ‘Dev’ , ‘Mr. Y’ : ‘DB Admin’, ‘Mr. Z’ : ‘UI Designer’ };
Accessing Values in Dictionary: For accessing elements in the dictionary, you don't need to do any herculean task. Just go through the approach that we used to do in Lists / Tuples in Python.
We need to use one and only those square brackets "[]" along with the key or index. Let's have a look.
Example
# Accessing Dictionary
- dict = {'Programmer': 'Business Layer', 'DataBase Admin': 'Database Layer', 'UI Developer': 'Presentation Layer'};
- print ("dict['Programmer']: ", dict['Programmer'])
- print ("dict['UI Developer']: ", dict['UI Developer'])
Output
Operations | Dictionary
Dictionary | Updating: You can do updates in the dictionary in one of the following ways:
- Adding a new element
- Updating a new key value
- Modifying an existing entry
- Deleting an existing entry
Let's explore it with an example.
Example
# Updating Dictionary
- dict = {'Programmer': 'Business Layer', 'DataBase Admin': 'Database Layer', 'UI Developer': 'Presentation Layer'};
# Updating a New Entry
- dict['Programmer'] = 'Architecture Design';
# Showing New Entry
- print ("dict['Programmer']: ", dict['Programmer'])
OutputDictionary | Deletes: In Python you either delete a single element or all the dictionary elements. To do delete in a dictionary, we use a del statement.
Example
# Deletion in Dictionary
- dict = {'Programmer': 'Business Layer', 'DataBase Admin': 'Database Layer', 'UI Developer': 'Presentation Layer'};
- del dict['UI Developer'];
- dict.clear(); # remove all entries in dict
- del dict ; # delete entire dictionary
- print ("dict['Programmer']: ", dict['programmer'])
- print ("dict['Database Admin']: ", dict['Database Admin'])
Output
Explanation
As you can see, after deletion there will be no elements in the dictionary. That's why it's giving an error.
Built-in Tuple functionsHere are some built-in tuple functions.
Dictionary | CompareThis built-in function is used for comparing two dictionaries. It compares them in terms of their elements.
Example
- dict1 = ('CSK', 180)
- dict2 = ('MI', 175)
- print (cmp(dict1, dict2))
- print (cmp(dict2, dict1))
Output-1 , 1
Dictionary | LengthAs the name of this built-in function suggests, it is used for getting the length of any tuple value from a dictionary.
Example
- tup = len ('C# Corner')
- print (a)
Output: 9Guidelines from my side
- Do as much as code you can.
- Code anything you want, the best way to learn.
- Don't just study things, try to learn them.
- Work on your Concepts.
- Work on the fundamentals of any technology or stuff you want to learn
Moto
“Keep calm and code Python”.
I tried to make this an interesting and interactive article and wish you guys going to like that, meanwhile if you have any suggestions then your welcome.
Until the next part, keep sharing!