Python 3 Quick Reference Guide

User Manual:

Open the PDF directly: View PDF 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
Multilinestring:
Container Types
 #$%$& #'$$& #&#&
( )$%$*$+$, )$* )*
-.
/0$01$0$,023
/4+053

/3
/$$$3
 !"
)*
a priori   #$%#$&
/4+$4+3
Non modiable values (immutables)
Variables assignment
'677)+*
+$8$6$$9
:8;:"<' $:8;:"<:
( ' % 
())#$'  
('*+,- 
.expression with just comas →tuple


integer, %oat, boolean, string, bytes
Identiers
999'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/9A@
).*9/9#$$.&
-.)#)$1*$)$*&*9/9/0$013
)#$&*9/9/$3
separator sequence of /assembled
0B)#$$(-&*9/900(-
splitted on whitespaces/
-9199(.()*9/9#-$1$(.&
splitted on separator/of
$,$$()$*9/9#$,$$&
&$"/%$"(via comprehension list)
#)'*99'99)$$*&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>D60
996E-
9>F%0
996G-
0
996;.5
Boolean Logic Statements Blocks
parent statement0
99statement block 1…
999999H
99parent statement0
9999statement block2…
next statement after block 1

"6DFD6F666I6
=
9-9
99
9
)
)
)
one or other
or both
both simulta-
-neously
9logical condition0
9999statements block
statement block executed only
if a condition is true
)'%elifelif777 $
8else79$%#8
 ! 7
#&%
#&
⇒ last one
⇒ rst one
'6? «undened» constant value
Maths
9"67JKKKLJJ
 :
): : 
919(9$(:
)(K,*C:
.)J(K*C,:
M)*C9999N
>)JJ*C
.)%*C
)%*C
escaped9
. %oating numbers… approximated values angles in radians
)7%*JC
)*C
-)%$*C
(),$*C,
for variables, functions,
modules, classes… names
;<77
 (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
$J6M
J$6M unpacking of sequence in
item and list
+
+
'%
hexadecimal octal
 ',
binary octal hexa

empty
-.)6$6,$465*

.keys=hashable values (base types, immutables…)

 = > .congure 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$99.
module .⇔le truc.py
→direct acces to names, renaming with as
(9- →acces via monmod.nom1
.  "#)% python path0+(11
A
$

shallow copy of sequence
A
$ 

J6
K6
L6
@
.with a var x:
9)'*660?99'0
9)'*660?999'0
Exceptions on Errors
9Exception(…)
B))6 ,")6
+0
9999normal procesising block
'.(9Exception990
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
((69 " "
(-6  " '
(6+- "8   "
 "$26!"
" *?"#$&
slices"&
Conditional Loop Statement
statements block executed as long as
condition is true
19logical condition0
9999statements block
969
969
199D690
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
9var99sequence0
9999statements block
969P9'
.969
9.990
99999.96690
99999999.969.979
()-$.$*
C&D
Algo: count
number of e
in the string.
C&D
( $ !
(  !0*1
969#$$$$$,$&
969#&
9-'99>))**0
99995969#-'&
9999959F9%0
99999999((-)5*
99999999#-'&969%
()-0$$0$*
Algo: limit values greater
than 15, memorizing
of lost values.
.beware of innite loops!
initializations before the loop
loop variable, assignment managed by for statement
Operations on Strings
) 
Integers Sequences
Files
969().0*
.('$& $"
07! Conversions%%17
>)%*/E >)$$*/F
>)$*/EG >)$%$%*/
>))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
")
(
('
(""
(@79'99

%text
les6
utf8 ascii
latin1 …
8
 #
0H"%@1
8
"
)..*
)list of lines*
 
-)[n]*→ next chars
if n not specied, read up to end!
-)[n]*list of next lines
-)* → next line
19():*990
99999O0
9999Q processing ofline
7 (1(1
.)* . ) 2I
J$6")'%)  #
0)1  )"
!86
Function Denition
-9.)'$+$8*0
9999-.
9999# statements block, res computation, etc.
99999
0 81
%"
69?
." 
%#!$in%# during %
0%#K#!1
 "
Function Call
969.)$7$J*
Generic Operations on Containers
read empty string if end of le
).*/
).*99').*99).*
-).*/ copy
599./%""091
).*/iterator0 !1
8().$.:*/iterator").9 !
).*/. 
+).*/. 
. $)
((-)val* 
'-)seq* &
)idx$val* !
5)val*8'%val
(()[idx]*→value L !idx0 1
)*995)* *in place
/073),%*
/7,%
/0F3)$*
/999999
/'I3)'6*
/
.start n  &pas)  
(!60 !10"1
DFR6 8)'%
)6$.%- 0 1' %!@
M6S!"8! ">T"""0 12
)6@ L"
("6
ll char alignment sign mini widthprecision~maxwidth type
7space
Operations on Dictionaries Operations on Sets
9"6
9U9/0%1
9V9/
99R9/ N<*$ N7
9D9D69F9F69/
Operators also exist as methods.
-(-)d2*" * 

Note: For dictionaries and sets, these
operations use keys.
Specic 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*95)key*
-.-)key*9.)*
(()*
Loop Control
C$&D 6
9-'$599)*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)prex[,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]*99B)[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.

Navigation menu