“守破离” ——《程序员修炼手册》
小知识点汇总:
不要在程序中行开头处增加空格,空格代表缩进。
每个缩进层次使用4个空格。
养成使用注释的习惯,行注释是3个英文单引号。
使用\行连接符
举个例子
#段注释
'''
1.
2.
3.
'''#连接符
>>>a=abcdefg/
hijklmn/
opqrst
#对象
python中,一切皆对象,每个对象由:标识(identity)、类型(type,数字对象/字符串)、值(value)。
对象的本质就是:一个内存块,拥有特定的值,支持特定类型的相关操作。
#引用
python中,变量是对对象的引用
变量位于:栈内存(压栈、出栈)
对象位于:堆内存
变量不需要声明类型(int、str),根据变量引用的对象可以自动确定数据类型。
每个对象都有数据类型,只支持该类型的相关操作。
#标识符
用于变量、函数、类、模块等的名称。
区分大小写,第一个字符必须是字母、下划线。不能使用关键字(keywords),如if、or、while,help(),keywords,quit。
以双下划线的开头和结尾的名称通常有各自的含义,尽量避免使用。
#变量
变量名=表达式
变量在使用前必须先被初始化(先被赋值)
用程序画图
1. 海龟绘图代码
#导入海龟绘图模块
import turtle
t=turtle.Pen()
#这是一个循环
for x in range(360):
t.forward(x)
t.left(59)
我修改了变量,又画了另一个
好晕
2. 图形化程序设计
#导入turtle模块
>>>import turtle
#显示箭头
>>>turtle.showturtle()
#写字符串
>>>turtle.write("520")
#前进300像素
>>>turtle.forward(300)
#画笔颜色改为red
>>>turtle.color("red")
#箭头左转90度
>>>turtle.left(90)
>>>turtle.forward(300)
#去坐标
>>>turtle.goto(0,50)
>>>turtle.goto(0,0)
#抬笔,这样路径就不会画出来
>>>turtle.penup()
>>>turtle.goto(0,300)
#下笔,开始画路径
>>>turtle.pendown()
#画圈
>>>turtle.circle(100)
找到一个画红心的代码,要复杂好多,慢慢学习
https://blog.csdn.net/weixin_44324375/article/details/86683853
3.绘制奥运五环
import turtle
turtle.width(10)
turtle.color("blue")
turtle.circle(50)
turtle.penup()
turtle.goto(120,0)
turtle.pendown()
turtle.color("black")
turtle.circle(50)
turtle.penup()
turtle.goto(240,0)
turtle.pendown()
turtle.color("red")
turtle.circle(50)
turtle.penup()
turtle.goto(60,0)
turtle.pendown()
turtle.color("yellow")
turtle.circle(50)
turtle.penup()
turtle.goto(150,0)
turtle.pendown()
turtle.color("green")
turtle.circle(50)
turtle.hideturtle()
turtle.done()