A08_数据转换_Python教学视频

A08_数据转换_Python教学视频

李兴球Python数据转换幻灯片教程

这是风火轮编程Python初级教程的第八课,下面是用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)
screen.tracer(0,0)

# 下面的代码让窗口可以拖动.
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)

# 下面的代码按方向箭头则窗口能上下左右移动
screen.onkeypress(lambda:root.move(10),'Right')
screen.onkeypress(lambda:root.move(-10),'Left')
screen.onkeypress(lambda:root.move(0,-10),'Up')
screen.onkeypress(lambda:root.move(0,10),'Down')
screen.listen()

ft = ('楷体',38,'bold')
t = Sprite(visible=False,pos=(-500,100))
t.color('magenta')
clock = Clock()
for x in range(100):
    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()

# 简介
brief ="""
数据之间是可以进行转换的,如整数可以转换小数,

列表和元组可以互相转换,字符串也能转换成列表。

本课主要学习一些命令,然后做练习以巩固所学。
"""
if brief!='':  # 如果有课程简介,则生成一个角色,
    ftx = ('宋体',18,'normal')
    简介 = Sprite(visible=False,pos=(0,-100))
    简介.color('white')
    简介.write(brief,align='center',font=ftx)    
    while  m1.down():screen.update()    
    while not m1.down():screen.update()    

for x in range(66):              # 标题向右消失
    t.clear()
    t.write(s,align='center',font=ft)    
    t.fd(10)
    if brief!='':        
        简介.clear()
        简介.write(brief,align='center',font=ftx)
        简介.bk(10)
    clock.tick(60)
简介.clear()     

#以下是显示学习的内容段
studycontent = """
主要学习内容

1、int命令

2、float命令

3、bool命令

4、str命令

5、list命令

6、tuple命令

7、综合运用与作业
"""
t.color('white')
t.clear()
t.sety(-260)              # 这里修改菜单的显示y坐标
ft = ('楷体',24,'bold')
s = studycontent
while not m1.down():screen.update()
#  下面的代码显示主菜单
for x in range(120):
    t.clear()
    t.write(s,align='center',font=ft)    
    t.bk(5)
    clock.tick(60)

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、int命令

把小数或者全是数字的字符串转换成整数。
如int('32')返回结果就是32。int(3.14)返回3。
"""
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、float命令

把全是数字的字符串(包括.号)和整数转换成浮点数。
如float('10.9')得到10.9,float(56)得到56.0
"""
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、bool命令

把其它数据转换成逻辑数据。
如bool(10)返回True,bool(-10)也是返回True。
总之,非0的数据返回的都是真,0本来就是假。
bool('')返回False,bool('编程')返回True。
非空字符串返回真,空字符串返回假。
bool([])返回False,bool(['px','a'])返回True。
空列表返回假,非空列表返回真,元组也一样。  
"""
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、str命令

把其它数据转换成字符串,相当于把任何其它数据
加上引号。如str(123)结果是'123',
str([102,476])返回'[102,476]'。
"""
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、list命令

新建空列表,或者把字符串或元组转换成列表。
如list('abc')返回['a','b','c']
list((32,76))返回[32,76]。
以后会学range命令,它会产生一些数据。
如range(10)会产生从0到9的数据,用list命令
的话,那么list(range(10))就会产生下面的列表:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
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、tuple命令

新建空元组,或者把列表或字符串转换成元组。
如tuple('abc')得到('a','b','c'),
tuple([102,476,8])得到(102,476,8),
tuple(str(65))得到('6','5')
"""
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')

s7 = """
7、综合运用与作业
   
int(str(32)),   
str(type(32)),
float(id(256))
type(len('ab')),
len(str(type(()))) ,
len(str(type('爱编程'))),
list(str(id(len('abc')))),
len(str(['风火轮编程','Python']),   
len('Python') * 3 + len('Python'*3)
"""
def press7():
    t.clear()
    slow_write(t,s7)
    while not m1.down():screen.update()
    t.clear()
    t.write(s,align='center',font=ft)    
screen.onkeypress(press7,'7')

 

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资源。)

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。