python有趣的案例_Python有趣的小案例
# 所需依赖:python3 pycharm
# print 打印
print('hello world!')
# 注释符号
# 井号后面灰色的内容是注释,相当于笔记,会被机器忽略
# 变量和值
# n 是变量, 100 是值,等号的作用是赋值
# n 相当于高中数学的 xyz ,只不过 xyz 的值只能是数字,变量的功能要更强大
n = 100
m = 'hello'
print(n)
print(m)
# 数据类型,这里只讲两个,剩下的需要同学自己去系统地学习了
# 字符串 和 整数
# 100 是整数类型
# 'hello' 是字符串类型
# 导入 turtle 模块
# 模块是 python 自带的工具箱,这里将工具箱导入就能使用了
# turtle 模块是 python 用来画图的工具箱
import turtle
# 将 turtle 里的工具拿出来,赋给 t 变量
# 照猫画虎用就是了,这些东西要到很后面才能理解
t = turtle.Turtle()
# 这一行用来加快画笔速度,从 1~9 依次变快,但 0 是最快
t.speed(0)
# 这是向前走,单位是像素
t.forward(100)
# 这是转弯,单位是角度
t.right(120)
t.forward(100)
t.right(120)
t.forward(100)
t.right(120)
# 复制三次,就画了一个三角形
# 正方形
# 长方形
# 如果我们需要改变三角形的边长怎么办?
# 这就要用到变量了,到时候只需改变变量就能改变长度
# 如果有相同的变量,后面定义的会覆盖前面的
l = 200
t.forward(l)
t.right(120)
t.forward(l)
t.right(120)
t.forward(l)
t.right(120)
# for 循环
# 循环还有 while 循环,考虑到用不着就不讲了
# 循环用来处理重复的事情
# range() 是一个区间
# range(3) 相当于 0 1 2
# range(5) 相当于 0 1 2 3 4
# i 取的是 range() 里的值,一次取一个,取一次就循环一次
# 冒号后面必有缩进,缩进的代表是同一个代码块
# 照着用就行了,注意一个字符都不能敲错,不能用中文符号
for i in range(3):
t.forward(l)
t.right(120)
# 如果想画两个三角形怎么办,再复制一个 for 循环?
# 我们用函数将代码封装起来,到时候直接调用就好了
# def 关键字用来定义函数, triangle 是函数名
# 必须要有冒号接缩进,函数里面也是一个代码块
def triangle():
for i in range(3):
t.forward(l)
t.right(120)
# 函数的调用
# triangle()
# 函数可以传递参数进去
def triangle2(l):
for i in range(3):
t.forward(l)
t.right(120)
# 需要传递个参数进去才能调用这个函数
# triangle2(250)
# 定一个函数画长方形
# 四则运算
# + 加
# - 减
# * 乘
# / 除
# // 整除
# % 取余
# 写一个画 n 边形的通用函数
def polygon(l, n):
angle = 360 / n
for i in range(n):
t.forward(l)
t.right(angle)
# polygon(100, 6)
# 画一个五角星
def five_star(l):
for i in range(5):
t.forward(l)
t.right(144)
# five_star(100)
# 画一个圆
# 边长在 36 以上就是个圆
def circle():
for i in range(36):
t.forward(10)
t.right(15)
# circle()
# 在指定的坐标画图
# 比如要在坐标为 (100, 150) 的位置画个正方形
def square(x, y, l):
t.penup()
t.goto(x, y)
t.pendown()
for i in range(4):
t.forward(l)
t.right(90)
# square(100, 150, 100)
# 将画笔定位封装成函数使用,就能有效去除重复代码
def setpen(x, y):
t.penup()
t.goto(x, y)
t.pendown()
t.setheading(0)
def square(x, y, l):
setpen(x, y)
for i in range(4):
t.forward(l)
t.right(90)
# square(100, 150, 100)
# 画一排正方形,共五个,间隔 10
# 蠢方法
# square(100, 150, 30)
# square(140, 150, 30)
# square(180, 150, 30)
# square(220, 150, 30)
# square(260, 150, 30)
# 使用 for 循环、函数
def square_line(x, y, l, n, dis):
for i in range(n):
inner_x = x + (l + dis) * i
square(inner_x, y, l)
# square_line(100, 150, 30, 6, 10)
# 画一个正方形方阵
def square_matrix(x, y, l, n, dis, m):
for i in range(m):
inner_y = y - (l + dis) * i
square_line(x, inner_y, l, n, dis)
# square_matrix(100, 150, 30, 5, 10, 6)
# 填充颜色,给图形上色
def five_star(l):
t.fillcolor('yello')
t.begin_fill()
for i in range(5):
t.forward(l)
t.right(144)
t.end_fill()
# five_star(100)
# 字典的简单用法
# 抽象画
# for i in range(500):
# t.forward(i)
# t.left(90)
# for i in range(500):
# t.forward(i)
# t.left(91)
colors = ['red', 'yellow', 'blue', 'green']
# for i in range(500):
# t.pencolor(colors[i % 4])
# t.circle(i)
# t.left(91)
# sides = 5
# colors = ['red', 'yellow', 'blue', 'orange', 'green', 'purple']
# for i in range(360):
# t.pencolor(colors[i % sides])
# t.forward(i * 3 / sides + i)
# t.left(360 / sides + 1)
# t.width(i * sides / 200)
# 美队盾牌
def circle(x, y, r, color):
n = 36
angle = 360 / n
pi = 3.1415926
c = 2 * pi * r
l = c / n
start_x = x - l / 2
start_y = y + r
setpen(start_x, start_y)
t.pencolor(color)
t.fillcolor(color)
t.begin_fill()
for i in range(n):
t.forward(l)
t.right(angle)
t.end_fill()
def five_star(l):
setpen(0, 0)
t.setheading(162)
t.forward(150)
t.setheading(0)
t.fillcolor('WhiteSmoke')
t.begin_fill()
t.hideturtle()
t.penup()
for i in range(5):
t.forward(l)
t.right(144)
t.end_fill()
def sheild():
circle(0, 0, 300, 'red')
circle(0, 0, 250, 'white')
circle(0, 0, 200, 'red')
circle(0, 0, 150, 'blue')
five_star(284)
sheild()
# 结尾这一行必须有,照着用就行了
turtle.done()
python有趣的案例_Python有趣的小案例相关推荐
- python小案例_Python的应用小案例
1.python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc ...
- 案例:倒计时 js小案例
案例:倒计时 倒计时展示案例 动图展示: 代码展示: <p>案例:倒计时</p> <div><span class="hour">1 ...
- python有趣的工具_python好玩的小工具(正在create中)
思路:(可以不用key value来做,本次也没用到) 0)先创建new.xlsx 复制test.xlsx的所有内容,放到new.xlsx里 ,并 以key ,value的形式存base.xlsx数据 ...
- python 文本分析库_Python有趣|中文文本情感分析
前言 前文给大家说了python机器学习的路径,这光说不练假把式,这次,罗罗攀就带大家完成一个中文文本情感分析的机器学习项目,今天的流程如下: 数据情况和处理 数据情况 这里的数据为大众点评上的评论数 ...
- python情感分析中文_Python有趣|中文文本情感分析
前言 前文给大家说了python机器学习的路径,这光说不练假把式,这次,罗罗攀就带大家完成一个中文文本情感分析的机器学习项目,今天的流程如下: 数据情况和处理 数据情况 这里的数据为大众点评上的评论数 ...
- python中文文本分析_Python有趣|中文文本情感分析
前言 前文给大家说了python机器学习的路径,这光说不练假把式,这次,罗罗攀就带大家完成一个中文文本情感分析的机器学习项目,今天的流程如下: 数据情况和处理 数据情况 这里的数据为大众点评上的评论数 ...
- python有趣的库_Python有趣的库tqdm,给程序加一个进度条吧!
Python开发过程中,经常会使用到大量的循环,如果能有个进度条提示我们当前程序执行到第几步了,这会是一个很有用的功能. tqdm在阿拉伯语中表示"progress",而在西班牙语 ...
- python 图形_Python切分图像小案例(1、3、2、4象限子图互换)
首先解释上一篇文章详解Python科学计算扩展库numpy中的矩阵运算(1)最后的习题,该问题答案是10 ** 8 = 100000000,原因在于Python中的运算符**是从右往左计算的,这在Py ...
- python 有趣的爬虫_Python有趣|微博榜单爬虫,尽知天下事
项目背景 大家对于网页的数据爬虫了解的已经很多了,这次爬取APP端的数据.之前我也讲解过APP爬虫,但是没有讲解过Fiddler的配置和使用.这次以微博榜单为案例,介绍APP爬虫流程和数据的可视化(数 ...
最新文章
- hexo-github-博客搭建指南
- hql 语法与详细解释转
- mega2560单片机开发_[MEGA DEAL] Ultimate Java开发和认证指南(59%折扣)
- 从“No space left on device”到删除海量文件
- matlab 可视化 —— axis
- node的里html中写script报错,内置对象 · TypeScript 入门教程
- Java前端如何发送date类型的参数给后端
- 单例模式中的线程安全问题
- vscode中文设置不生效_vscode 无法设置中文怎么办
- 小米无线路由器服务器用户名和密码忘了,小米路由器登陆入口用户名和密码指南...
- 服务器上文件拒绝访问,云服务器文件访问被拒绝访问
- php网易云信im即时通讯和聊天室
- 学习OpenCV——计算邻接区域列表(build_adjoin)
- ipa shell自动打包
- 大学计算机简单的感想,爱计算机的我大学生活感悟
- 软件测试面试考什么综合素质,综合素质测评都考了啥
- 我的世界java版变形模组下载_我的世界变形模组
- Maven中的LastUpdated文件生成原因
- 使用ADB命令卸载安卓设备上的应用
- Android开发基础——RecyclerView
热门文章
- 【Elasticsearch】Elasticsearch 7.6 IDEA 源码环境搭建
- 【Kafka】如何判断一个kafka集群是否稳定
- Spring:@AutoConfigurexxx注解-控制配置类的加载顺序
- MySQL与Oracle主键冲突解决方式
- spark学习-38-Spark的MemoryManager
- 如何从零设计一款牛逼的高并发架构(建议收藏)
- 介绍6款热门的SpringCloud微服务开源项目,总有适合你的!
- SpringBoot部署Jar文件,瘦身优化指南!
- vue+Java后端进行调试时如何解决跨域问题
- Example of Zookeeper and Solr cluster with Docker networking