原文: https://howtodoinjava.com/python/python-keywords/

    Python 关键字python 编程语言的保留字。 这些关键字不能用于其他目的。

    Python 中有 35 个关键字 - 下面列出了它们的用法。

    and

    A logical AND operator. Return True if both statements are True.

    1. x = (5 > 3 and 5 < 10)
    2. print(x) # True

    or

    逻辑或运算符。 如果两个语句中的任何一个为True,则返回True。 如果两个语句都为假,则返回False

    1. x = (5 > 3 or 5 > 10)
    2. print(x) # True

    as

    它用于创建别名。

    1. import calendar as c
    2. print(c.month_name[1]) #January

    assert

    它可以用于调试代码。 它会测试条件并返回True,否则产生AssertionError

    1. x = "hello"
    2. assert x == "goodbye", "x should be 'hello'" # AssertionError

    async

    它用来声明一个函数为协程,就像@asyncio.coroutine装饰器所做的一样。

    1. async def ping_server(ip):

    await

    它用于调用async协程。

    1. async def ping_local():
    2. return await ping_server('192.168.1.1')

    class

    它用于创建一个类。

    1. class User:
    2. name = "John"
    3. age = 36

    def

    它用于创建或定义函数。

    1. def my_function():
    2. print("Hello world !!")
    3. my_function()

    del

    它用于删除对象。 在 Python 中,所有事物都是对象,因此del关键字也可以用于删除变量,列表或列表的一部分,等等。

    1. x = "hello"
    2. del x

    if

    它用于创建条件语句,该条件语句仅在条件为True时才允许我们执行代码块。

    1. x = 5
    2. if x > 3:
    3. print("it is true")

    elif

    它用于条件语句中,是else if的缩写。

    1. i = 5
    2. if i > 0:
    3. print("Positive")
    4. elif i == 0:
    5. print("ZERO")
    6. else:
    7. print("Negative")

    else

    它决定如果if..else语句中的条件为False时该怎么办。

    1. i = 5
    2. if i > 0:
    3. print("Positive")
    4. else:
    5. print("Negative")

    也可以在try...except块中使用。

    1. x = 5
    2. try:
    3. x > 10
    4. except:
    5. print("Something went wrong")
    6. else:
    7. print("Normally execute the code")

    try

    如果它包含任何错误,它将定义一个测试代码块。

    except

    如果try块引发错误,它将定义要运行的代码块。

    1. try:
    2. x > 3
    3. except:
    4. print("Something went wrong")

    finally

    它定义了一个代码块,无论try块是否引发错误,该代码块都将执行。

    1. try:
    2. x > 3
    3. except:
    4. print("Something went wrong")
    5. finally:
    6. print("I will always get executed")

    raise

    它用于手动引发异常。

    1. x = "hello"
    2. if not type(x) is int:
    3. raise TypeError("Only integers are allowed")

    False

    它是一个布尔值,与 0 相同。

    True

    它是一个布尔值,与 1 相同。

    for

    它用于创建for循环。 for循环可用于遍历序列(如列表,元组等)。

    1. for x in range(1, 9):
    2. print(x)

    while

    它用于创建while循环。 循环继续进行,直到条件语句为假。

    1. x = 0
    2. while x < 9:
    3. print(x)
    4. x = x + 1

    break

    它用于中断for循环或while循环。

    1. i = 1
    2. while i < 9:
    3. print(i)
    4. if i == 3:
    5. break
    6. i += 1

    continue

    它用于在for循环(或while循环)中结束当前迭代,并继续进行下一个迭代。

    1. for i in range(9):
    2. if i == 3:
    3. continue
    4. print(i)

    import

    它用于导入模块。

    1. import datetime

    from

    它仅用于从模块中导入指定的节。

    1. from datetime import time

    global

    它用于从非全局范围创建全局变量,例如在函数内部。

    1. def myfunction():
    2. global x
    3. x = "hello"

    in

    1. 它用于检查序列(列表,范围,字符串等)中是否存在值。

    2. 它也用于在for循环中遍历序列。

    1. fruits = ["apple", "banana", "cherry"]
    2. if "banana" in fruits:
    3. print("yes")
    4. for x in fruits:
    5. print(x)

    is

    它用于测试两个变量是否引用同一对象。

    1. a = ["apple", "banana", "cherry"]
    2. b = ["apple", "banana", "cherry"]
    3. c = a
    4. print(a is b) # False
    5. print(a is c) # True

    lambda

    它用于创建小的匿名函数。 它们可以接受任意数量的参数,但只能有一个表达式。

    1. x = lambda a, b, c : a + b + c
    2. print(x(5, 6, 2))

    None

    它用于定义一个“空”值,或者根本没有值。 None与 0,False或空字符串不同。

    None是它自己的数据类型(NoneType),并且只有None可以是None

    1. x = None
    2. if x:
    3. print("Do you think None is True")
    4. else:
    5. print("None is not True...") # Prints this statement

    nonlocal

    它用于声明变量不是局部变量。 它用于在嵌套函数内部使用变量,其中变量不应属于内部函数。

    1. def myfunc1():
    2. x = "John"
    3. def myfunc2():
    4. nonlocal x
    5. x = "hello"
    6. myfunc2()
    7. return x
    8. print(myfunc1())

    not

    它是一个逻辑运算符,并反转TrueFalse的值。

    1. x = False
    2. print(not x) # True

    pass

    它用作将来代码的占位符。 当执行pass语句时,什么也不会发生,但是当不允许使用空代码时,可以避免出现错误。循环,函数定义,类定义或if语句中不允许使用空代码。

    1. for x in [0, 1, 2]:
    2. pass

    return

    它用于退出一个函数并返回一个值。

    1. def myfunction():
    2. return 3+3

    as

    用于简化异常处理

    yield

    用于结束一个函数,返回一个生成器

    学习愉快!

    参考: W3School