💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Python – 关键字 > 原文: [https://howtodoinjava.com/python/python-keywords/](https://howtodoinjava.com/python/python-keywords/) **Python 关键字**是 [python](https://howtodoinjava.com/python-tutorial/) 编程语言的保留字。 这些关键字不能用于其他目的。 Python 中有 35 个关键字 - 下面列出了它们的用法。 `and` A **logical AND** operator. Return `True` if both statements are `True`. ```py x = (5 > 3 and 5 < 10) print(x) # True ``` `or` 逻辑或运算符。 如果两个语句中的任何一个为`True`,则返回`True`。 如果两个语句都为假,则返回`False`。 ```py x = (5 > 3 or 5 > 10) print(x) # True ``` `as` 它用于创建别名。 ```py import calendar as c print(c.month_name[1]) #January ``` `assert` 它可以用于调试代码。 它会测试条件并返回`True`,否则产生`AssertionError`。 ```py x = "hello" assert x == "goodbye", "x should be 'hello'" # AssertionError ``` `async` 它用来声明一个函数为协程,就像`@asyncio.coroutine`装饰器所做的一样。 ```py async def ping_server(ip): ``` `await` 它用于调用`async`协程。 ```py async def ping_local(): return await ping_server('192.168.1.1') ``` `class` 它用于创建一个类。 ```py class User: name = "John" age = 36 ``` `def` 它用于创建或定义函数。 ```py def my_function(): print("Hello world !!") my_function() ``` `del` 它用于删除对象。 在 Python 中,所有事物都是对象,因此`del`关键字也可以用于删除变量,[列表](https://howtodoinjava.com/python/python-lists/)或列表的一部分,等等。 ```py x = "hello" del x ``` `if` 它用于创建条件语句,该条件语句仅在条件为`True`时才允许我们执行代码块。 ```py x = 5 if x > 3: print("it is true") ``` `elif` 它用于条件语句中,是`else if`的缩写。 ```py i = 5 if i > 0: print("Positive") elif i == 0: print("ZERO") else: print("Negative") ``` `else` 它决定如果`if..else`语句中的条件为`False`时该怎么办。 ```py i = 5 if i > 0: print("Positive") else: print("Negative") ``` 也可以在`try...except`块中使用。 ```py x = 5 try: x > 10 except: print("Something went wrong") else: print("Normally execute the code") ``` `try` 如果它包含任何错误,它将定义一个测试代码块。 `except` 如果`try`块引发错误,它将定义要运行的代码块。 ```py try: x > 3 except: print("Something went wrong") ``` `finally` 它定义了一个代码块,无论`try`块是否引发错误,该代码块都将执行。 ```py try: x > 3 except: print("Something went wrong") finally: print("I will always get executed") ``` `raise` 它用于手动引发异常。 ```py x = "hello" if not type(x) is int: raise TypeError("Only integers are allowed") ``` `False` 它是一个布尔值,与 0 相同。 `True` 它是一个布尔值,与 1 相同。 `for` 它用于创建`for`循环。 `for`循环可用于遍历序列(如列表,元组等)。 ```py for x in range(1, 9): print(x) ``` `while` 它用于创建`while`循环。 循环继续进行,直到条件语句为假。 ```py x = 0 while x < 9: print(x) x = x + 1 ``` `break` 它用于中断`for`循环或`while`循环。 ```py i = 1 while i < 9: print(i) if i == 3: break i += 1 ``` `continue` 它用于在`for`循环(或`while`循环)中结束当前迭代,并继续进行下一个迭代。 ```py for i in range(9): if i == 3: continue print(i) ``` `import` 它用于导入模块。 ```py import datetime ``` `from` 它仅用于从模块中导入指定的节。 ```py from datetime import time ``` `global` 它用于从非全局范围创建全局变量,例如在函数内部。 ```py def myfunction(): global x x = "hello" ``` `in` 1\. 它用于检查序列(列表,范围,字符串等)中是否存在值。 2\. 它也用于在`for`循环中遍历序列。 ```py fruits = ["apple", "banana", "cherry"] if "banana" in fruits: print("yes") for x in fruits: print(x) ``` `is` 它用于测试两个变量是否引用同一对象。 ```py a = ["apple", "banana", "cherry"] b = ["apple", "banana", "cherry"] c = a print(a is b) # False print(a is c) # True ``` `lambda` 它用于创建小的匿名函数。 它们可以接受任意数量的参数,但只能有一个表达式。 ```py x = lambda a, b, c : a + b + c print(x(5, 6, 2)) ``` `None` 它用于定义一个“空”值,或者根本没有值。 `None`与 0,`False`或空字符串不同。 `None`是它自己的数据类型(`NoneType`),并且只有`None`可以是`None`。 ```py x = None if x: print("Do you think None is True") else: print("None is not True...") # Prints this statement ``` `nonlocal` 它用于声明变量不是局部变量。 它用于在嵌套函数内部使用变量,其中变量不应属于内部函数。 ```py def myfunc1(): x = "John" def myfunc2(): nonlocal x x = "hello" myfunc2() return x print(myfunc1()) ``` `not` 它是一个逻辑运算符,并反转`True`或`False`的值。 ```py x = False print(not x) # True ``` `pass` 它用作将来代码的占位符。 当执行`pass`语句时,什么也不会发生,但是当不允许使用空代码时,可以避免出现错误。循环,函数定义,类定义或`if`语句中不允许使用空代码。 ```py for x in [0, 1, 2]: pass ``` `return` 它用于退出一个函数并返回一个值。 ```py def myfunction(): return 3+3 ``` `as` 用于简化异常处理 `yield` 用于结束一个函数,返回一个生成器 学习愉快! 参考: [W3School](https://www.w3schools.com/python/python_ref_keywords.asp)