Python 3 Quick Reference Guide
User Manual:
Open the PDF directly: View PDF .
Page Count: 2

Sequence Containers Indexing
Base Types
Python 3 Cheat Sheet
Latest version on:
https://perso.limsi.fr/pointal/python:memento
!"
escaped tab
escaped new line
Multilinestring:
Container Types
#$%$& #'$$& #&#&
( )$%$*$+$, )$* )*
-.
/0$01$0$,023
/4+053
/3
/$$$3
!"
)*
a priori #$%#$&
/4+$4+3
Non modiable values (immutables)
Variables assignment
'677)+*
+$8$6$$9
:8;:"<' $:8;:"<:
( ' %
())#$'
('*+,-
.expression with just comas →tuple
integer, %oat, boolean, string, bytes
Identiers
☺999'9+<'9=>
☹9+9-9
'76
'6
increment ⇔ '6'7
decrement ⇔ '6'
Conversions
for lists, tuples, strings, bytes…
)%*9/9%
)$*9/9 "$) "
)%%*9/9% "
),*9/9,
-)%%$*/% ) 0 /)1
)'*'"$'?'23%'
)'*/: ")' "$ (cf. formating on the back)
.1),*/@ -)@*/, 4%
()'*/: literal2")'
+)#$$,&*9/9A@
).*9/9#$$.&
-.)#)$1*$)$*&*9/9/0$013
)#$&*9/9/$3
separator sequence of /assembled
0B)#$$(-&*9/900(-
splitted on whitespaces/
-9199(.()*9/9#-$1$(.&
splitted on separator/of
$,$$()$*9/9#$,$$&
&$"/%$"(via comprehension list)
#)'*99'99)$$*&9/9#$$&
+()expression*
6#$9$9$9,$9%& #&
#&,
,
% , 5 #index&
positive index
negative index
%,
% ,
negative slice
positive slice
#start slice0end slice0step&
)*%
#0&C#$&
#00&C#$$%&
#0&C#$,&
#0&C#$$&
#0&C#$$$,& #0&C#,$%&
#0&C#$$,&
#0&C#$$$,$%&
Missing slice indication → from start / up to end.
On mutable sequences (list), remove with -9#0%& and modify with assignment #0,&6#%$%&
Conditional Statement
9>D60
996E-
9>F%0
996G-
0
996;.5
Boolean Logic Statements Blocks
parent statement0
99statement block 1…
999999H
99parent statement0
9999statement block2…
⁝
next statement after block 1
"6DFD6F666I6
≠=≥≤
9-9
99
9
)
)
)
one or other
or both
both simulta-
-neously
9logical condition0
9999statements block
statement block executed only
if a condition is true
)'%elifelif777 $
8else79$%#8
! 7
#&%
#&
⇒ last one
⇒ rst one
'6? «undened» constant value
Maths
9"67JKKKLJJ
:
): :
919(9$(:
)(K,*C:
.)J(K*C,:
M)*C9999N
>)JJ*C
.)%*C
)%*C
escaped9
. %oating numbers… approximated values angles in radians
)7%*JC
)*C
-)%$*C
(),$*C,
for variables, functions,
modules, classes… names
;<77
(ordered sequences of chars / bytes)
(key/value associations)
."26 - and return value of or
of (under shortcut evaluation).
⇒ ensure that and are booleans.
(boolean results)
66.6assignment to same value
multiple assignments
$6$values swap
$J6M
J$6M unpacking of sequence in
item and list
+
+
'%
hexadecimal octal
',
binary octal hexa
empty
-.)6$6,$465*
.keys=hashable values (base types, immutables…)
= > .congure editor to insert 4 spaces in
place of an indentation tab.
#00&C#%$,$$$&
#00&C#%$$&
1) evaluation of right side expression value
2) assignment in order with left side names
6
.)?name'%value
.immutables
On mutable sequences (list), remove with
-9#& and modify with assignment
#,&6%
-9'remove name '
@/!python3.5+numpy
.
0%1
8 immutable set
$)@*
.usual priorities
1.-
-..(+etc. (cf. doc)
Modules/Names Imports
9-9(9$99.
module .⇔le truc.py
→direct acces to names, renaming with as
(9- →acces via monmod.nom1 …
. "#)% python path0+(11
A
$
shallow copy of sequence
A
$
J6
K6
L6
@
.with a var x:
9)'*660?99'0
9)'*660?999'0
Exceptions on Errors
9Exception(…)
B))6 ,")6
+0
9999normal procesising block
'.(9Exception990
9999error processing block
normal
processing
error
processing
error
processing
null
.+ block for nal processing in all cases.

-/39/39/3)'$+$*
/selection0formatingIconversion3
(6
99
99
99
99,#4+&
99#&
Display
()56$$.O0$'$$$+7,*
("6
((69 " "
(-6 " '
(6+- "8 "
"$26!"
" *?"#$&
slices"&
Conditional Loop Statement
statements block executed as long as
condition is true
19logical condition0
9999statements block
969
969
199D690
9999969979JJ
9999969979
()0$*
initializations before the loop
condition with a least one variable value (here i)
s=
∑
i=1
i=100
i
2
.make condition variable change!
Iterative Loop Statement
statements block executed for each
item of a container or iterator
9var99sequence0
9999statements block
969P9'
.969
9.990
99999.96690
99999999.969.979
()-$.$*
C&D
Algo: count
number of e
in the string.
C&D
( $ !
( !0*1
969#$$$$$,$&
969#&
9-'99>))**0
99995969#-'&
9999959F9%0
99999999((-)5*
99999999#-'&969%
()-0$$0$*
Algo: limit values greater
than 15, memorizing
of lost values.
.beware of innite loops!
initializations before the loop
loop variable, assignment managed by for statement
Operations on Strings
)
Integers Sequences
Files
969().0*
.('$& $"
07! Conversions%% 17
>)%*/E >)$$*/F
>)$*/EG >)$%$%*/
>))seq**→ sequence of index of values in seq
.range provides an immutable sequence of int constructed as needed
>)[start,] end [,step]*
969()'$$.->6*
storing data on disk, and reading it back
")
(
('
(""
(@79'99
%text
les6
utf8 ascii
latin1 …
8
#
0H"%@1
8
"
)..*
)list of lines*
-)[n]*→ next chars
if n not specied, read up to end!
-)[n]*→ list of next lines
-)* → next line
19():*990
99999O0
9999Q processing ofline
7 (1 (1
.)* . ) 2I
J$6")'%) #
0)1 )"
!86
Function Denition
-9.)'$+$8*0
9999-.
9999# statements block, res computation, etc.
99999
0 81
%"
69?
."
%#!$in%# during %
0%#K#!”1
"
Function Call
969.)$7$J*
Generic Operations on Containers
read empty string if end of le
).*/
).*99').*99).*
-).*/ copy
599./%""091
).*/iterator0 !1
8().$.:*/iterator").9 !
).*/.
+).*/.
. $)
((-)val*
'-)seq* &
)idx$val* !
5)val*8'%val
(()[idx]*→value L !idx0 1
)*995)* *in place
/073),%*
/7,%
/0F3)$*
/999999
/'I3)'6*
/
.start n &pas)
(!60 !10"1
DFR6 8)'%
)6$.%- 0 1' %!@
M6S!"8! ">T"""0 12
)6@ L"
("6
ll char alignment sign mini widthprecision~maxwidth type
7space
Operations on Dictionaries Operations on Sets
9"6
9U9/0%1
9V9/
99R9/ N<*$ N7
9D9D69F9F69/
Operators also exist as methods.
-(-)d2*" *
Note: For dictionaries and sets, these
operations use keys.
Specic to ordered sequences containers (lists, tuples, strings, bytes…)
5-).*→ inversed iterator .J%/ " .7./
.-')val*→ position ..)val*→ events count
Operations on Lists
-#key&6value
-#key&→ value
-4+)*
-5)*
-)*
-.)*
-9-#key&
→iterable views on
keys/values/associations
,!"
-(()key[,default]*→ value
-(()*→ (key,value)
->)key[,default]*→ value
--)key[,default]*→value
(-)s2*9.(+)*
--)key*95)key*
-.-)key*9.)*
(()*
Loop Control
C$&D 6
9-'$599)*0
.) %26 D $"
6-9.)'$+$8$J>$6$6%$JJ4>*0
*args variable positional arguments (→tuple), default values,
**kwargs variable named arguments (→dict)
one argument per
parameter
storage/use of
returned value
Algo:
1)* '%
)*→position
reading/writing progress sequentially in the le, modiable with:
4)position[,origin]*
.)[taille]*O
Formating
Advanced:
*sequence
**dict
1)prex[,start[,end]]*
-1)suBx[,start[,end]]*9()[chars]*
.)sub[,start[,end]]*9()sep*→ (before,sep,after)
-')sub[,start[,end]]*9-)sub[,start[,end]]*
:)* tests on chars categories (ex. s.isalpha())
(()*999)*999)*999(.)*
.-)*999.(8)*999.)[width,ll]*9
B)[width,ll]*99B)[width,ll]*998)[width]*
.-)encoding*999()[sep]*99B)seq*
A
$
!
8%
@
Input
(9.(+
.(+.(+).*/%'"$
.(+-(.(+).*/ ""$
.%%
with parenthesis
'%% %
.)*
.
.
.text mode t by default (read/write ), possible binary
mode b (read/write +). Convert from/to required type!
4immediate exit
. next iteration
. block for normal loop exit.