sschaub essential-python

До загрузки: 30 сек.



Благодарим, что скачиваете у нас :)

Если, что - то:

  • Поделится ссылкой:
  • Документ найден в свободном доступе.
  • Загрузка документа - бесплатна.
  • Если нарушены ваши права, свяжитесь с нами.
Формат: pdf
Найдено: 28.08.2020
Добавлено: 30.09.2020
Размер: 0.06 Мб

Esse ntia l P yth on C heat S heet
by
ssch aub
v ia
ch eato gra phy.c o m /1 000/c s/3 74/
D ata T yp es
In te ger
-2 56, 1 5
Flo at
-2 53.2 3, 1 .2 53e-1 0
S tr in g
" ​Hel
​lo ", 'G oodbye ', "
​" ​" ​Mul
​til
​in e
​" ​" ​"
B oole an
Tru e, F als e
Lis t
[
va lu e
, ... ]
Tuple
(
va lu e
, ... )
D ic tio nary
{
ke y
:
va lu e
, ... }
S et
{
va lu e
,
va lu e
, ... }
P are nt
​hese s u su ally o ptio nal
C re ate a n e m pty s e t w it h s e t( )
S ta te m en ts
If S ta te m
​en t
if
exp re
​ssio n
:
​ ​sta
​te m ents
elif
exp re
​ssio n
:
​ ​sta
​te m ents
els e :
​ ​sta
​te m ents
W hile L oop
w hile
exp re
​ssio n
:
​ ​sta
​te m ents
For L oop
fo r
va r
in
co lle
​ctio n
:
​ ​sta
​te m ents
C ountin g F or L oop
fo r i in r a nge(
​st
​art
,
end
[,
ste p
]) :
​ ​sta
​te m ents
( sta rt
is in clu ded;
end
is n ot)
A rit h m etic O pera to rs
x + y
add
x - y
su btr a ct
x * y
mult ip ly
x / y
div id e
x % y
modulu s
x * * y
x
A ssig nm ent s h ortc u ts : x
op
= y
E xa m ple : x + = 1 in cre m ents x
Com paris o n O pera to rs
x< y
Less
x < = y
Less o r e q
x > y
Gre ate r
x > = y
Gre ate r o r e q
x = = y
Equal
x != y
Not e qual
Boole an O pera to rs
not x
x a nd y
x o r y
Excep tio n H an dlin g
tr y :
​ ​sta
​te m ents
exce pt [
exce ptio n ty p e
[ a s
va r
] ]:
​ ​sta
​te m ents
fin ally :
​ ​sta
​te m ents
Convers io n F unctio ns
in t(
e ​xp r
) Conve rts
exp r
to in te ger
flo at(
​ exp r
) Conve rts
exp r
to flo at
str (
e ​xp r
) Conve rts
exp r
to s tr in g
ch r(
num
) ASC II c h ar
num
Str in g / L is t / T uple O pera tio ns
le n(
s ) le ngth o f
s
s [ i ] i th it e m in
s ( 0 -b ase d)
s [ s ​ta rt
:
end
] slic e o f
s fr o m
sta rt
( in clu ded) to
end
( e xclu ded)
x in
s Tru e
if
x is c o nta in ed in
s
x n ot in
s Tru e
if
x is n ot c o nta in ed in
s
s +
t th e c o nca t
​enatio n o f
s w it h
t
s *
n n c o pie s o f
s c o nca t
​enate d
so rte d
​( s
) a s o rte d c o py o f
s
s .in
​dex
​(
i ​te m
) posit io n in
s o f
it e m
More S tr in g O pera tio ns
s .lo
​wer()
lo w erc a se c o py o f
s
s .r e
​pla
​ce (
​old
,
new
) co py o f
s w it h
old
r e pla ce d
wit h
new
s .s p lit (
delim
)
lis t o f s u bstr in gs d elim it e d b y
delim
More S tr in g O pera tio ns ( c o nt)
s .s t
​rip ()
co py o f
s w it h w hit e sp ace tr im med
s .u p
​per()
upperc a se c o py o f
s
See a ls o
http :/
​/d o
​cs.p
​yt
​hon.o r
​g/l
​ib r
​ary
​/s t
​dty
​pes.h t
​ml#
​str
​in g
​-
me
​th ods
Muta tin g L is t O pera tio ns
del
ls t
[ i ] Dele te s
i th it e m fr o m
ls t
ls t
.a
​pp
​end
​( e ) Appends
e to
ls t
ls t
.i
​ns
​ert
​( i ,
e ) In se rts
e b efo re
i th it e m in
ls t
ls t
.s
​ort( )
Sorts
ls t
See a ls o
http :/
​/d o
​cs.p
​yt
​hon.o r
​g/l
​ib r
​ary
​/s t
​dty
​pes.h t
​ml#
​ty p
​ess

eq-
​muta ble
Dic tio nary O pera tio ns
le n(
d ) Num ber o f it e m s in
d
del
d [ ke y
] Rem ove s
ke y
fr o m
d
ke y
in
d Tru e if
d c o nta in s
ke y
d .k e ys()
Retu rn s a lis t o f k e ys in
d
See a ls o
http :/
​/d o
​cs.p
​yt
​hon.o r
​g/l
​ib r
​ary
​/s t
​dty
​pes.h t
​ml#
​map
​pi
n ​g-t
​yp e
​s-d ic t
Functio n D efin i
​tio ns
def
nam e
​( a ​rg 1
,
arg 2
, ...) :
​ ​st
​ate
​ments
​ ​re tu rn
exp r
Envir o
​nm en t
sys.a rg v
Lis t o f c o m mand lin e a rg um ents
(a rg v[0 ] is e xe cu t
​able )
os.e nvir o n
Dic tio nary o f e nvir o
​nm ent
va ria ble s
os.c u rd ir
Str in g w it h p ath o f c u rre nt
dir e cto ry
im port s ys; p rin t(
​sys.a r
​gv)
​ ​ ​ ​ or
fr o m s ys im port a rg v; p rin t(
​arg v)
By
ssch au b
ch eato gra phy.c o m /s sch aub/
Publis h ed 2 1st M ay, 2 012.
Last u pdate d 2 nd J u ne, 2 014.
Page 1 o f 2 .
Sponso re d b y
Cro ssw ord C heats .c o m
Learn to s o lv e c ry p tic c ro ssw ord s!
http ://c ro ssw ord ch eats .c o m
1
2
1
2
y

Esse ntia l P yth on C heat S heet
by
ssch aub
v ia
ch eato gra phy.c o m /1 000/c s/3 74/
S tr in g F orm attin g
" ​Hello , { 0 } { 1 }" .fo
​rm a
​t( " a
​be", "
​jo n
​es")
H ello , a be jo nes
" ​Hello , { fn } { ln }" .f
​orm
​at(
​fn =
​" ​abe
​", ln = "
​jo n
​es")
H ello , a be jo nes
"Y ou o w e m e $ {0 :,.2
​f}
​".fo
​rm a
​t( 2
​534
​22.3 )
Y ou o w e m e $ 253,4
​22.3 0
now = d ate ti
​me.n
​ow ()
'{ :% Y-
​%m-% d % H:% M:
​%S}
​'. f
​orm
​at( n ow )
2012-
​05-1 6 1 5:0 4:3 3
S ee a ls o
http :/
​/d o
​cs.p
​yt
​hon.o r
​g/l
​ib r
​ary
​/s t
​rin
​g.h
​tm l
​#fo
​rm a
​t-
s ​pec
​if i
​ca t
​io n
​-m i
​ni-
​la n
​guage
U sefu l F unctio ns
exit (
co de
)
Term in ate p ro gra m w it h e xit
co de
ra w _in
​put
​("
p ​ro m
​pt
​")
Prin t
pro m pt
a nd r e adlin e() fr o m s td in
U se in put(
​" ​pr
​om p
​t ") in P yth on 3
C ode S nip pets
Loop O ver S eq uen ce
fo r in dex, v a lu e in e num er
​ate
​(s e q):
​ ​pri
​nt(
​"{} : { }" .f
​or
​mat
​(in dex, v a lu e))
Loop O ver D ic tio
​nary
fo r k e y in s o rte d
​(d ic t) :
​ ​pri
​nt(
​dic
​t[k e y])
R ead a F ile
w it h o pen(" f
​ile
​nam
​e", "
​r" ) a s f:
​ fo r lin e in f:
​ ​ ​ lin e = lin e.r
​str
​ip (
​" ​\n ") # S tr ip n ew lin e
​ ​ ​ ​pri
​nt(
​lin e)
O th er R efe re n ces
http :/
​/r g
​ru e
​t.f
​re e.fr /
G re at P yth on 2 .x Q uic k R efe re nce
http :/
​/w w
​w.c
​hea
​to g
​ra p
​hy.c
​om
​/d a
​ve c
​hil
​d/c
​hea
​t- s
​hee
​ts /
​pyth on/
M ore P yth on C heats h eet G oodness

By
ssch au b
ch eato gra phy.c o m /s sch aub/
Publis h ed 2 1st M ay, 2 012.
Last u pdate d 2 nd J u ne, 2 014.
Page 2 o f 2 .
Sponso re d b y
Cro ssw ord C heats .c o m
Learn to s o lv e c ry p tic c ro ssw ord s!
http ://c ro ssw ord ch eats .c o m
1
1

Сообщить о нарушении / Abuse

Все документы на сайте взяты из открытых источников, которые размещаются пользователями. Приносим свои глубочайшие извинения, если Ваш документ был опубликован без Вашего на то согласия.