Python有哪些关键字
Python常用的关键字
and, del, from, not, while, as, elif, global, or, with, assert, else, if, pass, yield, break, except, import, print, class, exec, in, raise, contiue, finally, is, return, def, for, lambda, try
1.and , or
and , or 为逻辑关系用语,Python具有短路逻辑,False and 返回 False
不执行后面的语句, True or 直接返回True,不执行后面的语句
2.del
删除变量
if __name__=='__main__':
a=1 # 对象 1 被 变量a引用,对象1的引用计数器为1
b=a # 对象1 被变量b引用,对象1的引用计数器加1
c=a #1对象1 被变量c引用,对象1的引用计数器加1
del a #删除变量a,解除a对1的引用
del b #删除变量b,解除b对1的引用
#print a #运行此句出错,name 'a' is not defined,说明 del 删除变量a
print(c) #最终变量c仍然引用1
print (c)
而列表本身包含的是变量,例:
list = [1,2,3]
# 包含list[0],list[1],list[2]
# 并不包含数字1,2,3
所以
if __name__=='__main__':
li=[1,2,3,4,5] #列表本身不包含数据1,2,3,4,5,而是包含变量:li[0] li[1] li[2] li[3] li[4]
first=li[0] #拷贝列表,也不会有数据对象的复制,而是创建新的变量引用
del li[0] # 列表本身包含的是变量,del 删除的是变量。
print (li) #输出[2, 3, 4, 5]
print(first) #输出 1
3.from
from引用模块时会用到,例:
from sys import argv
# 从sys中导入argv
from sys import *
# 将sys中所有东西都导入
import sys
# 导入sys,当需要sys中内容时,需sys.argv而from sys import *
#不用每次都重复输入'sys.'
4.golbal
golbal为全局变量,但当单个函数中出现同一变量名时,在单个函数中为局部变量
golbal q
q = 66
print ("q=", q) #q = 66
def function():
q = 3
print ('q =',q)
function() # q = 3
print ('q =',q) # q = 66
5.with
with被用来处理异常
- 不用with 处理文件异常
file = open("/tmp/foo.txt")
try:
data = file.read()
finally:
file.close()
- 用with
with open("/tmp/foo.txt")
as file:
data = file.read()
紧跟with后面的语句被求值后,返回对象的enter()方法被调用,这个方法的返回值将被赋值给as后面的变量,此处为file
当with后面的代码块全部被执行完后,将调用前面返回对象的exit()方法
#with 的工作
class Sample:
def __enter__(self):
print ("In __enter__()")
return "Foo"
def __exit__(self, type, value, trace):
print ("In __exit__()")
def get_sample():
return Sample()
with get_sample() as sample:
print ("sample:", sample)
#1. __enter__()方法被执行
#2. __enter__()方法返回的值 - 这个例子中是"Foo",赋值给变量'sample'
#3. 执行代码块,打印变量"sample"的值为 "Foo"
#4. __exit__()方法被调用
with真正强大之处是它可以处理异常。
可能你已经注意到Sample类的__exit__
方法有三个参数- val, type 和 trace。
这些参数在异常处理中相当有用。
class Sample:
def __enter__(self):
return self
def __exit__(self, type,
value, trace):
print ("type:", type)
print ("value:",value)
print ("trace:",trace)
def do_something(self):
bar = 1/0
return bar + 10
with Sample() as sample:
sample.do_something()
实际上,在with后面的代码块抛出任何异常时,exit()方法被执行。
正如例子所示,异常抛出时,与之关联的type,value和stack trace传给exit()方法,
因此抛出的ZeroDivisionError异常被打印出来了。
开发库时,清理资源,关闭文件等等操作,都可以放在exit方法当中。
6.while, for…in…
均为循环语句,使用while时要注意成立条件,防止陷入死循环
for in 遍历
7.assert
断言,声明其布尔值必须为真的判定,如果发生异常就说明表达示为假。
可以理解assert断言语句为raise-if-not,用来测试表示式,其返回值为假,就会触发异常。
assert 1==1
assert 1 == 2# 会报错 Asserterror
assert expression , 'arguments'
#assert 表达式 [, 参数]用来解释断言并更好知道哪里错了
8.pass
pass是空语句,为了保证程序结构的完整性,
pass不做任何事情,一般用作 占位语句
当你编写程序部分内容还没想好,可用pass语句占位
def no_idea():
pass
#实例
for letter in 'python':
if letter == 'h':
pass
print (u'这是pass块')
print (u'当前字母:', letter)
print ('bye,bye')
9.yield
yield的意思是生产,返回了一个生成器对象,每个生成器只能使用一次
def h():
print ('To be brave')
yield 5
h()
看到某个函数包含了yield,这意味着这个函数已经是一个Generator
调用h()函数后,print 语句并未执行,执行yield用.next()方法
def h():
print 'Wen Chuan'
yield 5
print 'Fighting!'
c = h()
# >>>c.next()# 在IDE 中不用print c.next(),直接c.next()。
# next(