前言
所有前期准备工作做好了之后,我们就要正式进入python的具体学习中去了。你们准备好了吗?但是,小编不想就此就介绍python的语法,那先从一个比较有趣的turtle库开始来感受下python的编码风格和python的强大。turtle 库
turtle 库应用举例
代码结构和特点
turtle 库
turtle库是python中一个十分流行的绘制图像的函数库,是初学python者最先接触的一个库。想象在电脑屏幕有一只乌龟,在一个笛卡尔坐标系中,从原点(0,0)开始,然后你通过一些命令(我们一般称其为函数),让其在屏幕上走动,留下来的轨迹就是绘制出来的图像了。原理很简单,但我们可以看出使用turtle库的关键在于了解其中的函数的含义,那么我们现在退一步来说,如果要你来设计turtle库,那你会设计出哪些函数呢?
仔细思考一下,其实也不难得出结果。绘图吗,无非就是控制画笔的大小,颜色,粗细,笔走的方向等。实际上turtle库也是在这些方面上进行设计的。下面我们就一起来欣赏几个例子,顺便介绍python代码的结构和特点,对于代码中牵扯到的一些不能理解的地方可以跳过,毕竟我们还没开始介绍语法。
turtle库应用举例
python蟒蛇
code:
#coding:utf_-8
import turtle #导入turtle库
turtle.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.circle(40, 80)
turtle.circle(-40, 80)
turtle.circle(40, 80/2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2/3)
turtle.done()
太阳花
code:
#coding:utf_-8
from turtle import * #导入Turtle库
color('red', 'yellow') #设置画笔和线的颜色
begin_fill() #准备开始填充图形
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill() #填充完成
done()
八卦图
code:
# coding:utf-8
from turtle import *
def yin(radius, color1, color2):
width(3)
color("black", color1)
begin_fill()
circle(radius/2., 180)
circle(radius, 180)
left(180)
circle(-radius/2., 180)
end_fill()
left(90)
up()
forward(radius*0.35)
right(90)
down()
color(color1, color2)
begin_fill()
circle(radius*0.15)
end_fill()
left(90)
up()
backward(radius*0.35)
down()
left(90)
def main():
reset()
yin(200, "black", "white")
yin(200, "white", "black")
ht()
return "Done!"
if __name__ == '__main__':
main()
mainloop()
玫瑰花
code:
#coding:utf_-8
from turtle import *
import time
setup(1000, 800, 0, 0)
speed(0)
penup()
seth(90)
fd(340)
seth(0)
pendown()
speed(5)
begin_fill()
fillcolor('red')
circle(50, 30)
for i in range(10):
fd(1)
left(10)
circle(40, 40)
for i in range(6):
fd(1)
left(3)
circle(80, 40)
for i in range(20):
fd(0.5)
left(5)
circle(80, 45)
for i in range(10):
fd(2)
left(1)
circle(80, 25)
for i in range(20):
fd(1)
left(4)
circle(50, 50)
time.sleep(0.1)
circle(120, 55)
speed(0)
seth(-90)
fd(70)
right(150)
fd(20)
left(140)
circle(140, 90)
left(30)
circle(160, 100)
left(130)
fd(25)
penup()
right(150)
circle(40, 80)
pendown()
left(115)
fd(60)
penup()
left(180)
fd(60)
pendown()
end_fill()
right(120)
circle(-50, 50)
circle(-20, 90)
speed(1)
fd(75)
speed(0)
circle(90, 110)
penup()
left(162)
fd(185)
left(170)
pendown()
circle(200, 10)
circle(100, 40)
circle(-52, 115)
left(20)
circle(100, 20)
circle(300, 20)
speed(1)
fd(250)
penup()
speed(0)
left(180)
fd(250)
circle(-300, 7)
right(80)
circle(200, 5)
pendown()
left(60)
begin_fill()
fillcolor('green')
circle(-80, 100)
right(90)
fd(10)
left(20)
circle(-63, 127)
end_fill()
penup()
left(50)
fd(20)
left(180)
pendown()
circle(200, 25)
penup()
right(150)
fd(180)
right(40)
pendown()
begin_fill()
fillcolor('green')
circle(-100, 80)
right(150)
fd(10)
left(60)
circle(-80, 98)
end_fill()
penup()
left(60)
fd(13)
left(180)
pendown()
speed(1)
circle(-200, 23)
hideturtle()
exitonclick()
小猪佩奇
code:
#coding:utf_-8
import turtle as t
t.pensize(4) # 设置画笔的大小
t.colormode(255) # 设置GBK颜色范围为0-255
t.color((255,155,192),"pink") # 设置画笔颜色和填充颜色(pink)
t.setup(840,500) # 设置主窗口的大小为840*500
t.speed(10) # 设置画笔速度为10
#鼻子
t.pu() # 提笔
t.goto(-100,100) # 画笔前往坐标(-100,100)
t.pd() # 下笔
t.seth(-30) # 笔的角度为-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) # 笔的角度为90度
t.fd(25) # 向前移动25
t.seth(0) # 转换画笔的角度为0
t.fd(10)
t.pd()
t.pencolor(255,155,192) # 设置画笔颜色
t.seth(10)
t.begin_fill()
t.circle(5) # 画一个半径为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) # 顺时针画一个半径为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):
if0<=i<30or60<=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()
代码结构和特点
从上面的代码中可以看出来Python与C一个很明显的区别就是没有了表示代码块的{},在Python中使用缩进来表示代码块,不需要使用大括号{}。
当然,缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数,所以一般为了避免不必要的麻烦,还是建议统一缩进,用4个空格或者一个Tab,缩进是Python最大的语法要求,强制执行的。
注释
在程序中注释是不执行的,是给人阅读的以方便理解程序,一定要养成良好的习惯,一定要在关键代码上书写注释。Python中单行注释以#开头,例如:
#这是注释
print("Hello World")
多行注释用三个单引号
'''
或者三个双引号
"""
将注释括起来,例如:
'''这是注释这是注释'''
"""这是注释这是注释"""
print("Hello World")
输入和输出
接着看一个简单的猜数字小游戏:
num = 56
val = int(input("请输入数字:"))
while val!=num:
if val > num:
print("大了")
val = int(input("请输入数字:"))
else:
print("小了")
val = int(input("请输入数字:"))
print("答对了!")
print()
由上面的代码可知,在print()的括号中加上字符串,就能输出该字符串。具体语法如下所示
1print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)objects:输出对象。可以一次输出多个对象,输出多个对象时,需要用,分隔。
sep:分隔符,用来间隔多个对象,默认值是一个空格。
end:输出结束时补充该参数所指定的字符串。默认值是换行符,我们可以换成其他字符串。
file:要写入的文件对象,默认为标准的系统输出sys.stdout,可以重定义为别的文件。
flush:是否立即把内容输出到流文件,不作缓存,默认为False。(version3.3加入)
比如需要输出500+20=520:
print("500+20=",500+20)
#输出结果:500+20=520
input()
Python提供input()函数来接收用户输入的字符串,具体语法如下
input([prompt])
其中prompt表示提示信息,可以省略。这里需要注意的是input()接收的是字符串,所以当用户输入数字时,input()是将信息作为字符串储存的,所以上面猜数字代码中需要通过int()转换为整数来进行比较。
PS.最后注意一下Python中是区分大小写的,写错了的话程序会报错。
编辑不易,欢迎推广