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