0% found this document useful (0 votes)
7 views23 pages

Tuple and Dictionaries [Part - 02]

Dictionaries in Python store data as key:value pairs and are ordered, changeable, and do not allow duplicates. They can be created, accessed, updated, and traversed using various methods and syntax. Common functions include len(), get(), items(), keys(), values(), and copy() for managing dictionary contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

Tuple and Dictionaries [Part - 02]

Dictionaries in Python store data as key:value pairs and are ordered, changeable, and do not allow duplicates. They can be created, accessed, updated, and traversed using various methods and syntax. Common functions include len(), get(), items(), keys(), values(), and copy() for managing dictionary contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Dictionary in Python -

Dictionaries are used to store data values in key:value pairs. A


-
dictionary is a collection which is ordered, changeable and do not
- -

allow duplicates.
-
Creating a Dictionary

Syntax is:

<dictiionary_name>={‘key1’:’value1’,’key2’:’value2’,....’keyn’:’valuen’}
-

Eg: ---
d={‘Rollno’:’2’,’name’:’Anu’,’age’:’17’}
-
- -

-
-
Accessing Elements in a Dictionary

Syntax is: -
Tup = (25 30 95)
, ,

dictionary_name[key]
-
↑ upO] = 25

Eg: TYPID = 30

dict1={‘Name’:’Riya’,’Age’:’7’,’class’:’second’}
=

print(dict1[‘Name’]) dist;
for i in

Ct1EName] = Rigs
di Pant (i dizid
,

dict1['Age) : T Name Righ


dict1[class] = Second Age 7
Traversing a dictionary -

in operator using for loop :


-

=
Eg: d={1:’one’,2:’two’,3:’three’,4:’four’} ⑪
-for i in d:
print(i,’:’,d[i])
Eg:
1: one -
2: two -
3: three
4: four
Appending values to a dictionary -
-
..
()
O
"

Syntax:
Dictionary_name[key]=value E
-
--
- 93
Eg: -
- -
d1={‘mon’:’monday’,’tue’:’tuesday’,’wed’:’wednesday’}
- -
-

d1[‘thu’]=’thursday’ L
--

print(d1)
{‘mon’:‘monday’,’tue’:’tuesday’,’wed’:’wednesday’,’thu’:’thursday’}
-
Updating Elements in a Dictionary -

Syntax:
-

<Dictionary>[key]=<value>
-

-
Eg: D={‘Teena’:18,’Riya’:12,’Alya’:22,’Ravi’:25}
-
-

D[‘Riya’]=28 u
-

print(D)
-

yeanc : 18
,
Riya : 28 Alta : 22
,
S
Ravi : 25
Student =
23 -

= home =
input ("Enter name")
("Enter mask"))
Int (input
mark =
Agel))
int Cinput
("Enter
Age =

'Anil'
Student Encms]
-

25
Student [mo]
=

15
Student [Aga]
=

: And mark : 25 Age : 153


Student :
Game ,
,
Common Dictionary Functions And Methods

-len() : This method returns number of key-value pairs in the


-
given dictionary .
-

Syntax :
len(dictionary_name)
-
-

- -- &

Eg: d1={1:10,2:30,3:30,5:40,6:60}
-----

len(d1) -
>>>E 5
get() method -

The get method returns a value for the given key . if key is not
-
available , then returns default value None .
-

Syntax: ~
dict.get(key,default=None)
'Bull', 3 : Cil]
El : 'An ; 2 :
T =
-

dist get (2)


Binil
D1={‘sun’:’sunday’,’mon’:’monday’,’tue’:’tuesday’,’wed’:’wednesday’
- - - -

,’thu’:’Thursday’}
-

D1.get(‘wed’)
-

>>>wednesday -

D1.get(‘fri’) -

6
>>>None

E
Items() -

It returns content of a dictionary as a list of tuples having key-value


-

pairs .
-

Syntax :
D.items()
Eg:
D={‘sun’:’sunday’,’mon’:’monday’,’tue’:’tuesday’,’wed’:’wednesday’,
c -

’thu’:’Thursday’}
D.items() (the 'nesday
C'mon Monday)
-

Sunday B , ,
, ,
D G2(sun , ,
Wednesday's33
=

Cwed' ,
keys() -

It returns a list of keys from key-value pairs in a dictionary .

Syntax:
D.keys()

D={‘sun’:’sunday’,’mon’:’monday’,’tue’:’tuesday’,’wed’:’wednesday’,
’thu’:’thursday’}
D.keys()

D = 2 sun, mon
,
te
,
wed
,
thu
?
values() -

It returns a list of values from key-value pairs in a dictionary .


-

Syntax :

D.values()
D={‘sun’:’Sunday’,’mon’:’Monday’,’tue’:’tuesday’,’wed’:’wednesday’,
-
- -

‘thu’:’thursday’}
-

D.values()
-
copy()

We cannot copy a dictionary by using assignment ('=') operator as it


-
will create a reference to the same dictionary variable and
modifications will also be reflected in both dictionaries. We can use
built-in function copy() to create a new dictionary and modifications
done on base dictionary will not be reflected.
Vilmsck-

Exa
; 2 :
Af
L

point (v)

You might also like