Class 12 : Review Of Python Basics

Home Page class 12 @ Python

Class 12 : Python

Python Basics
Data Types
String
List
Tuple
Dictionary
Flow of Execution
Mutable and Immutable Types
Type Casting

Class 12 : Python Revision MCQs

Revision Tour MCQs

Class 12 | Revision Tour 9

By: Archana Shukla and Rajesh Shukla

Dictionary

Python dictionaries are a collection of some key-value pairs. Dictionaries are mutable, unordered collections with elements in the form of key:value pairs that associate keys to value. Dictionary is enclosed within curly braces {}.

Creating a dictionary

To create a dictionary following syntax is used.

Dictionary name={key1:value1,key2:value2,…….}

Example

Subject={“A”:”Accounts”,”B”:”B-Studies”,”C”:”Chemistry”}

Accessing elements of dictionary

The elements of dictionary are accessed through the keys defined in the key:value pairs.

Syntax: dictionary name[key]

>>> Subject={"A":"Accounts","B":"B-Studies","C":"Chemistry"}
>>> Subject

Output
{“A”:”Accounts”,”B”:”B-Studies”,”C”:”Chemistry”}

>>> Subject[‘B’]

Output
‘B-Studies’

Dictionary Operations

Traversing a dictionary

Traversal means to access and process each element of the dictionary. This can be done through for loop.

Example :

dic1={'m':'maths','s':'science','c':'commerce','e':'english','h':'hindi'}
for key in dic1:
    print(key,":",dic1[key])

Output
m : maths
s : science
c : commerce
e : english
h : hindi

Adding elements to dictionary

We can add new elements to the dictionary. But the key should not exist in the dictionary and must be unique. If the key already exists, then the value of existing key will be changed and no new entry will be added to the dictionary.

Syntax : dictionaryname[key]=value

Example 1: 

dic1={'Teena':18,'Riya':12,'Aliya':13,'Priya':17}
print(dic1)
dic1['Ravi']=20
print(dic1)

output
{‘Teena’: 18, ‘Riya’: 12, ‘Aliya’: 13, ‘Priya’: 17}
{‘Teena’: 18, ‘Riya’: 12, ‘Aliya’: 13, ‘Priya’: 17, ‘Ravi’: 20}

Example 2: if the key already exist.

dic1={'Teena':18,'Riya':12,'Aliya':13,'Priya':17}
print(dic1)
dic1['Riya']=22
print(dic1)

output:
{‘Teena’: 18, ‘Riya’: 12, ‘Aliya’: 13, ‘Priya’: 17}
{‘Teena’: 18, ‘Riya’: 22, ‘Aliya’: 13, ‘Priya’: 17}

Deleting elements from the dictionary

There are two methods for deleting elements from the dictionary:

del

We can use del command to delete a dictionary entry :

syntax: del <dictionary>[key]

dic1={'Teena':18,'Riya':12,'Aliya':13,'Priya':17}
del dic1['Priya']
print(dic1)

output :
{‘Teena’: 18, ‘Riya’: 12, ‘Aliya’: 13}

pop()

This method is also used for deleting elements from the dictionary.

Syntax: <dictionary>.pop(key)

Example :

dic1={'Teena':18,'Riya':12,'Aliya':13,'Priya':17}
dic1.pop('Aliya')
print(dic1)

output:
{‘Teena’: 18, ‘Riya’: 12, ‘Priya’: 17}

Checking for existence of the key

Membership operators ‘in’ and ‘not in’ are used to check the existence of the keys in the dictionary. These operators will return true or false on the basis of the absence or presence of the key in the dictionary.

Syntax: <key> in <dictionary> / <key> not in <dictionary>

Example :

dic1={'Teena':18,'Riya':12,'Aliya':13,'Priya':17}
print('Teena' in dic1)

output: True

dic1={'Teena':18,'Riya':12,'Aliya':13,'Priya':17}
print('Riya' not in dic1)

output : False

Dictionary Functions

FunctionDescriptionExample
Len(dict)Returns the total number of items present in the dictionary>>> dic1={‘Teena’:18,’Riya’:12,’Aliya’:13,’Priya’:17}
>>> print(len(dic1))
4
Str(dict)Produces a printable string representation of a dictionary>>> dic1={‘Teena’:18,’Riya’:12,’Aliya’:13,’Priya’:17}
>>> str(dic1)
“{‘Teena’: 18, ‘Riya’: 12, ‘Aliya’: 13, ‘Priya’: 17}”
Type(variable)Returns the type of variable passed as an argument

Dictionary Methods

MethodsDescriptionExample
clear()Removes all the elements of the dictionary
get()Returns the item with the specified key>>> emp={‘name’:’Amit’,’class’:’XII’,’rollno’:1,’sub’:’computer science’}
>>> emp.get(‘name’)
‘Amit’
items()Returns the list of (key,value) tuple pairs from the dictionary>>> emp.items()
dict_items([(‘name’, ‘Amit’), (‘class’, ‘XII’), (‘rollno’, 1), (‘sub’, ‘computer science’)])
keys()Returns the list of keys from the dictionary>>> emp.keys()
dict_keys([‘name’, ‘class’, ‘rollno’, ‘sub’])
values()Returns the list of values>>> emp.values()
dict_values([‘Amit’, ‘XII’, 1, ‘computer science’])
update()Adds the key:value pairs of one dictionary to otherdic1={‘Teena’:18,’Riya’:12,’Aliya’:13,’Priya’:17}
dic2={‘amit’:20,’Ali’:22,’Riya’:20}
dic1.update(dic2)
print(dic1)