首先通过help('keywords)列出Python有哪些关键字
as else import raise
assert except in return
break exec is try
class finally lambda while
continue for not with
def from or yield
del global pass
1. global
作用是将局部作用域的变量声明为全局的,例如:
def func(): global a print a a=11 a=1
func()
执行后,输出全局变量值,1
func()再次执行后,输出被修改的全局变量值11
2. yield
每执行到yield返回一个中间值给调用者并暂停执行,以后继续执行同样操作,占用更少内存
def yieldcase(): i=0 while i<10: yield i**2 i+=1 for i in yieldcase(): print i,3. with
通过由上下文管理器定义的方法来封装执行块
path=''
with open(path,'r') as file: print file.read()