Comprehensive notes Dictionaries in Python for class 11

In this article we are going to start a new topic dictionaries in python for class 11. As you know python supports different ways to handle data with collections such as list, tuple, set etc. Dictionary is also one of the collection based type. So here we start!

Comprehensive notes Dictionaries in Python for class 11

Comprehensive notes Dictionaries in Python for class 11 starts with the introduction to dictionary. So let’s think about the dictionary.

As we have used the English dictionary. It contains different words with its meanings. In python, the dictionary refers to the mapped items using key-value pairs. It is a versatile type of python.

A dictionary item doesn’t contain any index just like list or tuple. Every element in dictionary is mapped with key-value pair. The dictionary element can be referred to a key with the associated value.

What is a dictionary?

Dictionaries are mutable, unordered collections with elements in the form of a key:value pairs the associate keys to value. – Textbook Computer Science with Python, Sumita Arora

In the dictionary, a key is separated from values using colon (:) and the values with commas.

In the next section of Comprehensive notes Dictionaries in Python for class 11, let’s discuss about how to create dictionary.

Creating a Dictionary

To create a dictionary follow this syntax:

<dictionary-object> = {<key>:<value>,<key:value>,.....}

According to the syntax:

  1. <dictionary-object>: It is just like a variable, referred as dictionary object
  2. =: As usual, it is assignment operator
  3. {}: The curly braces are used to write key-value pairs inside
  4. key:value pair: The items or elements of dictionary object will be written in this way

Points to remember:

While creating a dictionary always remember these points:

  1. Each value is separated by a colon
  2. The keys provided in the dictionary are unique
  3. The keys can be of any type such as integer, text, tuple with immutable entries
  4. The values can be repeated
  5. The values also can be of any type

Observe the below given examples:

d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}

d = {1:'Sharda',2:'Champa',3:'Babita',4:'Pushpa',5:'Chandirka',6:'Meena'}

d = {1:100,2:200,3:300,4:400}

Now understand the key-value pairs:

key-value pairKeyValue
‘Mines’:’Rajesh Thakare’
1:’Sharda’
1:100
Mines
1
1
Rajesh Thakare
Sharda
100
‘HR’:’Dinesh Lohana’
2:’Champa’
2:200
HR
2
2
Dinesh Lohana
Champa
200
‘TPP’: ‘Kamlesh Verma’
3:’Babita’
3:300
‘TPP’
3
3
‘Kamlesh Verma’
‘Babita’
300

The next topic for Comprehensive notes Dictionaries in Python for class 11 is creating a dictionary. So let’s discuss it with examples.

Creating an empty dictionary

d = {}

As simple as that we have created an empty lists, empty tuple, you can also create an empty dictionary and manipulate it afterwards.

Dictionaries are also known as associative arrays or mappings or hashes.

After creating a dictionary in comprehensive notes Dictionaries in Python for class 11, now learn how to access the elements of a dictionary using various methods.

Accessing elements of the dictionary

Actually dictionaries are indexed based on their keys. So whenever you want to access the values of it keys are used to access them. Observe the following:

d = {1:'Virat Kohli',2:'Ajinkya Rehane',3:'Subhman Gill'}

#Priting with keys
print(d[1],d[3])

#Printing all values
print(d)

The process of taking a key and finding a value from the dictionary is known as lookup. Moreover, you cannot a access any element without key. If you try to access a value with key doesn’t exist in the dictionary, will raise an error.

Now have a look at the following code:

d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}
for i in d:
   print(i, ":",d[i])

In the above code you can see the variable i prints the key and d[i] prints the associated value to the key.

d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}
for i,j in d.items():
   print(i, ":",j)

Here, I have separated the values using two variables in the loop.

You can also access the keys and values using d.keys() and d. values() respectively. It will return the keys, and values in the form of sequence. Observe this code and check the output yourself:

d = {'Mines':'Rajesh Thakare','HR':'Dinesh Lohana','TPP':'Kamlesh Verma','School':'A. Vijayan','Hospital':'Shailendra Yadav'}
print(d.keys())
print(d.values())

Now let’s getting familiar with common operations can be performed with a dictionary for comprehensive notes Dictionaries in Python for class 11.

Create dictionary using dict() function

A dict() function can be used to create a dictionary and initialize the elements as key:value pair. For example,

pr = dict(name='Ujjwal',age=32,salary=25000,city='Ahmedabad')
print(pr)

You can also specify the key:pair value in following manners:

pr = dict({'name':'Ujjwal','age':32,'salary':25000,'city':'Ahmedabad'})
print(pr)

You can also specify the key-values in the form sequences (nested list) as well. Observe the following code:

pr = dict([['name','Ujjwal'],['age',32],['salary',25000],['city','Ahmedabad']])
print(pr)

In the next section of comprehensive notes Dictionaries in Python for class 11, lets discuss about how to add elements to a dictionary.

Add elements to a dictionary

You can add an element to the dictionary with the unique key which is not already exist in the dictionary. Look at the following code:

pr = dict([['name','Ujjwal'],['age',32],['salary',25000],['city','Ahmedabad']])
pr['dept']='school'
print(pr)

Update an element in a dictionary

To update the element, use a specific key and assign the new value for the dictionary. Observe this code:

d = dict({'name':'Ujjwal','age':32,'salary':25000,'city':'Ahmedabad'})
d['salary']=30000
print(d)

Now in the next section of comprehensive notes Dictionaries in Python for class 11, you will learn how to delete elements from the dictionary.

Deleting element from the dictionary

You can delete the elements by using del, pop() and popitem() function. The pop() function we will discuss in another article. Observe the following code for del:

d = dict({'name':'Shyam','age':32,'salary':25000,'city':'Ahmedabad'})
del d['age']

Membership Operator

As we you know in and not in operator we have used with list and tuple, similarly used with dictionary. If key is present in the dictionary, it will return true otherwise false. Look at the code:

d = dict({'name':'Shyam','age':32,'salary':25000,'city':'Ahmedabad'})
if 'name' in d:
   print("Key found")
else:
   print("Key not found")

if 'Shyam' in d.values():
   print("Value found")
else:
   print("Value not found")

So I hope you are now familiar with the concept dictionary in python after reading this article Dictionaries in Python for class 11. If you have doubt or query regarding the article, feel free to ask in the comment section.

Share your feedback and views in the comment section. Hit the like button and share the article with your friends.

View Complete Course

Thank you for reading this article.

Comment Your Views

%d bloggers like this: