Python Cheat Sheet
Python Cheat Sheet
Date Object
Date Time reptace(lyear,month,day)l tlmetuple()
toordlnalO w�kdayO(l isoweekday()O
Date Formatting
isocalendar()() isoformat{) _nr O_
Time Object ·ct i me{) strlt i meO %a Abbreviated weekday (Mon) 'l!iA Weekday (Monday)
%b Abbreviated rrionth hame (Nov) %8 Month name (November)
replace(l,hourL rnlnutel,,secondt microsetc;,ndl, 12lnfo]II]]) %c Date-<1nd time %d Day (leadlng zeros) (01 to 31)
isoforrnatll _str_O strf1imel) utcoffse1() c:!s1() 12name()
%H 24 hour (leading 2eros)(OO to 23) %1 12 hour (leading zeros)(01 to 12)
%) Day ofy!,!ar (001 tq366) %m Month (01 to 12) 'Mir.I Mioute (00 to59)
Datetime Object %p AM or PM %S Second(OOto617) %U Weeknumberl (00to53)
date() tlmet) t ·lmelZ() roordlnal() weekday() lsoweel\day() isocalendar() %w Weekday2 (0 to 6) %W Week number3 (00 to 53) %x Da(e
replace(lyear{, month(.dayt houri , minute!, secondL mlaosec_ond[ , tzlnfollJ}JIII) %X Time %y Year without c.entury (001099) %Y Year (2016)
astimezone(IZ) u1c_offsetO ds1<) tznameO limetuple() u1crln1erupl e{) %Z T ime zone (EST) %% A literal •%•character(%)
lsoforn,at{) _str_(l ctlme() 5lrftlme{)
sorted( C)- list sorted cop_v operations use ke_r"S. -- - - - -- -- - - -- ---- - - - -- - - -- -- - - -- ---- - - ---- - -f
,. • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • ,
val in c - boolean, 1ne1nbership operator in (abse11ce not in) : range ({start,} e,,d [,step]) Integers Sequences :
enumerate (c) - ireraror on ( il1dex, value)
zip(cl,c2___) - iterator on ruples containing ci iten1s at sa1ne index : � start default 0, fin not included in sequence_ pas sig11ed default 1 1
1
range(s)-01234 range(2,12,3)-25811 :
all(c)- True if all c ite1ns evaluated to true, else False : range(3, a)-34 5 6 7 range(20, 5 , -s)-20 15 10 ,
1
ti 1nodify original list Operations on Lists --• # staten1e11ls block, res con1p11tc1tio11, etc.
1st_ append ( ,•al) add ite111 at e11d / return res ◄- result value of the call, if no con1puted
add se que11ce of ite111s at e11cl result to return: return None
1st_ extend (seq) ii, paran1eters and all
1st_ insert (id..>.·, l'al) i11sert ite1n at i11dex variables of this block exist only in the block and during the function
1st_ remove (1 1al) re111ove first item witl1 value ,,al call (think of a "black box')
1st_ pop ([icl\ ·})-> 11al11e re111ove & retw·n iten1 at index idx (default last) Advanced: def fct (x, y , z, *args, a=3, b=S, **kwargs):
1st_ sort() 1st _ reverse () sort I reverse liste in place
""·-----------·-·-----·-· �·----·------······-----
�------------·----·-- ·---·-------------·------------
:- : -- - : - - ., - - :--- - - -'
, *n,gs vnrinble positionnl a1g11111ents (-tuple), defnulr values,
* */..,,va1gs l'ariable na111ed a1gu111enrs (--J diet)
' ---------------------- - ---, -------------------------�, ·----------------------------------------,
Boo Iean Log1c • •'
, Statements Blocks , 111od11le truc¢:::>file true. PY Modules/Names Imports :
: Co1n1)arators: < > <= >= == ! = : from monmod import noml,nom2 as fct •
• (boolean resulrs) < > * pare11.t stc1te111e11t: • -+direct acces to na,nes1 rena111ing 111ith as :
I
both sinntlra- - stc1te111e11t block 1 ... I .
import monmod -+acces via monmod. nom1 ...
-·
--=
, a and b logical arid •
: :
, -neously = •
• � Yb n1odules and packa.ges searched in python parh (cf sys. path)
1
. . . l'1<:aflo11
. � b,I". • _ _ _ _ _ _ _ _ _ _ _ _ _ _ T,• _ _ _ _ ,
i = 1
I
e1 ore tIte loop
� . ••
•
:
.
,
_
- Some text } 1111rzal1zatro11s before the loop
- '---
s
• • =
co11dzt1011 ,vzth a least one variable value (here i)
11 11 • • • • •
� while
. cnt 0
�•
--'
Algo: �•
s = s + i** 2
� i <= 1 0 0: i= lOO loov, variable, assig111ne11t 111a11aged by for state1ne11t
for 1c"1 in s:
:•
i = i + 1 l'b 111ake co11ditio11 variable change ! -
_ , ,2 1
•
..C)
� 5 L..,.; l if c == "e " : Algo: count >:
-�
cnt = cnt + 1 g.
- ------ - --- ------- -------- -------------------Display
-----
print (" sum: " , s) i=l : niunber of e
, - • - • • • - • • • • • • - - - - - - - - - • • - - - - - - - - - - • • - - • - - • - - - - - - - - _.
' ... pri·nt ("found", cnt, " ' e ' " ) ,·,,, tlte s11·1·11g. _Q
,.............
• print ( 11 v= " 3 " cm •· " x " " y +4 ) ' 1oop on d.1ct;set ¢=> 1oop on keys sequences _;:,--,
:';::l
•
f f f f f f
--------------------------------------------------------------------------------------------------------
use slices to loop on a subset of a sequence s
I
� ��-- - -t-✓�
�
I "8
: ite1ns to display : literal values, variables, expressil11s Go over sequence's i11dex �
print options: □ 1noclify ite111 at index §
□ access ite1ns around iI1dex (before / after) :='.
□ sep=
□ end= \n 11
ite1ns separator, default space
II
11
II
-
del x re1110Pe 11a111e x '
- - -- -
[int (x) £or x in ( ' 1 ' , ' 29 ' , ' -3 ' ) ] � [1 , 29 , -3]
- - --- - - - ---- -- -� -- - - - - ,
- -------------------------------------------------- ------------------------------------------ �
a
._.
Conditional Loop Statement
staten·1ents block executecl fts lo1tg fts � : stc1te1 rie1its block v:ecuted for eftc/1 Iterative Loop Statement :
conclitio,i is tru e : : ite1ri of a co,itainer or iterator . :
.--�
for var in sequerice :
#• • • • • • • • • • • • • • L- • • • • • t
. . . l'1<:aflo11
. � b,I". • _ _ _ _ _ _ _ _ _ _ _ _ _ _ T,• _ _ _ _ ,
i = 1
I
e1 ore tIte loop
� . ••
•
:
.
,
_
- Some text } 1111rzal1zatro11s before the loop
- '---
s
• • =
co11dzt1011 ,vzth a least one variable value (here i)
11 11 • • • • •
� while
. cnt 0
�•
--'
Algo: �•
s = s + i** 2
� i <= 1 0 0: i= lOO loov, variable, assig111ne11t 111a11aged by for state1ne11t
for 1c"1 in s:
:•
i = i + 1 l'b 111ake co11ditio11 variable change ! -
_ , ,2 1
•
..C)
� 5 L..,.; l if c == "e " : Algo: count >:
-�
cnt = cnt + 1 g.
- ------ - --- ------- -------- -------------------Display
-----
print (" sum: " , s) i=l : niunber of e
, - • - • • • - • • • • • • - - - - - - - - - • • - - - - - - - - - - • • - - • - - • - - - - - - - - _.
' ... pri·nt ("found", cnt, " ' e ' " ) ,·,,, tlte s11·1·11g. _Q
,.............
• print ( 11 v= " 3 " cm •· " x " " y +4 ) ' 1oop on d.1ct;set ¢=> 1oop on keys sequences _;:,--,
:';::l
•
f f f f f f
--------------------------------------------------------------------------------------------------------
use slices to loop on a subset of a sequence s
I
� ��-- - -t-✓�
�
I "8
: ite1ns to display : literal values, variables, expressil11s Go over sequence's i11dex �
print options: □ 1noclify ite111 at index §
□ access ite1ns around iI1dex (before / after) :='.
□ sep=
□ end= \n 11
ite1ns separator, default space
II
11
II