0% found this document useful (0 votes)
111 views19 pages

Class 11 Ip Dictionary All Functions PDF

The document provides an overview of various Python dictionary methods, including their descriptions and examples of usage. Key methods discussed include clear(), copy(), fromkeys(), get(), items(), keys(), pop(), setdefault(), update(), and values(). It also covers deleting elements from a dictionary using pop(), popitem(), clear(), and del statement.

Uploaded by

bhanwarkunwar79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views19 pages

Class 11 Ip Dictionary All Functions PDF

The document provides an overview of various Python dictionary methods, including their descriptions and examples of usage. Key methods discussed include clear(), copy(), fromkeys(), get(), items(), keys(), pop(), setdefault(), update(), and values(). It also covers deleting elements from a dictionary using pop(), popitem(), clear(), and del statement.

Uploaded by

bhanwarkunwar79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

functions and

methods

Prepared by :Rohit chouhan


Python dictionary methods
Method Description
clear() Removes all the elements from the dictionary

Copy() Returns a copy of the dictionary


fromkeys() Returns a dictionary with the specified keys
and value
get() Returns the value of the specified key
items() Returns a list containing a tuple for each key
value pair
keys() Returns a list containing the dictionary's keys

Pop() Removes the element with the specified key

pop.item() Removes the last inserted key-value pair

setdefault() Returns the value of the specified key. If the


key does not exist: insert the key, with the
specified value
update() Updates the dictionary with the specified key-
value pairs
values() Returns a list of all the values in the dictionary

Prepared by: Rohit Chouhan


len () It returns the number of
items (length) in the
dictionary.
sorted () It returns the sorted list of
keys.

Prepared by: Rohit Chouhan


Len()
This function returns the number of items(length) of the
dictionary.

 Dict={‘mohan’:95,’ram’:89,’susheel’:92,’sangeeta’:85}
 print(len(dict))

Output:
4

Prepared by: Rohit Chouhan


Keys()
Returns a list containing the dictionary's keys.

 Dict={‘mohan’:95,’ram’:89,’susheel’:92,’sangeeta’:85}
 print(dict.keys)

Output:
dict_keys(['mohan', 'ram', ‘susheel',
'sangeeta'])

Prepared by: Rohit Chouhan


Values()
Returns a list of all the values in the dictionary.

 Dict={‘mohan’:95,’ram’:89,’susheel’:92,’sangeeta’:85}
 print(dict.values)

Output:
Dict_values([55,89,92,85])

Prepared by: Rohit


Chouhan
Items()
Returns a list containing a tuple for each key value pair.

 Dict={‘mohan’:95,’ram’:89,’susheel’:92,’sangeeta’:85}
 print(dict.items())

Output:
Dict_items([(‘mohan’,95),(‘ram’,89),(‘susheel’,92),(‘sangeeta’,85)])

Prepared by: Rohit


Chouhan
get()
Returns the value of the specific key.
 Dict={1:’1’,2:’22’,3:’333’,4:’4444’,5:’55555’}
 print(Dict.get(2))

Output:
‘22’

Prepared by: Rohit


Chouhan
Update()
updates the dictionary with the specific key-
value pairs.

 Emp1={‘name’:’john’,’salary’:10000,’age’:24}
 Emp2={‘name’:’divya’,’salary’:54000,’dept’:’sales’}
 Emp1.update(Emp2)
 print(Emp1)

Output:
{‘name’:’divya’,’salary’:54000,’age’:24,’dept’:’sal
es’}

Prepared by: Rohit


Chouhan
Fromkeys()
This method is used to create a new dictionary from
a sequence containing all the keys and a common
values, which will be assigned to all the keys.
Syntax:dict.fromkeys(<key sequence>,([<value>])

 Dict1 = dict.fromkeys([2, 4, 6, 8], 110)


 print(Dict1)

Ouput:
{2: 110, 4: 110, 6: 110, 8:
110}

Prepared by: Rohit Chouhan


Setdefault()
If the GIVEN KEY IS ALREADY PRESENT in the
dictionary the existing value of that key will not be
updated, but the current value associated to the
specified key is returned. Setdefault()
 Marks={1:350,2:423,3:411,4:300}
 print(Marks.setdefault(2,220))

Output:
423
 print(Marks)

Output:
{1:350,2:423,3:411,4:300}

Prepared by: Rohit Chouhan


Setdefault()
if the GIVEN KEY IS NOT ALREADY PRESENT in the
dictionary, it will add the specified new key: value pair
to the dictionary and return the value of the added
key.
 Marks={1:350,2:423,3:411,4:300}
 print(Marks.setdefault(5,320))
 print(Marks)

Output:
{1: 350, 2: 423, 3: 411, 4: 300, 5:
320}

Prepared by: Rohit Chouhan


Setdefault()
If you only provide a key without any value and the
key doesn’t exist in the dictionary , it will take none
as its value.
 Marks={1:350,2:423,3:411,4:300}
 Marks.setdefault(5)
 print(Marks)

Output:
{1:350,2:423,3:411,4:300,5:none}

Prepared by: Rohit Chouhan


Deleting elements from dictionary

pop()
Removes the element with the specified key.

 Dict={0:’Amaan’,1:’Tushar’,2:’swati’,3:’mridul’}
 print(Dict.pop(3))

Output:
{0:’Amaan’,1:’Tushar’,2:’Swat
i’}

Prepared by: Rohit Chouhan


Popitem()
Removes the last key: value
 Car={‘brand’:’ford’,’model’:’mustang’,’year’:2019}
 Car.popitem()
 print(car)

Output:
{'brand': 'ford', 'model':
'mustang'}

Prepared by: Rohit Chouhan


Clear()
Removes all items from the dictionary.

 D1={‘name’:’Rohit’,’class’:11,’age’:17,’subject’:’IP’}
 D1.clear()
 print(D1)

Output:
{}

Prepared by: Rohit Chouhan


Del statement:
Del()This statement is used to delete a specific
key-value pairs in the dictionary.

 Car = {'brand': 'ford', 'model': 'mustang', 'year': 2019}


 del Car['model']
 print(Car)

Output:
{'brand': 'ford', 'year':
2019}

Prepared by: Rohit Chouhan


References:-

1. Youtube:-
https://youtu.be/qQUiFHWOeV4?si=kS-Pzx8xLG5-8ywB

2.IIT Bombay:-
https://www.cse.iitb.ac.in/~rbhandari/python-course

3. Sumita Arora:- Computer Science


with Python (Class 11)

Prepared by: Rohit Chouhan


Prepared by: Rohit Chouhan

You might also like