Home >Backend Development >Python Tutorial >Detailed analysis of Python keywords
This article brings you relevant knowledge about python, which mainly introduces related issues about keywords. It analyzes Python’s keyword knowledge points in detail based on examples. I hope it will be useful to everyone. help.
Recommended learning: python detailed tutorial
and, or, not The keywords are all logical operators and are used as follows:
x1 = (5 > 3 and 5 3 or 5 > 10)x2 x3 = Falsenot x3
The results are as follows:
if, elif, else are mainly used for conditional statements. The usage is as follows :
def func(x): if x <p>The result is as follows: <br><img src="https://img.php.cn/upload/article/000/000/067/fe1421fd000ace756583a0e389d5c9c7-2.png" alt="Detailed analysis of Python keywords"><br> Among them, the else keyword is also used in the try...except block, please see the example below. </p><pre class="brush:php;toolbar:false">def func1(x): try: 100//x except: print("ZeropisionError: pision by zero(除数不能是0)") else: print(f"程序计算结果是{str(100//x)}")func1(10)func1(0)
The results are as follows:
for, while are mainly used to define a loop, the usage is as follows:
name_list = ["张三","李四","王五"]for name in name_list: print(name)
The results are as follows:
x = 0while x<p>The results are as follows: <br><img src="https://img.php.cn/upload/article/000/000/067/fe1421fd000ace756583a0e389d5c9c7-5.png" alt="Detailed analysis of Python keywords"></p><h2>4 True, False</h2><p>True and False are the results returned by the comparison operation. The usage is as follows: </p>
print(9 > 6)print(6 in [11,6,33])print(5 is 5)print(5 == 5)print(5 == 5 and 7 == 7)print(5 == 5 or 6 == 7)print(not(5 == 7))
The results are as follows:
print(9 = 7)print(not(5 == 5))
The results are as follows:
continue and break are mainly used in for loops and while loops. The usage is as follows:
for i in range(10): if i <p>The results are as follows: <br><img src="https://img.php.cn/upload/article/000/000/067/33a9dc96ec9650f787ea254e66c73254-8.png" alt="Detailed analysis of Python keywords"></p><pre class="brush:php;toolbar:false">x = 0while x <p>The results are as follows: <br><img src="https://img.php.cn/upload/article/000/000/067/33a9dc96ec9650f787ea254e66c73254-9.png" alt="Detailed analysis of Python keywords"></p><h2>6 pass</h2><p>pass Statements serve as placeholders for future code. When the pass statement is executed, it will not have any effect. It is just a placeholder that represents blank code. However, if you do not write anything, an error will be reported. If empty code is not allowed in loops, function definitions, class definitions or if statements, pass can be used. <br><img src="https://img.php.cn/upload/article/000/000/067/33a9dc96ec9650f787ea254e66c73254-10.png" alt="Detailed analysis of Python keywords"></p><h2>7 try, except, finally, raise</h2><p>try, except, finally, and raise are all keywords related to exceptions. The usage is as follows: </p>
def func(x): try: 100 // x except: print("ZeropisionError: pision by zero(除数不能是0)") else: print(f"结果是:{str(100 // x)}") finally: print("无论如何,都会执行!") func(10)func(0)
结果如下:
x = 15if x <p>结果如下:<br><img src="https://img.php.cn/upload/article/000/000/067/dcff06d9f769f18601bb11c4301d30c1-12.png" alt="Detailed analysis of Python keywords"></p><h2>8 import、from、as</h2><p>import、from、as均与模块的导入有关,用法如下:</p>
import openpyxlimport pandas as pdfrom openpyxl import load_workbook()
def、return均与函数有关的关键字,用法如下:
def func1(): print("关注公众号:数据分析与统计学之美") func1()
结果如下:
def func2(x,y): return x + y func2(x=2,y=8)
结果如下:
class关键字用于创建(或定义)一个类。
class Person: name = "张三" age = 18 p = Person()p.name,p.age
结果如下:
lambda关键字用于创建一个 “匿名函数”。
x = lambda a: a + 8x(2)y = lambda a,b: a + b y(1,1)z = lambda a,b,c: a * c + b z(2,5,5)
结果如下:
在Python中,一切皆对象。del关键字主要用于删除对象,还可以用于删除变量,列表或列表的一部分等。
x = 1del xprint(x)
结果如下:
x = ["张三","李四","王五"]del x[0]print(x)
结果如下:
global关键字用于创建一个全局变量。nonlocal关键字用于声明一个非局部变量,用于标识外部作用域的变量。
# 定义一个函数:def func(): global x x = "函数中的变量"# 执行函数:func()# x定义在函数中,按说这里打印x会报错,我们看看print(x)
结果如下:
in、is这两个关键字大家一定要区别开来,用法如下:
x = ["张三","李四","王五"]"张三" in x# -------------------------for i in range(3): print(i)
结果如下:
x = 2.0y = 2.0x is y x == y
结果如下:
None关键字用于定义一个空值(根本没有值),与0,False或空字符串不同。 None是其自身的数据类型(NoneType),只能为None。
x = Noneprint(x)if x: print("嘻嘻")else: print("哈哈")
结果如下:
调试代码时,使用assert关键字。主要用于测试代码中的条件是否为True,如果为False,将引发AssertionError。
x = 666assert x == 666assert x == 888,"x应该等于666,你的输入有误!"
结果如下:
with常和open使用,用于读取或写入文件。
with open("哈哈.txt","r") as f: print(f.read())
结果如下:
yield关键字结束一个函数,返回一个生成器,用于从函数依次返回值。
def f(): yield 5f()next(f())
结果如下:
推荐学习:python教程
The above is the detailed content of Detailed analysis of Python keywords. For more information, please follow other related articles on the PHP Chinese website!