/**/ Python & MySQL: Dictionaries - Class - XI

Monday, December 13, 2021

Dictionaries - Class - XI

Informatics Practices

Dictionaries

Class - XI

=========================


Introduction: 

It is an unordered collection of items where each item consist of a key and a value. It is mutable that mean we can modify its conetens but key must be unique and immutable.

Creating A Dictionary

It is enclosed in curly braces {} and each item is  separated from other item by a comma(,). Within each  item, key and value are separated by a colon (:).

Example

d1 = {‘Subject': ‘Informatic Practices', 'Class': ‘11'}

Accessing List Item

d1 = {'Subject': 'Informatics Practices', 'Class': 11}

print(d1)

print ("Subject : ", d1['Subject'])

print ("Class : ", d1.get('Class'))

OUTPUT

{'Class': '11', 'Subject': 'Informatics Practices'}

('Subject : ', 'Informatics Practices')  ('Class : ', 11)

Iterating Through A Dictionary

Below example will show how dictionary items can be accessed through loop.

Example:

dict = {'Subject': 'Informatics Practices', 'Class': 11}  for i in dict:

print(dict[i]) 

OUTPUT

11

Informatics Practices

Updating Dictionary Elements

We can change the individual element of dictionary.

Example:

dict = {'Subject': 'Informatics Practices', 'Class': 11}  

dict['Subject']='computer science'

print(dict) 

OUTPUT

{'Class': 11, 'Subject': 'computer science'}

Deleting Dictionary Elements

del, pop() and clear() statement are used to remove elements from  the dictionary.

Example

dict = {'Subject': 'Informatics Practices', 'Class': 11}  print('before del', dict)

del dict['Class'] # delete single element  print('after item delete', dict)

del dict          #delete whole dictionary

print('after dictionary delete', dict)

Output

('before del', {'Class': 11, 'Subject': 'Informatics Practices'})  

('after item delete', {'Subject': 'Informatics Practices'})  

('after dictionary delete', <type 'dict'>)

 

POP()

pop() method is used to remove a particular item in a dictionary. clear()

method is used to remove all elements from the dictionary.

Example

dict = {'Subject': 'Informatics Practices', 'Class': 11}  print('before del', dict)

dict.pop('Class')

print('after item delete', dict)  dict.clear()

print('after clear', dict)

Output

('before del', {'Class': 11, 'Subject': 'Informatics Practices'})  

('after item delete', {'Subject': 'Informatics Practices'})  

('after clear', {})

Built-in Dictionary Functions

Built-in Dictionary Methods



Assignment Question:

1. WAP to create a phone dictionary for all your friends and print each key value pair in separate lines.

Sol:

D = {“Ramesh”:254846, “Vinod”: 854759, “Amit”: 548742, “Grumeet”: 654852, “Manali”: 958547, “Anjali”:695896}

for name in D:

            print(name, “:” D[name])


Output:

Ramesh         :           254846

Vinod            :           854759

Amit              :           548742

Gurmeet        :           654852

Manali           :           958547

Anjali            :           695896



==========================








No comments:

Post a Comment

Please do not any spam in the comment box.

CLASS XI HALF YEARLY QP WTH MS 2024

  Half Yearly Examination: 2024-25 Informatics Practices (065) Class- XI       Time Allowed: 3hrs                                     ...