隔天更新python文章,我希望用我的努力换来劳动的成果帮助更多的人掌握一门技术,因此我要更加努力。 |
|
Python爱好者社区 · “给我滚出贵大!”郑强出任贵州大学校长,打算 ... · 2 天前 |
|
Python爱好者社区 · DeepSeek 最新中国大学排名 · 16 小时前 |
|
Python开发者 · 国产 DeepSeek V3 ... · 4 天前 |
|
Python爱好者社区 · 史上最强!PINN杀疯了 · 3 天前 |
|
Python爱好者社区 · 英伟达憾失DeepSeek关键人才?美国放走 ... · 3 天前 |
美国队长的锅
emmmmmmmm.......没错就是他的锅
代码
# 所需依赖: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
()
效果图
小猪佩奇
代码
# coding:utf-8
import
turtle
as
t
t
.
pensize
(
4
)
t
.
hideturtle
()
t
.
colormode
(
255
)
t
.
color
((
255
,
155
,
192
),
"pink"
)
t
.
setup
(
840
,
500
)
t
.
speed
(
10
)
#鼻子
t
.
pu
()
t
.
goto
(-
100
,
100
)
t
.
pd
()
t
.
seth
(-
30
)
t
.
begin_fill
()
a
=
0.4
for
i
in
range
(
120
):
if
0
<=
i
<
30
or
60
<=
i
<
90
:
a
=
a
+
0.08
t
.
lt
(
3
)
#向左转3度
t
.
fd
(
a
)
#向前走a的步长
else
:
a
=
a
-
0.08
t
.
lt
(
3
)
t
.
fd
(
a
)
t
.
end_fill
()
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
25
)
t
.
seth
(
0
)
t
.
fd
(
10
)
t
.
pd
()
t
.
pencolor
(
255
,
155
,
192
)
t
.
seth
(
10
)
t
.
begin_fill
()
t
.
circle
(
5
)
t
.
color
(
160
,
82
,
45
)
t
.
end_fill
()
t
.
pu
()
t
.
seth
(
0
)
t
.
fd
(
20
)
t
.
pd
()
t
.
pencolor
(
255
,
155
,
192
)
t
.
seth
(
10
)
t
.
begin_fill
()
t
.
circle
(
5
)
t
.
color
(
160
,
82
,
45
)
t
.
end_fill
()
#头
t
.
color
((
255
,
155
,
192
),
"pink"
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
41
)
t
.
seth
(
0
)
t
.
fd
(
0
)
t
.
pd
()
t
.
begin_fill
()
t
.
seth
(
180
)
t
.
circle
(
300
,-
30
)
t
.
circle
(
100
,-
60
)
t
.
circle
(
80
,-
100
)
t
.
circle
(
150
,-
20
)
t
.
circle
(
60
,-
95
)
t
.
seth
(
161
)
t
.
circle
(-
300
,
15
)
t
.
pu
()
t
.
goto
(-
100
,
100
)
t
.
pd
()
t
.
seth
(-
30
)
a
=
0.4
for
i
in
range
(
60
):
if
0
<=
i
<
30
or
60
<=
i
<
90
:
a
=
a
+
0.08
t
.
lt
(
3
)
#向左转3度
t
.
fd
(
a
)
#向前走a的步长
else
:
a
=
a
-
0.08
t
.
lt
(
3
)
t
.
fd
(
a
)
t
.
end_fill
()
#耳朵
t
.
color
((
255
,
155
,
192
),
"pink"
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(-
7
)
t
.
seth
(
0
)
t
.
fd
(
70
)
t
.
pd
()
t
.
begin_fill
()
t
.
seth
(
100
)
t
.
circle
(-
50
,
50
)
t
.
circle
(-
10
,
120
)
t
.
circle
(-
50
,
54
)
t
.
end_fill
()
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(-
12
)
t
.
seth
(
0
)
t
.
fd
(
30
)
t
.
pd
()
t
.
begin_fill
()
t
.
seth
(
100
)
t
.
circle
(-
50
,
50
)
t
.
circle
(-
10
,
120
)
t
.
circle
(-
50
,
56
)
t
.
end_fill
()
#眼睛
t
.
color
((
255
,
155
,
192
),
"white"
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(-
20
)
t
.
seth
(
0
)
t
.
fd
(-
95
)
t
.
pd
()
t
.
begin_fill
()
t
.
circle
(
15
)
t
.
end_fill
()
t
.
color
(
"black"
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
12
)
t
.
seth
(
0
)
t
.
fd
(-
3
)
t
.
pd
()
t
.
begin_fill
()
t
.
circle
(
3
)
t
.
end_fill
()
t
.
color
((
255
,
155
,
192
),
"white"
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(-
25
)
t
.
seth
(
0
)
t
.
fd
(
40
)
t
.
pd
()
t
.
begin_fill
()
t
.
circle
(
15
)
t
.
end_fill
()
t
.
color
(
"black"
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
12
)
t
.
seth
(
0
)
t
.
fd
(-
3
)
t
.
pd
()
t
.
begin_fill
()
t
.
circle
(
3
)
t
.
end_fill
()
#腮
t
.
color
((
255
,
155
,
192
))
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(-
95
)
t
.
seth
(
0
)
t
.
fd
(
65
)
t
.
pd
()
t
.
begin_fill
()
t
.
circle
(
30
)
t
.
end_fill
()
#嘴
t
.
color
(
239
,
69
,
19
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
15
)
t
.
seth
(
0
)
t
.
fd
(-
100
)
t
.
pd
()
t
.
seth
(-
80
)
t
.
circle
(
30
,
40
)
t
.
circle
(
40
,
80
)
#身体
t
.
color
(
"red"
,(
255
,
99
,
71
))
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(-
20
)
t
.
seth
(
0
)
t
.
fd
(-
78
)
t
.
pd
()
t
.
begin_fill
()
t
.
seth
(-
130
)
t
.
circle
(
100
,
10
)
t
.
circle
(
300
,
30
)
t
.
seth
(
0
)
t
.
fd
(
230
)
t
.
seth
(
90
)
t
.
circle
(
300
,
30
)
t
.
circle
(
100
,
3
)
t
.
color
((
255
,
155
,
192
),(
255
,
100
,
100
))
t
.
seth
(-
135
)
t
.
circle
(-
80
,
63
)
t
.
circle
(-
150
,
24
)
t
.
end_fill
()
#手
t
.
color
((
255
,
155
,
192
))
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(-
40
)
t
.
seth
(
0
)
t
.
fd
(-
27
)
t
.
pd
()
t
.
seth
(-
160
)
t
.
circle
(
300
,
15
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
15
)
t
.
seth
(
0
)
t
.
fd
(
0
)
t
.
pd
()
t
.
seth
(-
10
)
t
.
circle
(-
20
,
90
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
30
)
t
.
seth
(
0
)
t
.
fd
(
237
)
t
.
pd
()
t
.
seth
(-
20
)
t
.
circle
(-
300
,
15
)
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
20
)
t
.
seth
(
0
)
t
.
fd
(
0
)
t
.
pd
()
t
.
seth
(-
170
)
t
.
circle
(
20
,
90
)
#脚
t
.
pensize
(
10
)
t
.
color
((
240
,
128
,
128
))
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(-
75
)
t
.
seth
(
0
)
t
.
fd
(-
180
)
t
.
pd
()
t
.
seth
(-
90
)
t
.
fd
(
40
)
t
.
seth
(-
180
)
t
.
color
(
"black"
)
t
.
pensize
(
15
)
t
.
fd
(
20
)
t
.
pensize
(
10
)
t
.
color
((
240
,
128
,
128
))
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
40
)
t
.
seth
(
0
)
t
.
fd
(
90
)
t
.
pd
()
t
.
seth
(-
90
)
t
.
fd
(
40
)
t
.
seth
(-
180
)
t
.
color
(
"black"
)
t
.
pensize
(
15
)
t
.
fd
(
20
)
#尾巴
t
.
pensize
(
4
)
t
.
color
((
255
,
155
,
192
))
t
.
pu
()
t
.
seth
(
90
)
t
.
fd
(
70
)
t
.
seth
(
0
)
t
.
fd
(
95
)
t
.
pd
()
t
.
seth
(
0
)
t
.
circle
(
70
,
20
)
t
.
circle
(
10
,
330
)
t
.
circle
(
70
,
30
)
t
.
done
()
效果图
蓝胖子
代码
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author: dong dong
# @Env: python 3.6
from
turtle
import
*
# 无轨迹跳跃
def
my_goto
(
x
,
y
):
penup
()
goto
(
x
,
y
)
pendown
()
# 眼睛
def
eyes
():
tracer
(
False
)
a
=
2.5
for
i
in
range
(
120
):
if
0
<=
i
<
30
or
60
<=
i
<
90
:
a
-=
0.05
lt
(
3
)
fd
(
a
)
else
:
a
+=
0.05
lt
(
3
)
fd
(
a
)
tracer
(
True
)
# 胡须
def
beard
():
my_goto
(-
37
,
135
)
seth
(
165
)
fd
(
60
)
my_goto
(-
37
,
125
)
seth
(
180
)
fd
(
60
)
my_goto
(-
37
,
115
)
seth
(
193
)
fd
(
60
)
my_goto
(
37
,
135
)
seth
(
15
)
fd
(
60
)
my_goto
(
37
,
125
)
seth
(
0
)
fd
(
60
)
my_goto
(
37
,
115
)
seth
(-
13
)
fd
(
60
)
# 嘴巴
def
mouth
():
my_goto
(
5
,
148
)
seth
(
270
)
fd
(
100
)
seth
(
0
)
circle
(
120
,
50
)
seth
(
230
)
circle
(-
120
,
100
)
# 围巾
def
scarf
():
fillcolor
(
'#e70010'
)
begin_fill
()
seth
(
0
)
fd
(
200
)
circle
(-
5
,
90
)
fd
(
10
)
circle
(-
5
,
90
)
fd
(
207
)
circle
(-
5
,
90
)
fd
(
10
)
circle
(-
5
,
90
)
end_fill
|
Python爱好者社区 · “给我滚出贵大!”郑强出任贵州大学校长,打算把树全砍掉,学生愤怒抗议,4年后事情反转 2 天前 |
|
Python爱好者社区 · DeepSeek 最新中国大学排名 16 小时前 |
|
Python开发者 · 国产 DeepSeek V3 被秒成"前浪"?谷歌开放最强 Gemini 2.0 全家桶:速度快60倍,上下文还长16倍! 4 天前 |
|
Python爱好者社区 · 史上最强!PINN杀疯了 3 天前 |
|
Python爱好者社区 · 英伟达憾失DeepSeek关键人才?美国放走AI「钱学森」,哈佛教授痛心疾首 3 天前 |
|
IT时代网 · 北京扫黄 一帮互联网圈的人坐不住了!徐小平、papi酱等纷纷辟谣 8 年前 |
|
21财闻汇 · 今天,国家动真格,老赖末日彻底来了! 7 年前 |
|
互联网思想 · 硅谷预测未来5年10大商业趋势,最后一条正在发生! 7 年前 |
|
人民日报 · 来了!新闻早班车 7 年前 |
|
新华社 · 这首歌我们都会唱,但是没想到还有这些故事…… 7 年前 |