1.语句
语句 | 角色 | 例子 |
---|---|---|
赋值 | 创建引用值 | a.b,c='good',1,'nihao' |
调用 | 执行函数 | log.write('spam,ham') |
打印调用 | 打印对象 | print 'hello world' #python2 print('python3') #python3 |
if/elif/else | 选择动作 | if "python" in text: print(text) |
for/else | 序列迭代 | for x in mylist: print(x) |
while/else | 一般循环 | while X>Y: print('hello') |
pass | 空占位符 | while True: pass |
break | 循环推出 | while True: if exittest(): break |
continue | 循环继续(后续操作跳过) | while True: if skiptest(): continue |
def | 函数和方法 | def f(a,bc,c=1,*d): print(a+b+c+d[0]) |
return | 函数结果返回 | def f(a,bc,c=1,*d): return a+b+c+d[0] |
yield | 生成器函数(生成之后能够迭代) | def gen(n): for i in n:yield i*2 |
global | 命名空间 | x='old' def function(): global x,y;x='new' |
nonlocal | 命名空间(python3.0及以上) | x='old' def function(): nonlocal x;x='new' |
import | 模块访问 | import sys |
class | 声明对象 | class Subclass(Superclass): staticData = [] def method(self):pass |
try/except/finally | 捕捉异常 | try: action() except: print('action error') |
raise | 触发异常 | raise EndSearch(location) |
assert | 调试检查 | asset X>Y, 'X too small' |
with/as | 环境管理器(2.6) | with open('data') as myfile: process(myfile) |
del | 删除引用 | del data[k] del data[i:j] del obj.attr del variable |
2.关键字和表达式
3.赋值语句
4.打印语句