一、应用根本语法 [] 创立
a = []
print(a) #后果,创立空的列表 []
b = [1,2,”abc”]
print(b) #后果:[1, 2, ‘abc’]
print(b[2]) #后果:abc
二、list()创立
c = list()
print(c) #后果:创立一个空的列表
c.append(1)
print(c) #后果:[1]
c = list(“abcdefg”)
print(c) #后果:[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’]
d = list(range(10))
print(d) #后果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
三、range()创立整数列表
range()能够帮忙咱们十分不便的创立整数列表,这在开发中及其有用。语法格局为:
range([start],end ,[step])
start 参数:可选,示意起始数字。默认是 0
end 参数:必选,示意结尾数字。
step 参数:可选,示意步长,默认为 1
e = list(range(0,10,2))
print(e) #后果:[0, 2, 4, 6, 8]
f = list(range(10,0,-1))
print(f) #后果:[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
g = list(range(-10,-30,-2))
print(g) #后果:[-10, -12, -14, -16, -18, -20, -22, -24, -26, -28]
四、推导式生成列表(简介一下,重点在 for 循环后讲)
h = [x*2 for x in range(10)]
print(h) #后果:[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
k = [x*2 for x in range(100) if x%9==0]
print(k) #后果:[0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198]
python lists everything you need to know
create a list in python
5 useful python 3 range function examples
how to append list in python