Sie sind auf Seite 1von 12

!

"#$%&' )*+,+
ython keference

Ar|thmet|c Lxpress|ons 2
Comparlsons 2
Var|ab|es and Ass|gnment 3
!"#$% '
(%%)*+#$+, -,",$#$+, '
./0,)10$ (%%)*+#$+, '
rocedures 4
If Statements S
23*)4"0 51$6",36% 7
Loops 6
89)0$ 2331% :
8reak SLaLemenL 6
;36 2331% :
Str|ngs 7
-,6)+* 51$6",)3+% <
lndexlng SLrlngs 7
1he lndexlng operaLor provldes a way Lo exLracL subsequences of characLers from a sLrlng. 7
flnd 8
ConverLlng beLween numbers and SLrlngs 8
Looplng Lhrough SLrlngs 8
L|sts 9
Loops on LlsLs 9
D|ct|onar|es 10
Loops on ulcLlonarles 10
Lxcept|ons 11
L|brar|es 11
Cther 8u||t-In Iunct|ons 12
=>"0 ?@
A)#$ ?@

Ar|thmet|c Lxpress|ons

add|t|on: <-./012> + <-./012> ! <-./012>
ouLpuLs Lhe sum of Lhe Lwo lnpuL numbers
mu|t|p||cat|on: <-./012> * <-./012> ! <-./012>
ouLpuLs Lhe producL of Lhe Lwo lnpuL numbers
subtract|on: <-./012> - <-./012> ! <-./012>
ouLpuLs Lhe dlfference beLween Lhe Lwo lnpuL numbers
d|v|s|on: <-./012> ] <-./012> ! <-./012>
ouLpuLs Lhe resulL of dlvldlng Lhe flrsL number by Lhe second
Note: lf boLh numbers are whole numbers, Lhe resulL ls LruncaLed Lo [usL Lhe whole
number parL.
modu|o: <-./012> <-./012> ! <-./012>
ouLpuLs Lhe remalnder of dlvldlng Lhe flrsL number by Lhe second
exponent|at|on: <3#41> ** <56712> ! <-./012>
ouLpuLs Lhe resulL of ralslng <3#41> Lo Lhe <56712> power (mulLlplylng <3#41> by lLself
<56712> number of Llmes).
Compar|sons
equa||ty: <8#9.1> == <8#9.1> ! <36691#:>
ouLpuLs 1rue lf Lhe Lwo lnpuL values are equal, Ia|se oLherwlse
|nequa||ty: <8#9.1> != <8#9.1> ! <36691#:>
ouLpuLs 1rue lf Lhe Lwo lnpuL values are noL equal, Ia|se oLherwlse
greater than: <-./012
1
> > <-./012
2
> ! <36691#:>
ouLpuLs 1rue lf -./012
1
ls greaLer Lhan -./012
2

|ess than: <-./012
1
> < <-./012
2
> ! <36691#:>
ouLpuLs 1rue lf -./012
1
ls less Lhan Lhan -./012
2

greater or equa| to: <-./012
1
> >= <-./012
2
> ! <36691#:>
ouLpuLs 1rue lf -./012
1
ls noL less Lhan -./012
2

|ess than or equa| to: <-./012
1
> <= <-./012
2
> ! <36691#:>
ouLpuLs 1rue lf -./012
1
ls noL greaLer Lhan -./012
2




Var|ab|es and Ass|gnment
Names
A <-#/1> ln yLhon can be any sequence of leLLers, numbers, and underscores (_) LhaL does noL sLarL
wlLh a number. We usually use all lowercase leLLers for varlable names, buL caplLallzaLlon musL maLch
exacLly. Pere are some valld examples of names ln yLhon (buL mosL of Lhese would noL be good
cholces Lo acLually use ln your programs):

my_name
one2one
Dor|na
th|s_|s_a_very_|ong_var|ab|e_name
Ass|gnment Statement
An asslgnmenL sLaLemenL asslgns a value Lo a varlable:

<-#/1> = <;<=2144%6:>

AfLer Lhe asslgnmenL sLaLemenL, Lhe varlable <-#/1> refers Lo Lhe value of Lhe <;<=2144%6:> on Lhe
rlghL slde of Lhe asslgnmenL. An <;<=2144%6:> ls any yLhon consLrucL LhaL has a value.

Mu|t|p|e Ass|gnment
We can puL more Lhan one name on Lhe lefL slde of an asslgnmenL sLaLemenL, and a correspondlng
number of expresslons on Lhe rlghL slde:

<-#/1
1
>, <-#/1
2
>, ... = <;<=2144%6:
1
>, <;<=2144%6:
2
>, ...

All of Lhe expresslons on Lhe rlghL slde are evaluaLed flrsL. 1hen, each name on Lhe lefL slde ls asslgned
Lo reference Lhe value of Lhe correspondlng expresslon on Lhe rlghL slde. 1hls ls handy for swapplng
varlable values. lor example,
s, t = t, s
would swap Lhe values of s and t so afLer Lhe asslgnmenL sLaLemenL s now refers Lo Lhe prevlous value
of t, and t refers Lo Lhe prevlous value of s.

Note: whaL ls really golng on here ls a blL dlfferenL. 1he mulLlple values are packed ln a Luple
(whlch ls slmllar Lo Lhe llsL daLa Lype lnLroduced ln unlL 3, buL an lmmuLable verslon of a llsL),
and Lhen unpacked lnLo lLs componenLs when Lhere are mulLlple names on Lhe lefL slde. 1hls
dlsLlncLlon ls noL lmporLanL for whaL we do ln cs101, buL does become lmporLanL ln some
conLexLs.

rocedures
A procedure Lakes lnpuLs and produces ouLpuLs. lL ls an absLracLlon LhaL provldes a way Lo use Lhe same
code Lo operaLe on dlfferenL daLa by passlng ln LhaL daLa as lLs lnpuLs.

ueflnlng a procedure:

def <-#/1>(<5#2#/1&124>):
<396$>>

1he <5#2#/1&124> are Lhe lnpuLs Lo Lhe procedure. 1here ls one <-#/1> for each lnpuL ln order,
separaLed by commas. 1here can be any number of parameLers (lncludlng none).

1o produce ouLpuLs:

return <;<=2144%6:>, <;<=2144%6:>, .

1here can be any number of expresslons followlng Lhe reLurn (lncludlng none, ln whlch case Lhe ouLpuL
of Lhe procedure ls Lhe speclal value None).

uslng a procedure:

<526$1".21>(<?:=.&>, <?:=.&>, .)

1he number of lnpuLs musL maLch Lhe number of parameLers. 1he value of each lnpuL ls asslgned Lo Lhe
value of each parameLer name ln order, and Lhen Lhe block ls evaluaLed.


If Statements
1he |f sLaLemenL provldes a way Lo conLrol whaL code execuLes based on Lhe resulL of a LesL expresslon.

|f <@14&;<=2144%6:>:
<396$>>

1he code ln <396$>> only execuLes lf Lhe <@14&;<=2144%6:> has a 1rue value.

A|ternate c|auses. We can use an e|se clause ln an |f sLaLemenL Lo provlde code LhaL wlll run when Lhe
<@14&;<=2144%6:> has a Ia|se value.

|f <@14&;<=2144%6:>:
<396$>
1rue
>
e|se:
<396$>
Ia|se
>
Log|ca| Cperators
1he and and or operaLors behave slmllarly Lo loglcal con[uncLlon (and) and dls[uncLlon (or). 1he
lmporLanL properLy Lhey have whlch ls dlfferenL from oLher operaLors ls LhaL Lhe second operand
expresslon ls evaluaLed only when necessary.

<;<=2144%6:
1
> and <;<=2144%6:
2
>
lf ;<=2144%6:
1
has a Ia|se value, Lhe resulL ls Ia|se and ;<=2144%6:
2
ls noL evaluaLed (so
even lf lL would produce an error lL does noL maLLer). lf ;<=2144%6:
1
has a 1rue value,
Lhe resulL of Lhe and ls Lhe value of ;<=2144%6:
2
.


<;<=2144%6:
1
> or <;<=2144%6:
2
>
lf ;<=2144%6:
1
has a 1rue value, Lhe resulL ls 1rue and ;<=2144%6:
2
ls noL evaluaLed (so
even lf lL would produce an error lL does noL maLLer). lf ;<=2144%6:
1
has a Ia|se value,
Lhe resulL of Lhe or ls Lhe value of ;<=2144%6:
2
.



Loops
Loops provlde a way Lo evaluaLe Lhe same block of code an arblLrary number of Llmes.
Wh||e Loops
A wh||e loop provldes a way Lo keep execuLlng a block of code as long as a LesL expresslon ls 1rue.

wh||e <@14&;<=2144%6:>:
<396$>>

lf Lhe <@14&;<=2144%6:> evaluaLes Lo Ia|se, Lhe wh||e loop ls done and execuLlon conLlnues wlLh Lhe
followlng sLaLemenL. lf Lhe <@14&;<=2144%6:> evaluaLes Lo 1rue, Lhe <396$>> ls execuLed. 1hen, Lhe loop
repeaLs, reLurnlng Lo Lhe <@14&;<=2144%6:> and conLlnulng Lo evaluaLe Lhe <396$>> as long as Lhe
<@14&;<=2144%6:> ls 1rue.
8reak Statement
A break sLaLemenL ln Lhe <396$>> of a wh||e loop, [umps ouL of Lhe conLalnlng whlle loop, conLlnulng
execuLlon aL Lhe followlng sLaLemenL.

break

Ior Loops
A for loop provldes a way Lo execuLe a block once for each elemenL of a collecLlon:

for <-#/1> |n <)6991$&%6:>:
<396$>>

1he loop goes Lhrough each elemenL of Lhe collecLlon, asslgnlng LhaL elemenL Lo Lhe <-#/1> and
evaluaLlng Lhe <396$>>. 1he collecLlon could be a SLrlng, ln whlch case Lhe elemenLs are Lhe characLers
of a sLrlng, a LlsL, ln whlch case Lhe elemenLs are Lhe elemenLs of Lhe llsL, a ulcLlonary, ln whlch case Lhe
elemenLs are Lhe keys ln Lhe dlcLlonary, or many oLher Lypes ln yLhon LhaL represenL collecLlons of
ob[ecLs.
Str|ngs
A sLrlng ls sequence of characLers surrounded by quoLes. 1he quoLes can be elLher slngle or double
quoLes, buL Lhe quoLes aL boLh ends of Lhe sLrlng musL be Lhe same Lype. Pere are some examples of
sLrlngs ln yLhon:

"sllly"
'sLrlng'
"l'm a valld sLrlng, even wlLh a slngle quoLe ln Lhe mlddle!"
Str|ng Cperat|ons

|ength: |en(<*&2%:A>) ! <-./012>
CuLpuLs Lhe number of characLers ln <*&2%:A>

str|ng concatenat|on: <*&2%:A> + <*&2%:A> ! <*&2%:A>
ouLpuLs Lhe concaLenaLlon of Lhe Lwo lnpuL sLrlngs (pasLlng Lhe sLrlng LogeLher wlLh no space
beLween Lhem)

str|ng mu|t|p||cat|on: <*&2%:A> * <-./012> ! <*&2%:A>
ouLpuLs a sLrlng LhaL ls <!/#B$6> coples of Lhe lnpuL <-,6)+*> pasLed LogeLher

sp||t: <*&2%:A>.sp||t() ! [<*&2%:A>, <*&2%:A>, . ]
ouLpuLs a llsL of sLrlngs LhaL are (roughly) Lhe words conLalned ln Lhe lnpuL sLrlng. 1he words
are deLermlned by whlLespace (elLher spaces, Labs, or newllnes) ln Lhe sLrlng. (We dld noL cover
Lhls ln class, buL spllL can also be used wlLh an opLlonal lnpuL LhaL ls a llsL of Lhe separaLor
characLers, and a second opLlonal lnpuL LhaL conLrols Lhe maxlmum number of elemenLs ln Lhe
ouLpuL llsL.)

Index|ng Str|ngs
1he lndexlng operaLor provldes a way Lo exLracL subsequences of characLers from a sLrlng.
str|ng |ndex|ng: <*&2%:A>[<-./012>] ! <*&2%:A>
ouLpuLs a slngle-characLer sLrlng conLalnlng Lhe characLer aL poslLlon C!/#B$6D of Lhe lnpuL
C-,6)+*D. oslLlons ln Lhe sLrlng are counLed sLarLlng from 0, so s[1] would ouLpuL Lhe second
characLer ln s. lf Lhe C!/#B$6D ls negaLlve, poslLlons are counLed from Lhe end of Lhe sLrlng:
s[-1] ls Lhe lasL characLer ln s.

str|ng extract|on: <*&2%:A>[<*&#2& -./012>:<*&6= -./012>] ! <*&2%:A>
ouLpuLs a sLrlng LhaL ls Lhe subsequence of Lhe lnpuL sLrlng sLarLlng from poslLlon
<-,"6, !/#B$6> and endlng [usL before poslLlon <-,31 !/#B$6>. lf <-,"6, !/#B$6> ls mlsslng,
sLarLs from Lhe beglnnlng of Lhe lnpuL sLrlng, lf <-,31 !/#B$6> ls mlsslng, goes Lo Lhe end of Lhe
lnpuL sLrlng.

f|nd
1he f|nd meLhod provldes a way Lo flnd sub-sequences of characLers ln sLrlngs.

f|nd: <*1#2$B *&2%:A>.f|nd(<@#2A1& *&2%:A>) ! <-./012>
ouLpuLs a number glvlng Lhe poslLlon ln <-$"649 -,6)+*> where <A"6*$, -,6)+*> flrsL appears. lf
Lhere ls no occurrence of <A"6*$, -,6)+*> ln <-$"649 -,6)+*>, ouLpuLs -1.

1o flnd laLer occurrences, we can also pass ln a number Lo flnd:

f|nd after: <*1#2$B *&2%:A>.f|nd(<@#2A1& *&2%:A>, <*&#2& -./012>) ! <-./012>
ouLpuLs a number glvlng Lhe poslLlon ln <-$"649 -,6)+*> where <A"6*$, -,6)+*> flrsL appears LhaL
ls aL or afLer Lhe poslLlon glve by <-,"6, !/#B$6>. lf Lhere ls no occurrence of <A"6*$, -,6)+*> ln
<-$"649 -,6)+*> aL or afLer <-,"6, !/#B$6>, ouLpuLs -1.
Convert|ng between Numbers and Str|ngs
str: str(<-./012>) ! <*&2%:A>
ouLpuLs a sLrlng LhaL represenLs Lhe lnpuL number. lor example, str(23) ouLpuLs Lhe sLrlng '23'.

ord: ord(<C:1D)B#2#$&12 *&2%:A>) ! <-./012>
ouLpuLs Lhe number correspondlng Lo Lhe lnpuL sLrlng.

chr: chr(<-./012>) ! <C:1D)B#2#$&12 *&2%:A>
ouLpuLs Lhe one-characLer sLrlng correspondlng Lo Lhe number lnpuL. 1hls funcLlon ls Lhe
lnverse of ord: chr(ord(!)) = ! for any one-characLer sLrlng !.
Loop|ng through Str|ngs
A for loop provldes a way Lo execuLe a block once for each characLer ln a sLrlng ([usL llke looplng Lhrough
Lhe elemenLs of a llsL):

for <-#/1> |n <*&2%:A>:
<396$>>

1he loop goes Lhrough each characLer of Lhe sLrlng ln Lurn, asslgnlng LhaL elemenL Lo Lhe <-#/1> and
evaluaLlng Lhe <396$>>.
L|sts
A llsL ls a muLable collecLlon of ob[ecLs. 1he elemenLs ln a llsL can be of any Lype, lncludlng oLher llsLs.

Construct|ng a ||st. A llsL ls a sequence of zero or more elemenLs, surrounded by square brackeLs:

[ <;91/1:&>, <;91/1:&>, . ]

Se|ect|ng e|ements: <E%4&>[<-./012>] ! <;91/1:&>
CuLpuLs Lhe value of Lhe elemenL ln <2)%,> aL poslLlon <!/#B$6>. LlemenLs are lndexed sLarLlng
from 0.
Se|ect|ng sub-sequences: <E%4&>[<*&#2&> : <*&6=>] ! <E%4&>
CuLpuLs a sub-sequence of <2)%,> sLarLlng from poslLlon <-,"6,>, up Lo (buL noL lncludlng)
poslLlon <-,31>.
Update: <E%4&>[<-./012>] = <8#9.1>
Modlfles Lhe value of Lhe elemenL ln <2)%,> aL poslLlon <!/#B$6> Lo be <E"0/$>.

Length: |en(<E%4&>) ! <-./012>
CuLpuLs Lhe number of (Lop-level) elemenLs ln <2)%,>.

Append: <E%4&>.append(<;91/1:&>)
MuLaLes <2)%,> by addlng <=0$#$+,> Lo Lhe end of Lhe llsL.
Concatenat|on: <E%4&
1
> + <E%4&
2
> ! <;91/1:&>
CuLpuLs a new llsL LhaL ls Lhe elemenLs of <2)%,
1
> followed by Lhe elemenLs of <2)%,
2
>.
opp|ng: <E%4&>.pop() ! <;91/1:&>
MuLaLes <2)%,> by removlng lLs lasL elemenL. CuLpuLs Lhe value of LhaL elemenL. lf Lhere are no
elemenLs ln <2)%,>, [].pop() produces an error.

I|nd|ng: <E%4&>.|ndex(<8#9.1>) ! <-./012>
CuLpuLs Lhe poslLlon of Lhe flrsL occurrence of an elemenL maLchlng <E"0/$> ln <2)%,>. lf
<E"0/$> ls noL found ln <2)%,>, produces an error.

Membersh|p: <8#9.1> |n <E%4&> ! <36691#:>
CuLpuLs 1rue lf <E"0/$> occurs ln <2)%,>. CLherwlse, ouLpuLs lalse.
Non-membersh|p: <8#9.1> not |n <E%4&> ! <36691#:>
CuLpuLs lalse lf <E"0/$> occurs ln <2)%,>. CLherwlse, ouLpuLs 1rue.

Loops on L|sts
A for loop provldes a way Lo execuLe a block once for each elemenL of a LlsL:

for <-#/1> |n <E%4&>:
<396$>>

D|ct|onar|es
A ulcLlonary provldes a mapplng beLween keys, whlch can be values of any lmmuLable Lype, and values,
whlch can be any value. 8ecause a ulcLlonary ls lmplemenLed uslng a hash Lable, Lhe Llme Lo lookup a
value does noL lncrease (slgnlflcanLly) even when Lhe number of keys lncreases.

Construct|ng a D|ct|onary. A ulcLlonary ls a seL of zero or more key-value palrs, surrounded by squlggly
braces:
{ <key>: <Va|ue>, <key>: <Va|ue>, . }

Look|ng up e|ements: <F%$&%6:#2'>[<G1'>] ! <8#9.1>
ouLpuLs Lhe value assoclaLed wlLh <F$G> ln Lhe <H)4,)3+"6G>. roduces an error lf Lhe <F$G> ls
noL a key ln Lhe <H)4,)3+"6G>.

Updat|ng e|ements: <F%$&%6:#2'>[<G1'>] = <8#9.1>
updaLes Lhe value assoclaLed wlLh <F$G> ln Lhe <H)4,)3+"6G> Lo be <E"0/$>. lf <F$G> ls already a
key ln <H)4,)3+"6G>, replaces Lhe value assoclaLed wlLh <F$G>, lf noL, adds a new <F$G>: <E"0/$>
palr Lo Lhe <H)4,)3+"6G>.

Membersh|p: <G1'> |n <F%$&%6:#2'> ! <36691#:>
ouLpuLs 1rue lf <F$G> ls a key ln <H)4,)3+"6G>, Ia|se oLherwlse.


Loops on D|ct|onar|es
A for loop provldes a way Lo execuLe a block once for each key ln a ulcLlonary:

for <-#/1> |n <F%$&%6:#2'>:
<396$>>



Lxcept|ons
(We lnLroduced excepLlons ln unlL 4 because we needed Lhem ln our get_page procedure, buL
you are noL expecLed Lo undersLand Lhls ln deLall or be able Lo use excepLlon handlers ln your
own code ln cs101.)

try:
<A6G I034J>
except:
<K"+L0$6 I034J>

LxecuLe Lhe code ln Lhe <A6G I034J>. lf lL compleLes normally, sklp Lhe <K"+L0$6 I034J> and
conLlnue wlLh Lhe followlng sLaLemenL. lf code ln Lhe <A6G I034J> ralses an error, [ump Lo Lhe
code ln Lhe CK"+L0$6 I034J>.

1he except can be followed by an excepLlon Lype and a varlable name:

except <=M4$1,)3+ AG1$>, <!"#$>:
<K"+L0$6 I034J>

lf Lhe excepLlon ralsed ln Lhe <A6G I034J> maLches Lhe Lype <=M4$1,)3+ AG1$>, evaluaLe Lhe
<K"+L0$6 I034J> wlLh Lhe varlable <!"#$> referrlng Lo Lhe excepLlon ob[ecL.
L|brar|es
|mport <2)B6"6G>

lmporLs Lhe <2)B6"6G> lnLo Lhe yLhon envlronmenL, enabllng Lhe followlng code Lo use Lhe
deflnlLlons provlded ln Lhe <2)B6"6G>. yLhon provldes many llbrarles such as ur|||b LhaL we use
for downloadlng web pages ln get_page, and you can also lmporL your own code. As programs
geL larger, lL ls lmporLanL Lo organlze Lhem well lnLo separaLe flles.

Cther 8u||t-In Iunct|ons
Lva|
1he eva| funcLlon provldes a way Lo evaluaLe an lnpuL sLrlng as a yLhon expresslon:

eva|(<*&2%:A>) ! <8#9.1>

1he lnpuL ls a sLrlng LhaL ls a yLhon expresslon, Lhe value ls Lhe value LhaL sLrlng would evaluaLe Lo ln
yLhon.

Note: lL ls very dangerous Lo use eva| lf Lhe lnpuL sLrlng can be conLrolled by Lhe user. 1hls
would allow Lhe user Lo effecLlvely run any code Lhey wanL on your machlne!
1|me
1he Llme llbrary provldes funcLlons for obLalnlng and measurlng Llme.

|mport t|me

System t|me: t|me.c|ock()! <-./012>
ouLpuLs Lhe processor Llme ln seconds (a real number, lncludlng fracLlonal seconds wlLh llmlLed
accuracy)

Note: whaL clock means depends on your plaLform, and lL may noL provlde accuraLe Llmlngs on
all plaLforms. 1he t|me.t|me() meLhod may provlde beLLer Llmlngs on some plaLforms.

Das könnte Ihnen auch gefallen