这是风火轮编程Python初级教程的第六课,下面是用python制作的学习大纲。
用Python制作的幻灯片类型的作品,用来教Python列表入门的,以下是完整源代码。
本程序需要sprites模块支持,安装方法为在命令提示符下输入以下命令安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple sprites –upgrade
"""
第六课 列表入门
"""
from sprites import *
s = '第六课 列表入门'
screen = Screen()
screen.bgcolor('blue')
screen.titlebar(False)
root = screen._root # 窗口对象
root.wm_attributes('-alpha',0.7) # 设置窗口为全透明(0到1.0)
screen.setup(800,700)
# 下面的代码让窗口可以拖动.
oldx = 0
oldy = 0
def startmove(event):
global oldx,oldy
oldx = event.x
oldy = event.y
def stopmove(event):
global oldx,oldy
oldx = 0
oldy = 0
def movewindow(event):
global oldx,oldy
dx = event.x - oldx
dy = event.y - oldy
root.move(dx,dy)
screen.cv.bind("", startmove)
screen.cv.bind("", stopmove)
screen.cv.bind("",movewindow)
ft = ('楷体',38,'bold')
t = Sprite(visible=False,pos=(-500,0))
t.color('magenta')
clock = Clock()
for x in range(110):
t.clear()
t.write(s,align='center',font=ft)
t.wait()
t.fd(5)
clock.tick(60)
m1 = Mouse() # 鼠标左键
while not m1.down():screen.update()
for x in range(50):
t.clear()
t.write(s,align='center',font=ft)
t.wait()
t.fd(10)
clock.tick(60)
#以下是显示学习的内容段
studycontent = """
主要学习内容
1、列表的概念与定义列表
2、列表的索引
3、列表数据的可变性
4、append和len命令
5、颜色列表
6、练习与作业
"""
t.color('white')
t.clear()
t.sety(-260) # 这里修改菜单的显示y坐标
ft = ('楷体',24,'bold')
s = studycontent
while not m1.down():screen.update()
# 下面的代码显示主菜单
for x in range(110):
t.clear()
t.write(s,align='center',font=ft)
t.wait()
t.bk(5)
clock.tick(60)
screen.listen()
def slow_write(t,string):
"""
t:角色,string:要显示的字
本函数慢慢的显示字。
"""
string = string.split('\n') # 换成列表
oldxy = t.position() # 记录老的坐标
t.goto(-340,310) # 到新的坐标
for line in string: # 每一行字
for char in line: # 每一个字符
t.write(char,align='center',font=ft)
t.wait(0.2)
cd = len(bytes(char,'gb2312'))
if cd == 1:
t.addx(20)
else:
t.addx(30)
t.setx(-336)
t.addy(-50)
t.goto(oldxy)
s1 = """
1、列表的概念与定义列表
列表是以中括号封闭的,以逗号隔开的,有顺序的,
可变数据集合,英文名为list。
定义一个空列表可以直接输入[],也可以用list命令,
如list()会生成一个空列表。
例如,x = [32,76,1024],就定义了一个列表,
它存储了三个整型数据。
列表中的数据的类型也可以是浮点数、布尔数据、
字符串数据甚至另一个列表。用type查看列表的
结果为<class 'list'>。
"""
def press1():
t.clear()
slow_write(t,s1)
while not m1.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press1,'1')
s2 = """
2、列表的索引
和字符串一样,列表中的数据也有索引,
要访问列表中的每个数据的方法和字符串一样。
从左到右数,索引号是从0到最后一个数据的索引号。
从右到左数则是从-1到第一个数据的索引号。
和字符串一样,如果通过超出范围的索引号来
访问列表中的数据,也会发生索引错误。
"""
def press2():
t.clear()
slow_write(t,s2)
while not m1.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press2,'2')
s3 = """
3、列表数据的可变性
我们可以向列表中添加数据,删除数据,也可以直接
修改列表中的某个数据,列表是可变的!
如,x=[32,76,1024],那么让x[0]=255是可以的,
甚至可以让x[0]='风火轮编程'也是没有问题的。
"""
def press3():
t.clear()
slow_write(t,s3)
while not m1.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press3,'3')
s4 = """
4、append和len命令
如果要向列表末尾添加数据,那么可以用append命令。
用法:列表名称.append(数据)。假设有名为cs的列表。
它的值为['red','orange'],那么cs.append('yellow')
会向这个列表的末尾添加'yellow'这个字符串。
列表还有其它的一些命令,在此不做讲解。
如果要统计列表中数据的数量,可以用len命令。
假设有名为x的列表,len(x)则会返回x列表中的
数据的数量。
"""
def press4():
t.clear()
slow_write(t,s4)
while not m1.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press4,'4')
s5 = """
5、颜色列表
我们可以定义一个列表,在这个列表中都是颜色单词。
可以把它取名为colors。以下是定义颜色表的代码:
colors=['red','orange','yellow',
'green','cyan','blue','purple']
"""
def press5():
t.clear()
slow_write(t,s5)
while not m1.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press5,'5')
s6 = """
6、练习与作业
① 定义一个叫animals的列表,里面有些动物的名称。
然后用append命令添加一些数据进去。最后打印出
这个列表中的数据的数量。
② 用list命令不仅可以新建一个空列表,也能在小
括号里写上一个字符串,看看这样做会返回什么结果。
"""
def press6():
t.clear()
slow_write(t,s6)
while not m1.down():screen.update()
t.clear()
t.write(s,align='center',font=ft)
screen.onkeypress(press6,'6')
byebye = """
下次再见!
"""
def pressq():
t.clear()
t.color('cyan')
t.home()
t.write(byebye,align='center',font=('宋体',38,'bold'))
while not m1.down():screen.update()
screen.bye()
screen.onkeypress(pressq,'q')
screen.mainloop()
以下是付费内容,包括播放视频、思维导图、python源代码及素材等等。
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

