用python做些事:表达式
stevezhou 2015.11.08
赋值
输入/输出
raw_input
input
print a,b
print >>file,a,b
print '%d,%d,%s'%(a,b,c)
print '{0} like {1}'.format('we','python')
print '{a} like {b}'.format(a='we',b='python')
if/else
for/while
while
while xxx:
statements1
if xxx:break/continue
else: #当while循环正常结束时执行,如果while循环以break结束,该句将不被执行
statements2
for
for x in objects:
statements1
if xxx:break/continue
else:
statements2
列表解析
d1=['a','b','c','d','a','c']
d2=[]
[d2.append(s) for s in d1 if not s in d2] #去除d1中重复元素,加到d2中
异常处理
try/except,
try/finally #无论异常是否触发,都将执行finally
raise error(message) #触发异常
assert condition,message #条件不满足时触发异常,打印信息,并终止程序