License Creative Commo ns Attribution 4 . ---_.Bas----- -- y---_on... --_ ...ea --- _.- ee... --_ .. ---_ hlll's:lll'erso.limsi.jrlpmnlullpylhol1:mcmento --_ .. ---_ .. ---_ .. ---_ .. ---- . il1lcg<'r, floor, boolean, sIring, bytes e Typ e s • ordered sequences. fast index access. repealable values Containe r Typ e s : int 783 0 - 1 9 2 Ob OlO 0 0 6 4 2 Ox F3 l i st [ 1, 5 , 9] [ " x " , U , S . 9] [ "mo t "] ili : /lull biliary 0<1al !leu, " t u p l e ( 1 , 5 , 9) 11 , "y " ,7 .4 ( " mo t " ,) 1') f l o at 9 .23 0 .0 -1 .7~ /' ~ bool True False x l O'" N()fl mf:!iji(/b1e '~Io,es (immul"bies) ~ expression ...;/h jus(w mus ---o t up l e ~ s t r bytes (ordered sequences of chars I bytes) ,'" s t r " One\n Two" M"ltiline string " b '~ " eJmped n?ivline "" "x\ ty \tZ • key cuntainers. no a priori order. fast key acres. each key is unique ' I~' 1 \t2 ~3 " "" diL1......ry d i et { " ke y " : "v a 1ue " } dict (a =3 , b=4 , k=" v " ) escaped • escapedtab (/'Ryll'alwll"'locimion,<) {1: " one" , 3 : "three " , 2 : " t wo" , 3 . 1 4 : " n" } b yte s b "to t o \xfe \775 " rollf<1 ioo set { " ke y 1 " , "ke y2" } {1 , 9 , 3, O} set 0 "".mdecu,,,,1 oCUli f ;mmul"bies ~ key,<,.(UJ.Sh"b!e "l1lue,< (oose types; imlllw"b!es__ .) f ro:<e ns et i/lIl""tl1ble set elll!" y -... ~
: i o; ~;;ri~'i,;:'S~ ill~~io,~s: ••••••• ·Id~ ~ ti fi ~ ~s·· ' -i·~; ( '~ ~ ~ ~ )
' • int ( " 3f" , 1 6 ) ..... 63 can specify integer number base in 2 param eter • a.~zA ..Z followed by a.~zA ..Z O ~. 9 . 1 1 d . I " diacri t i~ allowed but should be-;voidcd ~ nt ( 5 .56 ) ..... 5 truncate ectma part cla nguage kcywords forbidden f l o a t ( " - 1 1 . 2 4e 8 " ) ..... -112 4 0 ~ 0 0 0 0 . 0 . . . o lower/UPPER case discrimination r ound ( 1 5 . 5 6 , 1 ) ..... 1 5 , 6 rounding to I dec imal (0 dec imal ..... integer num be r)
..... Q. ~: r: Q a toto x1 Y_lnaX Bi90ne
~:: s t r (x) ..... "
b o o l ( x) False for null x , em pty conta iner x . None or False x : True for other x re prese nta tion string of x for d isp lay (ef formcuing 0 11 Ih e back) .. . . . . . . . . . . . . . . . . . . . . . . . . ...... . chr ( 6 4 ) ..... '@' ord ( ' @' ) ..... 64 code ..... char Va riab les a s s ign m ent r e pr (x) ..... "c." literal representat ion SIring o f x I ) ",""f,"'tio" of right ,ide expres,';o" m ille 2) "ssigll/''''miJ, order ...;th lefl s;de "" mes bytes ( [ 7 2 , 9 , 6 4] ) ..... b' H\t @, ! a.'i., ign men t ~ bin dinl:0f a n"lIIe w i th am/"e l i s t ( " a b c ") ..... [ ' a ' , 'b' , ' c ' ] x = 1 . 2 + 8 +s i n (y ) d ict( [ ( 3 , " t h r e e " ) , ( l , " o n e ") ] ) ..... { 1 : ' o n e ' , 3: ' t h r e e ') a =b=c= O "ssignmmllOsl1me ""Iu.. set ( [ "one " , "two" ] ) ..... { ' one ' , 'two ' } y , Z , r = 9 . 2 , - 7 . 6 , 0 mult;ple ,mignmrnts separator s t r an d seq uence of s t r __ assemb led s t r a ,b=b ,a ""Iues'....ap . j o i n ( [ ' t o t o ' , ' 1 2' , 'pswd'J ) __ ' t o t o : 1 2 : p s wd ' a , "'b = s e q } uIJ!>tJcki"goj ,I'equetlce 'n st r spliced 0 11 whitcspoccs ---'> l i s t of s t r "'a, b=s e q lIemandllS/ x += 3 x - =2 incremen l ,,", x=x+3 decremem,,", x =x - 2 ' " wo r d s with spaces " . s p l i t ( ) __ [ ' wo r d s ' , ' wi t h ' , ' s p a c e s' J : ~~ : : s t r spliced 011 separator s t r ---'> l ist of s tr I" :: " 1 , 4 , 8 , 2 " . s p l i t (" ," ) ---'> [ ' 1 ' , ' 4 ' , ' 8 ' , ' 2 'J .. x = No n e ~ ""defined H'OI~<ll1m 1"(1/"" \ .. : : seq uence of one type ..... list of another type ( via comprehension list] de l x remo nam.. X [ i n t (x ) for x in ( ' 1 ' , ' 2 9 ' , ' - 3 ')] ---'> [ 1 , 2 9 , - 3] .. ....~ ~ ~ ~ ~ ~ ~ ~ ~ .. ... ~ :: ~.. --- .... -- ... --_ ... ... --_ .. ~ ~ ~ ~.- .. ---- -~_ ---_ .. ---_ .. ---_ ... .. -.-_ -- ~· · · ···· · · ····· · ···· ·· ··· · ~ ~j~ !0~!;~~;~~~~ bY~~ ~·· · · · ·· · · · · · · · · · · · · · · · · · ·S eque;ce C on i~ n e rs lndexj;g · n"gmil'eimlex -5 -4 -3 -2 i -1 Items count Individual access to item s via 1 s t [imler] IJO"it;"eil"le.x 0 i 1 , 21 3 : 4 len ( l s t ) ..... 5 1st [ 0] 10 ~ jirsl on.. 1 st [ 1 ] 20 lst = [ l O,i 20 ; 3D , 40 ; 50] lst [ - 1 ] 50 => Iust "' w lst [ - 2 ] 40 posilh'e sue .. o ~ ~ 3 i 4 !! index from 0 (here from 0 1O 4) 011 mutabte sequences (l i s t), remove willl 11egtJli\"e slice ~5 ~4 "':3 ~,2 -). del 1 st [ 3 ] and modify wilh a.\·sig mnem lst [ 4 ] :z25 Access to sun-sequences via 1s t [start slice :end slice : sTep } lst [ : - 1 ] .. [10 ,20 ,30 ,40] lst [ : : - 1 ]" [ 5 0 , 4 0 , 3 0 , 2 0 , 1 0 ] lst [1 : 3 ] .. [ 2 0 , 3 0] 1st l : 3 ] " [1 0 , 20 , 30] lst [ 1 : - 1 ] --- [ 2 0 , 3 0 , 4 0 ] lst [ ; : - 2] " [ 5 0 , 3 0 , 1 0 ] lst [ - 3 : - 1 ] .. [30 ,40] lst [ 3 : ] " [ 4 0 , 5 0 ] 1st [ : : 2 ] .. [10 ,30 ,50] 1st [ : ] ... [ 10 ,20 ,30 ,40 ,50 ] .,hal/ow coPY "f ' etjllence Missing slice indication ..... from "tarlllll' to end. Ott mutable Seqlletlce" (l i s t). remove ""ilh de l 1 st [ 3 : 5 J and modif y witti assignment 1s t [1: 4 ] = [15,2 5 ] ~--• .• ~------- .•.• -.~----- r -·B·~~~~~-'l~~i~·· 7- -; ~ '; ':.':.'; '; ~ ~ ~~~;~~;~t~··~I~~~~ ~ :;~-::T:;/~~~:·;::;;'i:~~~:~~~~-. -.-...._~~~~~~.;~.~;.;;,~~·i~~~~~· : Comparators: < > <= >= == ! = :: f rom monmod i mpo r t noml , n0m2 a s fct (booTeall results) 5' ~ .. lrent sfalemefll : :: d,reeI acces to names,'""", ning "'illt as a and b logical and /lOIh s;m"II,,· ~ . stalnl1em block I . :: import monmod </CCl'S ,.;" IIlOlllllOd . noml . ·neow;fy - : " ~ modoles andpackages searched In /I.wlton pmh (cf sys . path) a or b logical or :;e;;ltOlheT -§ parent stalem ellt : I ~~I~I~~I;'~ 11~,I ~~,~~~;;nj)~ : : : : C~~di;i~~~I: S~~~~~~~ ~ t pitfnll: and "nti or rel"m ud.tlt: of a 0' of b(undCl,ho,tcwemluw'(Hl). ' => e",,,,,e Iltal a "nd b "re boolea"s. L """ _: j SrGtement bIOCk2 .. : next slu{emel11 oftcr blod: I u: d IJ a con ilion is true if logical condition: __ J statements block $" no ~ ~ '00 • •
not a logical not "I
True } Can go with several elif. ebf... and only one Fa l s e True and False constants ~ COl1jigure edilor10il1.w't 4 spa cesin final else. Only the block of first true it age <..18 : ' •• _•••• __ ••••• _•••• _...... • ~ •••• psoceof __"" indentationtab. •••• _••••• _•••• __ . ' condition is executed elits t ate· "Ki d" a9 8 >65 : .. ~~j,(;,;;n~ ~:,:;.,;s.~ -,,;;r:u;i';"~':d',;,,:,;S~ ~ •• -. :,~i~ ;;,~(;'ii,,:'; ----.. -- Ma'ths ~ : t " 'ill! a ,'"r x: s t at ew"Reti r e d": Opcrators: + - * I II % ** from mat h i mpo rt sin , p L . :: i t boo1 (x} Tru . : ~ i t x : el se : " . ,, : . . x .. . . ' I ' , i t boo1 (x} Fa 1.e : ~ i t not x : s tate" Ac t >.ve , Priority ( . ' I I a. s in (p i 4 ) "0 .707.. •; ,':':':':'-::':':':':':':':':':' :':':':':':':':':':':':':':":,':':':':':':':':':':' . . mlcgcr -;- -;- tem.1! ndcr cos ( 2"' p i / 3 ) " - 0 _4 9 9 9 Signaling an error: Exc e p tion s on Errors @..... matn x x 1'l"",,"J,5. n UlOpy sqrt (8 1) " 9 . 0 -{ r a i s e £xceplio,,( .. ) Errors processing: ( 1 + 5 , 3l* 2 ~ 1 2 , 6 10g (e * * 2 ) .. 2 .0 try : abs (- 3 , 2 ) ~ 3 . 2 ceil (1 2 _5 ) .. 13 ---I norlllol pnxesising block round ( 3 , 5 7 , 1 ) ~ 3 . 6 fl o or (1 2 . 5 ) " 1 2 e xcept £(cepliOIl a s e : pow( 4, 3 ) ~ 6 4 . 0 modules math . statistics. random, ---I error processing block •••••••••~.!'!I!a! !nJ~r!.'~e~ ••• • • • .?!:~ah !:~c.t.i~~! .•n~:t..e~c.. (~f~ ~~). ' _ ~ •• _ ~ ~ ~i!,~~11!!::,![~~n!~t'~~ i~ ~t~~.~ • • • _ ~ • • • . ~.