Python教学案例的新思维及案例展示课件.ppt

上传人:牧羊曲112 文档编号:1455767 上传时间:2022-11-27 格式:PPT 页数:41 大小:6.35MB
返回 下载 相关 举报
Python教学案例的新思维及案例展示课件.ppt_第1页
第1页 / 共41页
Python教学案例的新思维及案例展示课件.ppt_第2页
第2页 / 共41页
Python教学案例的新思维及案例展示课件.ppt_第3页
第3页 / 共41页
Python教学案例的新思维及案例展示课件.ppt_第4页
第4页 / 共41页
Python教学案例的新思维及案例展示课件.ppt_第5页
第5页 / 共41页
点击查看更多>>
资源描述

《Python教学案例的新思维及案例展示课件.ppt》由会员分享,可在线阅读,更多相关《Python教学案例的新思维及案例展示课件.ppt(41页珍藏版)》请在三一办公上搜索。

1、Python教学案例的新思维,新思维:面向问题、引发兴趣、培养能力、引导创新,北京理工大学Python教学案例展示,第一类:培养兴趣的案例第二类:数据分析的案例第三类:网络爬虫的案例第四类:游戏创意的案例,绘制Python小蛇,import turtleimport timeturtle.setup(650, 350, 200, 200)turtle.penup()turtle.fd(-250)turtle.pendown()turtle.pensize(25)turtle.pencolor(purple)turtle.seth(-40)for i in range(4): turtle.ci

2、rcle(40, 80) turtle.circle(-40, 80)turtle.circle(40, 80/2)turtle.fd(40)turtle.circle(16, 180)turtle.fd(40 * 2/3)time.sleep(10),螺旋线绘制,import turtleimport timet = turtle.Pen()turtle.speed(fastest)t.pensize(2)for x in range(100): t.forward(2*x) t.left(90)time.sleep(10),10行代码,斜螺旋线绘制,import turtleimport

3、timet = turtle.Pen()turtle.speed(fastest)t.pensize(2)for x in range(100): t.forward(2*x) t.left(91)time.sleep(10),10行代码,彩色螺旋线绘制,import turtleimport timet = turtle.Pen()t.pensize(2)turtle.bgcolor(black)turtle.speed(fastest)sides = 6colors = red, yellow,purple,bluefor x in range(400): t.color(colorsx%

4、4) t.forward(x*3/sides+x) t.left(360/sides+1) t.width(x*sides/200)time.sleep(30)turtle.mainloop(),17行代码,绘制,import turtleimport timet = turtle.Pen()turtle.speed(fastest)turtle.bgcolor(black)sides = 5colors = red, yellow,purple,bluefor x in range(400): t.color(colorsx%4) t.forward(x*3/sides+x) t.left(

5、360/sides+1) t.width(x*sides/200) t.left(90)time.sleep(10),17行代码,旋转的窗花,import turtleimport timeh = turtle.Turtle()for i in range(0,400): h.speed(-9) h.right(46) h.pencolor(gray) h.circle(50) h.right(20) h.pencolor(yellow) h.circle(100) h.circle(70) h.pencolor(red) h.circle(80) h.circle(90) h.left(10

6、) h.pencolor(black) h.right(5),20行代码,科赫雪花绘制,import turtleimport timedef koch(size, n): if n = 0: turtle.fd(size) else: for angle in 0, 60, -120, 60: turtle.left(angle) koch(size/3, n-1)def main(): turtle.setup(800,400) turtle.penup() turtle.goto(-300, -50) turtle.pendown() turtle.pensize(2) koch(600

7、,3) turtle.hideturtle()main(),20行代码,玫瑰花绘制,92行代码,from turtle import*import timepencolor(“black”)fillcolor(“red”)speed(5000)s=0.15penup()goto(0,600*s)pendown()begin_fill()circle(200*s,30)for i in range(60): lt(1) circle(50*s,1)circle(200*s,30),玫瑰花束绘制,from turtle import*import timep = Turtle()setup(800

8、, 850, 100, 100)p.hideturtle()p.penup()p.goto(0,0)p.pendown()p.pensize(2)p.speed(1000)def rose(): p.left(120) p.fd(120) q = p.clone() q.begin_fill() q.fillcolor(“green”) q.left(70) q.circle(-170,16) q.circle(-170,-12) q.left(60) q.circle(-30,125) q.right(50) p.pencolor(black)p.penup()p.goto(-200,375

9、)p.pendown()p.write(Love Python, Love You),font=(Times,30,bold)time.sleep(10)pendown()begin_fill()circle(200*s,30)for i in range(60): lt(1) circle(50*s,1)circle(200*s,30),100行代码,的计算动态效果,from random import randomfrom math import sqrtfrom time import clockimport turtlen = int(input(请输入一个整数:)darts = 2

10、* nhits = 0clock()turtle.bgcolor(black)turtle.pencolor(black)turtle.speed(0)turtle.hideturtle()for i in range(1, darts): x, y = random(), random() turtle.penup() turtle.goto(500*x-250, 500*y-250) dist = sqrt(x * 2 + y * 2) if dist = 1.0: hits = hits + 1 turtle.dot(blue) else: turtle.dot(white)pi = 4

11、 * hits / darts,26行代码,七段数码管绘制,import turtleimport timedef drawLine(draw,color = “black”): turtle.pendown() if draw else turtle.penup() turtle.color(color) turtle.fd(40) turtle.right(90)def drawDigit(d): drawLine(True,red) if d in 2,3,4,5,6,8,9 else drawLine(False) drawLine(True, red) if d in 0,1,3,4

12、,5,6,7,8,9 else drawLine(False). turtle.left(180) turtle.penup() turtle.fd(20)def drawDate(date): for i in date: drawDigit(eval(i) def main(): turtle.setup(800, 350, 200, 200) turtle.penup() turtle.fd(-300) turtle.pensize(5) drawDate(datetime.datetime.now().strftime(%Y%m%d) turtle.hideturtle()main()

13、time.sleep(10),33行代码,from PIL import Imagefrom PIL import ImageFilterfor i in range(1,8): im = Image.open(beijing+str(i)+.jpg) e33 = im.filter(ImageFilter.CONTOUR) e33.save(beijings+str(i)+.jpg),北京印象系列绘图,星座字符画,import numpy as npfrom PIL import Imageascii_char = list(“$B%8:,”.“”)def get_char(r,b,g,al

14、pha = 256): if alpha = 0: return length = len(ascii_char) gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b) unit = (256.0 + 1) / length return ascii_charint(gray / unit)if _name_ = _main_: im = Image.open(textpicture.jpg) WIDTH, HEIGHT = (80,80) im = im.resize(WIDTH, HEIGHT), Image.NEAREST) .,25行代码,简

15、易计算器绘制,import tkinter as tkcalc = tk.Tk()calc.title(“PythonCalc”)buttons = C, 7, 8, 9, *, 4, 5, 6, /, 1, 2, 3, -, 0, ., =, +, row = 1col = 3for i in buttons: button_style = raised action = lambda x=i: click_event(x)tk.Button(calc, text=i, width=5, height=3, relief=button_style, command=action).grid(

16、row=row,column=col,sticky=nesw, ) col += 1 if col 3: col = 0 row += 1display = tk.Entry(calc, width=35, bg=white)display.grid(row=0, column=0, columnspan=4) .calc.mainloop(),43行代码,PyQt聊天软件实现,import sys,osfrom time import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *from PyQt5.QtCore import

17、 *def main(): def showDialog(): label = “Me : ”.format(strftime(“%Y-%m-%d %H:%M:%S”, localtime() message = chatText.toPlainText() outputarea.append(label) outputarea.append( +message) chatText.clear() chatText.setFocus() saveMsg(message) def cancelMsg(): chatText.clear() def saveMsg(txt): file = ope

18、n(save.txt, a) file.write(txt + n) file.close() .,76行代码,抽奖式随机提问软件,import sysimport timeimport randomimport itertoolsimport threadingfrom PyQt5.QtCore import QCoreApplication,Qtfrom PyQt5.QtGui import Qfont, Qpalettefrom PyQt5.QtWidgets import Qapplication,Qlabel,QMessageBox,QPushButton,Qwidgetstuden

19、ts = 张三, 李四, 王五, 赵六, 周七, 钱八class Example(QWidget): def _init_(self): super()._init_() self.initUI() def initUI(self): startbtn = QPushButton(开始, self) startbtn.clicked.connect(self.btnStartClick) startbtn.resize(startbtn.sizeHint() startbtn.move(200, 100) stopbtn = QPushButton(停, self) .,114行代码,PyQt

20、时钟实现,Turtle时钟实现,北京理工大学Python教学案例展示,第一类:培养兴趣的案例第二类:数据分析的案例第三类:网络爬虫的案例第四类:游戏创意的案例,科学坐标系绘制,import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0.0, 6.0, 100)y = np.cos(2 * np.pi * x) * np.exp(-x)+0.8z = 0.5 * np.cos(x * 2)+0.8color1 = “red”title = “阻尼衰减曲线绘制”label1,label2 = “$exp_decay$”,“$

21、cos(x2)$”note_point,note_text,size = (1, np.cos(2 * np.pi) * np.exp(-1)+0.8),(1, 1.4), 14x_label,y_label = 时间(s),幅度(mV)x_start,x_end,y_start,y_end = 0, 5, 0, 1.8a, b = 0.8,3.,59行代码,雷达图绘制,import numpy as npimport matplotlib.pyplot as pltimport matplotlib.labels = np.array(综合, KDA, 发育, 推进, 生存,输出)dataL

22、enth = 6data = np.array(7, 5, 6, 9, 8, 7)angles = np.linspace(0, 2 * np.pi, dataLenth, endpoint=False)data = np.concatenate(data, data0) angles = np.concatenate(angles, angles0) fig = plt.figure(facecolor=white)plt.subplot(111, polar=True)plt.plot(angles, data,bo-, color = g,linewidth=2)plt.fill(ang

23、les, data, facecolor=g, alpha=0.25)plt.thetagrids(angles * 180 / np.pi, labels)plt.figtext(0.52, 0.95, DOTA能力值雷达图, ha=center)plt.grid(True)plt.show(),27行代码,科学坐标系绘制,import numpy as npimport matplotlib.pyplot as pltfrom radar_factory import radar_factorymatplotlib.rcParamsfont.family=SimHeimatplotlib.

24、rcParamsfont.sans-serif = SimHeidata_verts = column names: 研究型(I),艺术型(A),社会型(S),企业型(E),常规型(C),现实型(R)data_points = points: 0.40, 0.32, 0.35, 0.30, 0.30, 0.88, 0.85, 0.35, 0.30, 0.40, 0.40, 0.30, 0.43, 0.89, 0.30, 0.28, 0.22, 0.30, 0.30, 0.25, 0.48, 0.85, 0.45, 0.40, 0.20, 0.38, 0.87, 0.45, 0.32, 0.28

25、, 0.34, 0.31, 0.38, 0.40, 0.92, 0.28 .,44行代码,LIGO引力波绘制,import numpy as npimport matplotlib.pyplot as pltfrom scipy.io import wavfilerate_h, hstrain= wavfile.read(r“H1_Strain.wav”,“rb”)rate_l, lstrain= wavfile.read(r“L1_Strain.wav”,“rb”)reftime, ref_H1 = np.genfromtxt(GW150914_4_NR_waveform_template.

26、txt).transpose()htime_len = hstrain.shape0/rate_hprint(hstrain.shape)htime = np.arange(-htime_len/2, htime_len/2 , htime_interval).fig = plt.figure(figsize=(12, 6)plth = fig.add_subplot(221)plth.plot(htime, hstrain, y)plth.set_xlabel(Time (seconds)plth.set_ylabel(H1 Strain)plth.set_title(H1 Strain).

27、pltref = fig.add_subplot(212)pltref.plot(reftime, ref_H1)pltref.set_xlabel(Time (seconds)pltref.set_ylabel(Template Strain)pltref.set_title(Template)fig.tight_layout()plt.savefig(Gravitational_Waves_Original.png)plt.show()plt.close(fig),42行代码,北京理工大学Python教学案例展示,第一类:培养兴趣的案例第二类:数据分析的案例第三类:网络爬虫的案例第四类:游

28、戏创意的案例,淘宝商品定价爬虫,import requestsimport redef getHTMLText(url): try: r = requests.get(url, timeout=30) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return “” def parsePage(ilt, html): .def printGoodsList(ilt): tplt = :4t:8t:16 print(tplt.format(序号, 价格, 商品名称) count = 0 fo

29、r g in ilt: count = count + 1 print(tplt.format(count, g0, g1) def main(): goods = 书包 depth = 3 start_url = https:/ + goods infoList = for i in range(depth): try: url = start_url + &s= + str(44*i) html = getHTMLText(url) parsePage(infoList, html) except: continue printGoodsList(infoList) main(),47行代

30、码,淘宝商品定价爬虫输出,序号 价格 商品名称 149.00 瑞士军刀旅行双肩包男中学生书包电脑背包 259.00 迪卡侬旗舰店官方店双肩包女男百搭登山包学生书包20L QUECHUA HB 369.00 迪士尼书包小学生男女1-3-4-6年级米奇减负背包儿童书包8-10-12岁 4258.00 新款头层牛皮流苏包多功能韩版学院风时尚书包真皮简约双肩包女潮 570.00 双肩包女韩版潮学院风几何印花时尚背包简约休闲中学生书包小清新 688.99 牛津布配铆钉双肩包女包2016新款日韩版休闲背包学院风书包潮 749.00 2017新款韩版双肩包女复古PU背包潮百搭学院风旅行流苏中学生书包 845

31、.90 2017新款学生书包双肩包女韩版pu皮简约时尚休闲百搭背包旅行包潮 9267.00 双肩包男士背包商务韩版时尚休闲旅行电脑包高中生大学生书包男女 1048.00 2017新款韩版男女双肩包帆布包学院风学生书包大容量旅行包百搭潮,Scrapy股票数据定向爬虫,#piplelines.pyclass BaidustocksPipeline(object): def process_item(self, item, spider): return itemclass BaidustocksInfoPipeline(object): def open_spider(self, spider):

32、 self.f = open(BaiduStockInfo.txt, w) def close_spider(self, spider): self.f.close() def process_item(self, item, spider): try: line = str(dict(item) + n self.f.write(line) except: pass return item#middlewares.pyfrom scrapy import signalsclass BaidustocksSpiderMiddleware(object): classmethod def fro

33、m_crawler(cls, crawler): # This method is used by Scrapy to create your spiders. s = cls() crawler.signals.connect(s.spider_opened, signal=signals.spider_opened) return s def process_spider_input(response, spider): return None,Scrapy股票数据定向爬虫输出,今开: 12.07, 成交量: 7.45万手, 最高: 12.07, 涨停: 12.72, 内盘: 4.06万手

34、, 成交额: 8629.61万, 委比: 65.99%, 流通市值: 28.90亿, 市盈率MRQ: 57.72, 每股收益: 0.20, 总股本: 5.36亿, 昨收: 11.56, 换手率: 2.92%, 最低: 11.04, 跌停: 10.40, 外盘: 3.39万手, 振幅: 8.91%, 量比: 2.77, 总市值: 60.78亿, 市净率: 3.56, 每股净资产: 3.19, 流通股本: 2.55亿, 股票名称: 新天科技300259今开: 27.13, 成交量: 2.00万手, 最高: 27.99, 涨停: 29.95, 内盘: 9410手, 成交额: 5531.07万, 委比

35、: 6.81%, 流通市值: 29.75亿, 市盈率MRQ: 60.07, 每股收益: 0.46, 总股本: 1.46亿, 昨收: 27.23, 换手率: 1.85%, 最低: 27.13, 跌停: 24.51, 外盘: 1.05万手, 振幅: 3.16%, 量比: 0.55, 总市值: 40.33亿, 市净率: 8.68, 每股净资产: 3.18, 流通股本: 1.08亿, 股票名称: 宝莱特300246今开: 14.20, 成交量: 1.52万手, 最高: 14.29, 涨停: 15.51, 内盘: 8364手, 成交额: 2153.25万, 委比: 53.51%, 流通市值: 45.05

36、亿, 市盈率MRQ: 233.28, 每股收益: 0.05, 总股本: 3.20亿, 昨收: 14.10, 换手率: 0.48%, 最低: 14.06, 跌停: 12.69, 外盘: 6835手, 振幅: 1.63%, 量比: 0.09, 总市值: 45.12亿, 市净率: 4.81, 每股净资产: 2.93, 流通股本: 3.20亿, 股票名称: 科泰电源300153今开: 30.20, 成交量: 8.04万手, 最高: 31.20, 涨停: 33.40, 内盘: 3.97万手, 成交额: 2.44亿, 委比: 75.79%, 流通市值: 116.22亿, 市盈率MRQ: 38.61, 每股

37、收益: 0.76, 总股本: 6.68亿, 昨收: 30.36, 换手率: 2.04%, 最低: 29.00, 跌停: 27.32, 外盘: 4.08万手, 振幅: 7.25%, 量比: 2.71, 总市值: 196.89亿, 市净率: 4.86, 每股净资产: 6.07, 流通股本: 3.94亿, 股票名称: 中金环境300145,北京理工大学Python教学案例展示,第一类:培养兴趣的案例第二类:数据分析的案例第三类:网络爬虫的案例第四类:游戏创意的案例,PyQt俄罗斯方块,Turtle俄罗斯方块,Pygame实现RPG角色扮演游戏,来源于github,Pygame实现棋牌类游戏,来源于github,Pygame实现射击类游戏,来源于github,北京理工大学Python教学案例展示,面向兴趣、面向学科/专业、面向能力其他类:文本处理、自然语言处理、机器学习、turtle艺术等,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号