Sunteți pe pagina 1din 1

Python Language & Syntax Cheat Sheet

Python is white-space dependent; code blocks are indented 4 spaces (not tabs)
Variable Assignment
integer = 1
string = string
unicode_string = uunicode string
mutli_line_string = multi-line
string

tuple = (element1, element2, element3, )


list = [ element1, element2, element3, ... ]
dictionary = { key1 : value1, key2 : value2, ... }
dictionary[key = value
class_instance = !lass"ame#init_args$
Frequently Used Built-in Tyes
True False !one
str uni"ode int
#loat list di"t
Other than True, False and !one, these can also be sed as
!nctions to e"plicitly cast a #ale to that type
Fun"tions
de# %unction_name#arg1, arg2,
key&ord1=val1, key&ord2=val2, ...$:
'%unction (ody)
return return_value
e$%$
de# &y'!nction(", y, ()*):
s& ) " + y + (
return s&
&y'!nction(,, -) . /
&y'!nction(,, -, /) . 0
&y'!nction(,, -, y)4) . 1
Classes
"lass !lass"ame#*uper!lass$+
class_varia(le = static_value
de# ''init''(sel#, value1, 2$$$3):
sel!$instance_varia(le1 = value1
sel!$instance_%unction()
de# instance_%unction(sel#, arg1, '...)):
'%unction (ody)
return return_value
e$%$
"lass 4y5lass(ob$e"t):
o!!set ) ,
de# ''init''(sel#, #ale):
sel!$#ale ) #ale
de# %et'o!!set'#ale(sel#):
return 4y5lass$o!!set +
sel!$#ale
4y5lass$o!!set . ,
c ) 4y5lass(-)
c$#ale . -
c$%et'o!!set'#ale() . /
%morts
imort module
#rom module imort class, %unction, varia(le
Frequently Used String &aniulations
string1 + string1 6str7 + 6in%7 . 6strin%7
89s9s8 9 (string1, string2) 69s9s7 9 (6s7, 6%7) . 6s%7
string$split(8delim8, limit) 6s:%7$split(6:7) . [6s7, 6%7]
string$strip() 6 strin% 6$strip() . 6strin%7
string$startswith#,pre%i-,$ 6str7$startswith(6s7) . ;re
su(string in string 6str7 in 6strin%7 . ;re
rint string
List Comrehension
[ value #or value in list i# condition ]
e$%$
[" !or " in [,,-,/,4,<,0,1,=,>] i! " 9 - )) *] . [-,4,0,=]
(y !ottage .a(s #/ttp+00cottagela(s.com$
%or 1ev21 #/ttp+00&&&.dev2d.org0$
A""essing Variable Values
value = dictionary[key]
value ) dictionary$%et(key, de%ault_value)
value = list[inde-] e$%$ [<,0,1][-] . 1
value = string[start:end] e$% 6strin%7[*:/] . 6str7
value = list[start:end] e$%$ [,,-,/][,:-] . [-]
value = !lass"ame$class_varia(le
value = class_instance$instance_varia(le
value = class_instance$%unction#args$
Comarisons
value1 )) value2 6str7 )) 6str7 . True
value1 ?) value2 6str7 ?) 6str7 . False
value1 2 value2 , 2 - . True
value1 2) value2 - 2) - . True
value1 3 value2 - 3 / . False
value1 3) value2 / 3) / . True
value is [not' !one
value in list , in [-,/,4] . False
isinstan"e(class_instance, !lass"ame)
Basi" Arithmeti"
i ) a + b i ) a - b
i ) a : b i ) a @ b
i ) a 9 b e$%$ ,, 9 / . -
Comments
"""
3ulti-line comment
"""
# .ine !omment
Control Flo(
i# conditional:
'(ody)
eli# conditional:
'(ody)
else:
'(ody)
e$%$
i# i )) 1:
rint 6se#en7
eli# i )) =:
rint 6ei%ht7
else:
rint str(i)
#or value in list:
'(ody)
"ontinue
brea)
e$%$
#or i in [,, -, /, 4]:
i# i )) -: "ontinue
i# i )) /: brea)
rint i
(hile conditional:
'(ody)
"ontinue
brea)
e$%$
(hile ;re:
rint 6in!inity7
*x"etions
try+
'(ody)
raise A"ception()
ex"et A"ception as e+
'e-ception /andling)
#inally+
'clean-up)
e$%$
try+
database$pdate()
ex"et A"ception as e+
lo%$error(e$&s%)
database$abort()
#inally+
database$co&&it()
File & Path &aniulation
imort os 4 import t/e os module %irst
os$path$Boin(pat/_segment1, pat/_segment2, $$$)
os$path$e"ists(pat/)
os$listdir(directory_pat/)
os$re&o#e#%ile_pat/$
os$r&dir#directory_pat/$
%ile ) open(pat/, 8rw8)
%ile$read()
string$write#,string,$

S-ar putea să vă placă și