The list creation python

Create a list of general

A=[0,1,2,3,4,5,6,7,8,9]      # 直接创建
print(A)    # 结果为:[1, 2, 4, 5, 6, 8, 15, 45]

Derivations create

B=[i for i in range(12)]    # range(12)表示列表[0,1,2,3,4,5,6,7,8,9,10,11]
print(B)    

Here Insert Picture Description
for each loop can be nested

B=[i+j for i in range(5) for j in range(1,3)]    # range(1,3)表示[1,2]
print(B)  

Here Insert Picture Description
You can also add if the judge sentences

B=[i for i in range(5) if i%2 ]    # if i%2 即 if i%2==1
print(B)                            # if not i%2 就是 if i%2==0

Here Insert Picture Description
range () Function Description:
https://blog.csdn.net/GrofChen/article/details/91490212
derived formula:
https://blog.csdn.net/GrofChen/article/details/91491483

Guess you like

Origin blog.csdn.net/GrofChen/article/details/91489383