无论学习什么编程语言,一开始我们在命名的时候都会接触到叫关键字的名词,我们在给变量命名的时候要避开关键字,下面我们来总结一下Python的33个关键字和它的用法,在我们的Python编程生涯中,几乎都会遇到他们,所以知道以及掌握它们非常重要。再次给大家推荐一下,我的github仓库,现在不断在填充内容,希望他能够成为大家Python学习和面试的指南,如果大家有什么建议,可以私信我。谢谢大家。
地址:https://github.com/hellgoddes...
下面我们开始进入Python的关键字总结:
False | None | True | |||
---|---|---|---|---|---|
and | as | assert | break | class | continue |
def | del | elif | else | except | finally |
for | from | if | import | in | global |
is | lambda | nonlocal | not | or | pass |
raise | return | try | while | with | yield |
我们可以将33个关键字分为七类:
1、内置常量
>>> False == 0
True
>>> True == 1
True
>>> type(False)
<class 'bool'>
>>> type(None)
<class 'NoneType'>
>>>
2、逻辑与或非 and or not
优先级:not and or
x and y 如果 x 为 False 、空、0,返 回 x,否则返回 y
x or y 如果 x 为 False、 空、0,返回 y,否则返回x
not x 如果 x 为 False、 空、0,返回 True,否则返回False
3、判断与循环
if elif else is in for while break continue
我们主要关注一下break与continue的区别
- break:跳出循环,不再执行
break语句用来终止循环语句,即循环条件没有False条件或者序列还没有被完全递归完,也会停止执行循环语句,break语句用在while与for循环中
while True:
print("123")
break
print("456")
最后输出: 123
程序执行到break时,跳出了本次循环,所以print("456")不会打印出来
第二个例子:
break是终止本次循环,比如你很多个for循环,你在其中一个for循环里写了一个break,满足条件,只会终止这个for里面的循环,程序会跳到上一层for循环继续往下走
for i in range(5):
print("-----%d-----" %i)
for j in range(5):
if j > 4:
break
print(j)
==============================================
-----0-----
0
1
2
3
4
-----1-----
0
1
2
3
4
-----2-----
0
1
2
3
4
-----3-----
0
1
2
3
4
-----4-----
0
1
2
3
4
这里遇到j>5的时候第二层的for就不循环了,继续跳到上一层循环
continue:跳出本次循环,执行下一次
- Python continue 语句跳出本次循环,而break跳出整个循环。
- continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。
- continue语句用在while和for循环中。
例子:continue通过if判断触发,跳出当前一层for循环,终止'h'输出,继续下一次for
for letter in 'Python':
if letter == 'h':
continue #此处跳出for枚举'h'的那一次循环
print('当前字母 :', letter)
4、函数
def lambda pass return yield
- def --定义函数
- lambda --匿名函数,这个关键字可以用一行实现一个函数
>>> sum = lambda x1, x2 : x1 + x2
>>> print("相加后的值:", sum(10, 20))
相加后的值: 30
- pass:一般用在空函数上,占位符。
def sample(n_samples):
pass
# 当一个函数还没有策划好之后,可以用pass来设置空函数。
return与yield
- return与yield都可以返回值,但是两者有的很大的区别
- 任何使用yield的函数都称为生成器,而生成器通常可被理解为迭代器。
如果不懂什么是生成器和迭代器,可以看看我之前写的这篇文章:
举个示例:
>>> def count(n):
while n > 0:
yield n
n -= 1
>>> c = count(5)
>>> c.__next__()
5
>>> c.__next__()
4
>>> c.__next__()
3
>>> c.__next__()
2
>>> c.__next__()
1
>>>
异常处理
try except finally raise
一个示例介绍完
def sayHello():
print("hello")
# 异常处理块
try:
sayHello()
except Exception as e: # 捕获错误
print(e)
finally: # 一定会执行
print("我一定执行")
raise: 主动跑出错误
6、导入模块,包
import from
重命名
as
Exceptiton重命名为e
变量
global nonlocal
- global : 一般在局部或函数内对全局变量进行修改,须在局部用global声明变量,不然无法修改。
>>> num = 1
>>> def fun():
global num
num =123
print(num)
>>> fun()
123
>>> print(num)
123
- nonlocal:nonlocal是在Python3.2之后引入的一个关键字,它是用在封装函数中的,且一般使用于嵌套函数的场景中。
In [8]: def make_counter():
...: count = 0
...: def counter():
...: nonlocal count
...: count += 1
...: return count
...: return counter
...:
In [9]: counter = make_counter()
In [10]: counter()
Out[10]: 1
In [11]: counter()
Out[11]: 2
类
class: 定义类
删除
del
上下文管理器
with
结合as使用
file = open('b.txt','w')
with file as f:
data = f.write('你好,世界!\n')
f.close()
with open('b.txt','r') as f:
data = f.read()
print(data)
python关键字的内容就介绍完啦。我们下次见
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。