Professional Documents
Culture Documents
Dictionaries
Dictionaries
com
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
<dictionary-object> = {<key>:<value>,<key:value>,.....}
1
Downloaded from www.tutorialaicsip.com
Points to remember:
d = {1:'Sharda',2:'Champa',3:'Babita',4:'Pushpa',5:'Chandirka',6:'Meena'}
d = {1:100,2:200,3:300,4:400}
The next topic for Comprehensive notes Dictionaries in Python for class 11 is creating a
dictionary. So let's discuss it with examples.
d = {}
As simple as that we have created an empty lists, empty tuple, you can also create an empty
dictionary and manipulate it afterwards.
2
Downloaded from www.tutorialaicsip.com
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.
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:
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.
In the above code you can see the variable i prints the key and d[i] prints the associated value
to the key.
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:
Now let's getting familiar with common operations can be performed with a dictionary for
comprehensive notes Dictionaries in Python for class 11.
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)
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.
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)
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)
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.