python学习一些小笔记与大家分享
前言
python的turtle画图功能,以几个实验为例,
另外还有几个输入输出相关知识
实验一:
# 学习利用 Turtle 模块编写程序
# 画出圆形、等边三角形、正方形,
# 并填充颜色。参考程序如下
import turtle
turtle.speed(1)
turtle.pensize(4)
turtle.pencolor("black")
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.circle(50)
turtle.penup()
turtle.goto(100, 0)
turtle.pendown()
turtle.begin_fill()
turtle.color("red")
turtle.circle(50, steps=3)
turtle.end_fill()
turtle.penup()
turtle.goto(100, 100)
turtle.pendown()
turtle.begin_fill()
turtle.color("black")
turtle.circle(50, steps=4)
turtle.end_fill()
turtle.penup()
turtle.goto(0, 100)
turtle.pendown()
turtle.begin_fill()
turtle.color("blue")
turtle.circle(50, steps=6)
turtle.end_fill()
turtle.done()
实验二
# (Turtle: draw shapes) Write a program that draws
# a triangle, square, pentagon, hexagon,
# and octagon, as shown in Figure.
# Note that the bottom edges of these
# shapes are parallel to the x-axis.
# (Hint: For a triangle with a bottom
# line parallel to the x-axis, set
# the turtle's heading to 60 degrees.)
import turtle
turtle.pensize(3) # Set pen thickness to 3 pixels
turtle.speed(1)
turtle.penup() # Pull the pen up
turtle.goto(-100, -50)
turtle.pendown() # Pull the pen down
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("red")
turtle.circle(40, steps=3) # Draw a circle
turtle.end_fill() # Fill the shape
turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("blue")
turtle.circle(40, steps=4) # Draw a triangle
turtle.end_fill() # Fill the shape
turtle.penup()
turtle.goto(100, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("green")
turtle.circle(40, steps=5) # Draw a square
turtle.end_fill() # Fill the shape
turtle.penup()
turtle.goto(100, 50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("blue")
turtle.circle(40, steps=6) # Draw a square
turtle.end_fill() # Fill the shape
turtle.penup()
turtle.goto(0, 50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color("black")
turtle.circle(40, steps=8) # Draw a square
turtle.end_fill() # Fill the shape
turtle.hideturtle()
turtle.done()
补充内容(python的其他基础知识)
1,输出函数(print)
fomat方法介绍
print('{},{}'.format('1', '2'))
print('{:.2f}'.format(12))
运行结果
2,输入函数(input)
调用方法input("可以在此处添加一些提示语句")
s = eval(input())
print(s)