You are on page 1of 22

CST283:Py

thonf
orMachi
neLear
ning
Modul
eII
I

 Adat
ast
ruct
urecombi
nessev
eraldat
aval
uesi
ntoauni
tsot
heycanbet
reat
ed
asonet
hing.

 Thedat
ael
ement
swi
thi
nadat
ast
ruct
urear
eusual
l
yor
gani
zedi
naspeci
alway
t
hatal
l
owst
hepr
ogr
ammert
oaccessandmani
pul
atet
hem.

 Ast
ri
ngi
sadat
ast
ruct
uret
hator
gani
zest
extasasequenceofchar
act
ers.

 I
nthi
schapt
er,
weexpl
oret
heuseoft
woot
hercommondat
ast
ruct
ures:

 Li
st

 Di
cti
onar
y.

 Al
i
stal
l
owst
hepr
ogr
ammert
omani
pul
ateasequenceofdat
aval
uesofany
t
ypes.

 Adi
cti
onar
yor
gani
zesdat
aval
uesbyassoci
ati
onwi
thot
herdat
aval
uesr
ather
t
hanbysequent
ial
posi
ti
on.

Li
sts

 Al
i
sti
sasequenceofdat
aval
uescal
l
edi
temsorel
ement
s.Ani
tem canbeof
anyt
ype.

 Eachoft
hei
temsi
nal
i
sti
sor
der
edbyposi
ti
on.

 Eachi
tem i
nal
i
sthasauni
quei
ndext
hatspeci
fi
esi
tsposi
ti
on.

 Thei
ndexoft
hef
ir
sti
tem i
s0,andt
hei
ndexoft
hel
asti
tem i
sthel
engt
hoft
he

For More Visit : KtuQbank.com | Fair Use Policy


l
i
stmi
nus1.

 I
nPy
thon,al
i
sti
swr
it
tenasasequenceofdat
aval
uessepar
atedbycommas.
Theent
ir
esequencei
sencl
osedi
nsquar
ebr
acket
s([and]
).

 Youcanal
souseot
herl
i
stsasel
ement
sinal
i
st,
ther
ebycr
eat
ingal
i
stofl
i
sts.

 Whent
heel
ementi
sav
ari
abl
eoranyot
herexpr
essi
on,i
tsv
aluei
sincl
udedi
n
t
hel
i
st.

 Youcanal
sobui
l
dli
stsofi
nteger
susi
ngt
her
angeandl
istf
unct
ions

For More Visit : KtuQbank.com | Fair Use Policy


 Thef
unct
ionl
enandt
hesubscr
iptoper
ator[
]wor
kjustast
heydof
orst
ri
ngs:

 Concat
enat
ion(
+)andequal
i
ty(
==)al
sowor
kasexpect
edf
orl
i
sts:

 Thepr
intf
unct
ionst
ri
pst
hequot
ati
onmar
ksf
rom ast
ri
ng,
butdoesnotal
tert
he
l
ookofal
i
st:

For More Visit : KtuQbank.com | Fair Use Policy


 Topr
intt
hecont
ent
sofal
i
stwi
thoutt
hebr
acket
sandcommas,
youcanuseaf
or
l
oop,
asf
oll
ows:

 Fi
nal
l
y,y
oucanuset
hei
noper
atort
odet
ectt
hepr
esenceorabsenceofagi
ven
el
ement
:

For More Visit : KtuQbank.com | Fair Use Policy


 Tabl
esummar
izest
heseoper
ator
sandf
unct
ions,
wher
eLr
efer
stoal
i
st.

Repl
aci
nganEl
ementi
naLi
st

 Thesubscri
ptoperat
ori
susedt
orepl
aceanel
ementatagi
venposi
ti
on,
as
showninthenextsessi
on:

 Muchofl
i
stpr
ocessi
ngi
nvol
vesr
epl
aci
ngeachel
ement
,wi
tht
her
esul
tof

For More Visit : KtuQbank.com | Fair Use Policy


appl
yi
ngsomeoper
ati
ont
othatel
ement
.

 Thenextexampl
erepl
acest
hef
ir
stt
hreeel
ement
sofal
i
stwi
thnewones:

Li
stMet
hodsf
orI
nser
ti
ngandRemov
ingEl
ement
s

For More Visit : KtuQbank.com | Fair Use Policy


For More Visit : KtuQbank.com | Fair Use Policy
Sear
chi
ngaLi
st

 Af
terel
ement
shav
ebeenaddedt
oal
i
st,apr
ogr
am cansear
chf
oragi
ven
el
ement
.
 Thei
noper
atordet
ermi
nesanel
ement
’spr
esenceorabsence,
butpr
ogr
ammer
s
of
ten ar
emor
eint
erest
ed i
nthe posi
ti
on ofan el
ementi
fiti
sfound (
for
r
epl
acement
,remov
al,orot
heruse)
.Unf
ort
unat
ely
,thel
istt
ypedoesnoti
ncl
ude
t
heconv entf
eni indmet
hodt
hati
susedwi
thst
ri
ngs.
 I
nst
eadoff
ind,
youmustuset
hemet
hodi
ndext
olocat
eanel
ement
’sposi
ti
oni
n
al
i
st.
 I
tisunf
ort
unat
ethati
ndexr
aisesaner
rorwhent
het
argetel
ementi
snotf
ound.
 Toguar
dagai
nstt
hisunpl
easantconsequence,
youmustf
ir
stuset
hei
noper
ator
t
otestf
orpr
esenceandt
hent
hei
ndexmet
hodi
fthi
stestr
etur
nsTr
ue.

For More Visit : KtuQbank.com | Fair Use Policy


Sor
ti
ngaLi
st

 Al
thoughal
i
st’
sel
ement
sar
eal
way
sor
der
edbyposi
ti
on,
iti
spossi
blet
oimpose
anat
ural
order
ingont
hem aswel
l
.
 I
not
herwor
ds,
youcanar
rangesomeel
ement
sinnumer
icoral
phabet
icalor
der
.
Al
i
stofnumber
sinascendi
ngor
derandal
i
stofnamesi
nal
phabet
icalor
derar
e
sor
tedl
i
sts.
 Whent
heel
ement
scanber
elat
edbycompar
ingt
hem f
orl
esst
hanandgr
eat
er
t
hanaswel
lasequal
i
ty,
theycanbesor
ted.
 Thel
i hodsor
stmet tmut
atesal
i
stbyar
rangi
ngi
tsel
ement
sinascendi
ngor
der
.
Her
eisanexampl
eofi
tsuse:

Li
stcompr
ehensi
on

 Li
stcompr
ehensi
onsar
eusedf
orcr
eat
ingnewl
i
stsf
rom ot
heri
ter
abl
es.

 Asl
i
stcompr
ehensi
onsr
etur
nli
sts,t
heyconsi
stofbr
acket
scont
aini
ng t
he
expr
essi
on,
whi
chi
sexecut
edf
oreachel
emental
ongwi
tht
hef
orl
oopt
oit
erat
e
ov
ereachel
ement
.

 Thi
sist
hebasi
csy
ntax:

For More Visit : KtuQbank.com | Fair Use Policy


new_
li
st=[
expr
essi
onf
or_
loop_
one_
or_
mor
econdi
ti
ons]

 Forexampl
e,f
indt
hesquar
esofanumberusi
ngt
hef
orl
oop:

number
s=[
1,2,
3,
4]

squar
es=[
]

f
orni
nnumber
s:

squar
es.
append(
n**
2)

pr
int
(squar
es)#Out
put
:[1,
4,9,
16]

 Fi
ndi
ngsquar
esusi
ngl
i
stcompr
ehensi
ons:

number
s=[
1,2,
3,4]

squar
es=[
n**
2forni
nnumber
s]

pr
int
(squar
es)#Out
put
:[1,
4,9,
16]

 Fi
ndcommonnumber
sfr
om t
wol
i
stsusi
ngl
i
stcompr
ehensi
on:
???
???
???
??

For More Visit : KtuQbank.com | Fair Use Policy


Tupl
es

 At
upl
eisat
ypeofsequencet
hatr
esembl
esal
i
st,exceptt
hat
,unl
i
keal
i
st,a
t
upl
eisi
mmut
abl
e.You i
ndi
cat
eat
upl
eli
ter
ali
n Py
thon byencl
osi
ng i
ts
el
ement
sinpar
ent
hesesi
nst
eadofsquar
ebr
acket
s.Thenextsessi
onshows
howt
ocr
eat
esev
eral
tupl
es:

 Youcanusemostoft
heoper
ator
sandf
unct
ionsusedwi
thl
i
stsi
nasi
mil
ar
f
ashi
onwi
tht
upl
es.

Di
cti
onar
ies

 Li
stsor
gani
zet
hei
rel
ement
sbyposi
ti
on.

 Howev
er,i
nsomesi
tuat
ions,t
heposi
ti
onofadat
um i
nast
ruct
urei
sir
rel
evant
;

For More Visit : KtuQbank.com | Fair Use Policy


we’
rei
nter
est
edi
nit
sassoci
ati
onwi
thsomeot
herel
ementi
nthest
ruct
ure.

 Adi
cti
onar
yor
gani
zesi
nfor
mat
ionbyassoci
ati
on,
notposi
ti
on.

 Forexampl
e,wheny
ouuseadi
cti
onar
ytol
ookupt
hedef
ini
ti
onof“
mammal
,

y
oudon’
tst
artatpage1;i
nst
ead,y
out
urndi
rect
lyt
othewor
dsbegi
nni
ngwi
th

M.”

 I
nPy
thon,
adi
cti
onar
yassoci
atesasetofkey
swi
thdat
aval
ues.

 APy
thondi
cti
onar
yiswr
it
tenasasequenceofkey
/val
uepai
rssepar
atedby
commas.

 Thesepai
rsar
esomet
imescal
l
edent
ri
es.Theent
ir
esequenceofent
ri
esi
s
encl
osedi
ncur
lybr
aces(
{and}
).Acol
on(
:)separ
atesakeyandi
tsv
alue.

 Her
ear
esomeexampl
edi
cti
onar
ies:

Aphonebook:
{'Sav
annah'
:
'476-
3321'
,'
Nat
hani
el'
:
'351-
7743'
}

Per
sonal
inf
ormat
ion:
{'Name'
:
'Mol
l
y',
'Age'
:
18}

 Youcanev
encr
eat
eanempt
ydi
cti
onar
y—t
hati
s,adi
cti
onar
ythatcont
ainsno
ent
ri
es.

Addi
ngKey
sandRepl
aci
ngVal
ues

 Youaddanewkey
/val
uepai
rtoadi
cti
onar
ybyusi
ngt
hesubscr
iptoper
ator[
].

 Thef
orm oft
hisoper
ati
oni
sthef
oll
owi
ng:

For More Visit : KtuQbank.com | Fair Use Policy


 Thenextcodesegmentcr
eat
esanempt
ydi
cti
onar
yandaddst
wonewent
ri
es:

 Thesubscr
ipti
sal
sousedt
orepl
aceav
alueatanexi
sti
ngkey
,asf
oll
ows:

Accessi
ngVal
ues

 Youcan al
so uset
hesubscr
iptt
o obt
aint
hev
alueassoci
ated wi
thakey
.
Howev
er,i
fthekeyi
snotpr
esenti
nthedi
cti
onar
y,Py
thonr
aisesaner
ror
.Her
e
ar
esomeexampl
es,
usi
ngt
hei
nfodi
cti
onar
y,whi
chwassetupear
li
er:

 I
ftheexi
stenceofakeyi
suncer
tai
n,t
hepr
ogr
ammercant
estf
ori
tusi
ngt
he
di
cti
onar
ymet
hodhas_
key
,butaf
areasi
erst
rat
egyi
stouset
hemet
hodget
.
Thi
smet
hodexpect
stwoar
gument
s,apossi
blekeyandadef
aul
tval
ue.I
fthe
keyi
sint
hedi
cti
onar
y,t
heassoci
atedv
aluei
sret
urned.Howev
er,i
fthekeyi
s
absent
,thedef
aul
tval
uepassedt
ogeti
sret
urned.(
has_
keywasr
emov
edi
n
py
thon3)

For More Visit : KtuQbank.com | Fair Use Policy


 Her
eisanexampl
eoft
heuseofgetwi
thadef
aul
tval
ueofNone:

Remov
ingKey
s

 Todel
eteanent
ryf
rom adi
cti
onar
y,oner
emov
esi
tskeyusi
ngt
hemet
hodpop.

 Thi
smet
hodexpect
sakeyandanopt
ionaldef
aul
tval
ueasar
gument
s.I
fthekey
i
sint
hedi
cti
onar
y,i
tisr
emov
ed,
andi
tsassoci
atedv
aluei
sret
urned.Ot
her
wise,
t
hedef
aul
tval
uei
sret
urned.

 I
fpopi
susedwi
thj
ustonear
gument
,andt
hiskeyi
sabsentf
rom t
hedi
cti
onar
y,
Py
thonr
aisesaner
ror
.

 Thenextsessi
onat
tempt
stor
emov
etwokey
sandpr
int
sthev
aluesr
etur
ned:

Tr
aver
singaDi
cti
onar
y

 Whenaf
orl
oopi
susedwi
thadi
cti
onar
y,t
hel
oop’
svar
iabl
eisboundt
oeachkey
i
nanunspeci
fi
edor
der
.

 Thenextcodesegmentpr
int
sal
loft
hekey
sand t
hei
rval
uesi
n ouri
nfo
di
cti
onar
y:

For More Visit : KtuQbank.com | Fair Use Policy


 Al
ter
nat
ivel
y,y
oucoul
duset
hedi
cti
onar
ymet
hodi
tems(
)toaccessal
i
stoft
he
di
cti
onar
y’s ent
ri
es.The nextsessi
on shows a r
un oft
his met
hod wi
tha
di
cti
onar
yofgr
ades:

 Not
ethatt
heent
ri
esar
erepr
esent
edast
upl
eswi
thi
nthel
i
st.

 At
upl
eofv
ari
abl
escant
henaccesst
hekeyandv
alueofeachent
ryi
nthi
sli
st
wi
thi
naf
orl
oop:

 I
faspeci
alor
der
ingoft
hekey
sisneeded,
youcanobt
ainal
i
stofkey
susi
ngt
he
key
smet
hodandpr
ocesst
hisl
i
stt
orear
ranget
hekey
s.

 Forexampl
e,y
oucansor
tthel
i
standt
hent
rav
ersei
ttopr
intt
heent
ri
esoft
he
di
cti
onar
yinal
phabet
ical
order
:

For More Visit : KtuQbank.com | Fair Use Policy


 Tabl
ebel
owsummar
izest
hecommonl
yuseddi
cti
onar
yoper
ati
ons,
wher
edr
efer
sto
adi
cti
onar
y.

For More Visit : KtuQbank.com | Fair Use Policy


Set

 A seti
sacol
l
ect
ionwhi
chi
sunor
der
edanduni
ndexed.I
nPy
thon,set
sar
e
wr
it
tenwi
thcur
lybr
acket
s.

 Cr
eat
eaSet
:

t
hisset={
"appl
e",
 
"banana"

"cher
ry"
}
pr
int
(thi
sset
)
 Set
sar
eunor
der
ed,
soy
oucannotbesur
einwhi
chor
dert
hei
temswi
l
lappear
.

For More Visit : KtuQbank.com | Fair Use Policy


 AccessI
tems:
Youcannotaccessi
temsi
nasetbyr
efer
ri
ngt
oani
ndexorakey
.

 Buty
oucanl
oopt
hrought
heseti
temsusi f
nga or
 l
oop,
oraski
faspeci
fi
ed
v
aluei
spr
esenti
naset
,byusi
ngt
he 
i
n key
wor
d.

t
hisset={
"appl
e",
 
"banana"

"cher
ry"
}
f
or 
x i
n t
hisset
:
 
 pr
int
(x)

 Checki
f"banana"i
spr
esenti
ntheset
:

t
hisset={
"appl
e",
 
"banana"

"cher
ry"
}
pr
int
("
banana"
 i
n t
hisset
)

 AddI
tems:Toaddonei
tem t
oasetuset
he 
add(

met
hod.Toaddmor
ethanone
i
tem t
oasetuset
he 
updat
e()
 met
hod.

#usi
ngt
he 
add(

met
hod

t
hisset={
"appl
e",
 
"banana"

"cher
ry"
}

t
hisset
.add(
"or
ange"
)

pr
int
(thi
sset
)
#usi
ngt
he 
updat
e()
 met
hod

t
hisset={
"appl
e",
 
"banana"

"cher
ry"
}

t
hisset
.updat
e([
"or
ange"

"mango"

"gr
apes"
])

pr
int
(thi
sset
)

 GettheLengthofaSet
:Todet
ermi
nehowmanyi
temsasethas,
use
t
he l
en()
 method.
t
hisset={
"appl
e",
 
"banana"

"cher
ry"
}
pr
int
(l
en(
thi
sset
))

For More Visit : KtuQbank.com | Fair Use Policy


 Remov
e I
tem: To r
emov
e an i
tem i
n a set
, use t
he 
remov
e()
, or
t
he 
discar
d()
 met
hod.
Ift
hei
tem t
oremov
edoesnotexi
st,
 
remov
e()
 wi
l
lrai
sean
er
ror
. I
fthei
tem t
oremov
edoesnotexi
st,
 
discar
d()
 wi
l

NOT 
rai
seaner
ror
.You
canal
souset
he 
pop(
),met
hodt
oremov
eani
tem,butt
hismet
hodwi
l
lremov
e
t l
ast
he   i
tem.
Ther
etur
nval
ueoft
he 
pop(

met
hodi
sther
emov
edi
tem.

#usi
ngt
he 
remov
e()
 met
hod

t
hisset={
"appl
e",
 
"banana"

"cher
ry"
}
t
hisset
.r
emov
e("
banana"
)
pr
int
(thi
sset
)

#usi
ngt
he 
discar
d()
 met
hod

t
hisset={
"appl
e",
 
"banana"

"cher
ry"
}
t
hisset
.di
scar
d("
banana"
)
pr
int
(thi
sset
)

#usi
ngt
he 
pop(

met
hod

t
hisset={
"appl
e",
 
"banana"

"cher
ry"
}
x= 
thi
sset
.pop(
)
pr
int
(x)
pr
int
(thi
sset
)

For More Visit : KtuQbank.com | Fair Use Policy


Dat
eti
me

 Dat
es:Adat
einPy
thoni
snotadat
aty
peofi
tsown,
butwecani
mpor
tamodul
e
dat
named  eti
me 
towor
kwi
thdat
esasdat
eobj
ect
s.

 I
mpor
tthedat
eti
memodul
eanddi
spl
ayt
hecur
rentdat
e:

i
mpor

dat
eti
me
x=dat
eti
me.
dat
eti
me.
now(
)
pr
int
(x)

For More Visit : KtuQbank.com | Fair Use Policy


 Cr
eat
ing Dat
e Obj
ect
s:To cr
eat
e a dat
e,we can use t
he 
dat
eti
me(

class
(
const
ruct
or) of t
he 
dat
eti
me 
modul
e. The 
dat
eti
me(

class r
equi
res t
hree
par
amet
erst
ocr
eat
eadat
e:y
ear
,mont
h,day
.

i
mpor

dat
eti
me
x=dat
eti
me.
dat
eti
me(
2020,
 
5, 
17)
pr
int
(x)

 The 
dat
eti
me(

classal
sot
akespar
amet
ersf
ort
imeandt
imezone(
hour
,mi
nut
e,
second,mi
crosecond,t
zone)
,butt
heyar
eopt
ional
,andhasadef
aul
tval
ueof
 0,
(
None 
fort
imezone)
.

 Thest
rft
ime(
)Met
hod:The 
dat
eti
me 
obj
ecthasamet
hodf
orf
ormat
ti
ngdat
e
obj
ect
sint
oreadabl
est
ri
ngs.Themet
hodi
scal
l
ed 
str
fti
me(
),andt
akesone
par
amet
er,
 
for
mat
,tospeci
fyt
hef
ormatoft
her
etur
nedst
ri
ng:

i
mpor

dat
eti
me
x=dat
eti
me.
dat
eti
me(
2018,
 
6, 
1)
pr
int
(x.
str
fti
me(
"%B"
))

For More Visit : KtuQbank.com | Fair Use Policy


 Ar
efer
enceofal
lthel
egal
for
matcodes:

For More Visit : KtuQbank.com | Fair Use Policy

You might also like