Adobe Scan Dec 04, 2024
Adobe Scan Dec 04, 2024
_
Ill - ■
,~r I hecks whether Pll'tladar i., 11 Ch •-•• A
~t) i
h'""'"' appears In the dlcttan■"' ..... llll ilkth.wll-, It
• 1, Othtrw11e _..._
..-ni .,,- If
,, , 1d y", "tue" • "t ,.....-nl PIIN
,, : . u sday", "w d": "-.a"'wu day", " h " :
' nA
, >n A
' not in A
•l
~a~
17 cor.1MDN DICTIONARY f UNCTIONS AND METHODS
,, vides you w• th a number of ways to manipulate the dictionary. Let's check out some
0
.holl. pr rtant ones w1·th examp l es.
·w
,the 1n1P 0
--~~Python Dict ionary Methods
copy()
•· · •.....
- · ·• ~ .- - ·. ,
...
C __. _ . i'
/
C
update()
c-- - - ~ · - - •
iterosO
values()
[ES( t pop1tem()
len()
'Ills method returns number of key-value pairs in the given dictionary.
lyntax:
len (d) # d is dictionary
returns number of key-value pairs in the dictionary.
6 6
)»dl-={l : 10, 2: 30, 3: 30, 5: 4 o, : 0}
9 27
( ' ',
( I W ',
I< 'f ()
. ..., ( [ I su n I , Imon ' , 'tue', ,
l- re•" 5
,,,, 1ues ()
0
' , ) D.,aJues ( [ ( ' sunday ', 'Monday', 'Tueoda
let,....
,opy()
;.cannot coPY a dictionary by usin
., saine dicti~nary v~nabl
dJI use built-in function
4JctionarY will not be re
pr.as:
new oict old.C
, 'April': 30}
t. ))
'
•
a.a c:.11
. plementatlon-1
, 0 ,a11111
,rarogram to enter names
~aP tPtOQ alll to creAte
int (input (
- 1
y.- - diet
c:owat () ,
<- _
- input (
s•lUJ • at l
llll)le>y.-(ua) •
1
• pY
I •l'OllM'
c•strea',
1110
m thod return the value of die
rt the key with the speclftecl ...~...
s,ata•:
etdrfaultl) lunct1on returns
, value of the k£'y, 1f it Is In the dlctl:1111...
, None, 1f key ts not m the dlctlo
• Default valm, 1f key Is not ln
,......~1 •ner
(. tudent , ,.,
(' tudentl ', IO)
ftt I :76)
-rr.,.(D.it-()))
.ftl. . ())
la lO C.O
- - - . . Cl/88er8/preeti/.lppDIU/Loc•l/Prograas
'11:adlattl', 'ltadellt2', 'Student3')
llldlllttl' 10, 'Stlldeat2': 78, 'StudentJ': 76)
, IOJ
an lllfflUlabll -.,.nee va ues wh ch can be of any type and they are indexed by an integer
I tuple ,, .,,.. 11 ,uttins valun separated by a comma Tuples can be created using parenthesesO
Cl'all9 • ~ wfth lnlll 1llment. 1N ft"" comma 1s necessary
...
1NW,idn V11'iou1 aperatun lib + • n not in etc , which can be applied to tuples
• IIIPle,...,. acass.,. 11Ch elefNnt of a tuple This can be done by usmg looping statement tllfler
,rwil11 • with I number of tuple method1 ke len() count() any() tuple(), max(), mini) 111111().
llldll(~-
, ,,.. ttO i,pdltl
~~,;,o"'
, t" ,- r~1d•• v04J
,rt-"°" p
91£ fYPE QU
of'JCfl the bl•"kt
J. f 111111 ,,,ents in a tuplt
(,I Ele
ies are indPtd
(bl ru P
ruples ar
(cl . '.,.
(di · ... ' ...
... .. . function
(el · · · ....
(fl Like lists, d1ctionarles lrt ......~........,
A python d1ct1onary Is • ....... N . . . . .. . .
111
(hl The association of a key and
To create a d1ct1onary,
111
IJ) In key-value pair, each key
(kl . . . .. ..... . ... function
(ll The ..................... 5
(rnl ......................... function will
, state whether the followina stl
2
(a) An element in a dictionary Is
(bl Internally, d1ct1onar1es are I
(cl We can repeat a key 1n
(dl In d1ct1onary, two unique kiwi
le) A tuple can be a key If It
(f) clear() function 1s used to dlllltl
(g) Atuple 1s a mutable datl lWlwJ{t.1111
(h) Lists can be used as keys In 1
(t) Updating an element 1ft a
Ul We can create immutlllll
(kl A tuple can store elllllllllllll
(I) Atuple cannot stoft _.._
(m) Creat1n1 ,ndivldull wllllll
(n) We cannot dellta 1
,,, p
pt' I'\
tJrtt• • 5uternent to
jO, .. t ,10, 20, ,4
,,,
, ; ... t t+ (60,)
.,,,
,S. ,,..ttt different ways of II
s,10,1s,20
(a) t l ( 5 , 1 0 , 1 ,
pt,, (c) t 1 5, 10, 1 , 2 ,
(el t 1 t up 1 ( [ ,
il· following are the statemd
after correcting them
(al tupl 1,6,a,8,
(cl t up 1 ( ( 0, 1, 2, ) ,
(e) tupl
,_. (al tupl 1,6,'a'
(cl tupl ( (0, 1,2
(e) tupl ('a','b
13. eonsider the f ollowl
(al tupl ( ,
pri t (ma
(bl t (1, '
[1 l I I
(cl
(d)
If ~rtt• I pythOn , , . . .
( , , ,4,
I I
Contldtrt
(
,,,) flt''"
111• cor,dltlOfl
:, ftlldltPIIY
(di OltPl•Y ,nonth
(e t,lfl r,&llfl
.... ,.
,,
lb
,,
Id
at, write a pytho" pr
phone nurnber of •
()
program t rnnnh""""' f
' ,
hould be
t I I
' '
ors and rewrite the same after corr ct gt
, . , . I • I • , ,
g -. a ... e )
_.gets< / 8
r )
)
13 Suppose I I
' I ~~) •• ' w I
f
1 .• I t h I I ' 4: I