x数值的单列写法:
xlist = [1,2,3,4,5]
for x in xlist:
print(x)
关于len(长度)的用法:(结果为:5)(lens的作用就是把list 化为单个数字(内容的数量总值))
xlist = [2,6,3,4,8]
print(len(xlist))
例子1:结合上述用法就会出现数列矩阵:(结果为xlist = [1, 2, 3, 4, 5]
for i in range(len(xlist)):
print(i,xlist[i])
例子2:
ball1 = [1,2]
ball2 = [3,4]
ball3 = [5,6]
balls = []
balls.append(ball1) #balls = [1,2]
balls.append(ball2) #balls = [3,4]
balls.append(ball3) #balls = [5,6]
for ball in balls:
print(ball[0],ball[1])
上述结果为例子3:用list 重写小球弹跳运动:
import pgzrun #导入游戏库
WIDTH = 800 #设置窗口宽度
HEIGHT = 600 #设置窗口高度
x = WIDTH/2 #小球x坐标,初始化在窗口中间
y = HEIGHT/2 #小球x坐标,初始化在窗口中间
speed_x = 3 #小球x方向的速度
speed_y = 3 #小球y方向的速度
r = 30 #小球的半径
colorR = 255 #小球三个颜色分量
colorG = 0
colorB = 0
#存储小球所有信息的列表
ball = [x,y,speed_x,speed_y,r,colorR,colorG,colorB]
def draw():
screen.fill('white')
screen.draw.filled_circle(
(ball[0],ball[1]),ball[4],(ball[5],ball[6],ball[7]))
def update():
ball[0] = ball[0] + ball[2]
ball[1] = ball[1] + ball[3]
if ball[0] > WIDTH - ball[4] or ball[0] < ball[4]:
ball[2] = - ball[2]
if ball[1] > HEIGHT - ball[4] or ball[1] < ball[4]:
ball[3] = - ball[3]
pgzrun.go()