python practical operation (a)

Drawing flag

  • Library: Auxiliary do some of the operations, reduce duplication of work, reduce the amount of code and complexity. Good encapsulation of various operations, without making the wheel is repeated.
  • turtle graphics library
  • django flask tornado backstage production site
  • pandas numpy data analysis
  • scipy Scientific Computing
  • pygame making games
  • tensorflow pytorch keras Artificial Intelligence
  • pyspark pyhive Big Data
  • pymysql database
  • Compared with other Python programming language, variable type is the same. (Represented by three multi-line character string in quotation marks)
  • list files (data changes frequently, the reading speed is relatively slow)
  • dict dictionary (key-value pair, the key is a hash key is immutable object)
  • tuple tuple (data does not change, there will be a variety of data, such as identification numbers, read data fast)
  • set collection

Python logic statements

  • if conditional statement
  • Nested statements
  • for loop

function

  • def represents a function

Image Processing

  • Single Channel: Black and white / gray
  • Dual-channel: RGB

The practical operation to use a simple graphics library Python library --turtle
some common methods

  • pensize set the brush thickness (adjusted parameter)
  • pencolor pen color set (adjusted parameter)
  • forward to go forward (100 pixels)
  • right turn right from the current position
  • left from the current position of the left turn
  • speed setting brush speed
  • penup down his brush, to end the drawing

Python statements --for loop
for i in range () i is an integer variable from 0

"""
用Python的turtle模块绘制国旗
"""
import turtle


def draw_rectangle(x, y, width, height):
    """绘制矩形"""
    turtle.goto(x, y)
    turtle.pencolor('red')
    turtle.fillcolor('red')
    turtle.begin_fill()
    for i in range(2):
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)
    turtle.end_fill()


def draw_star(x, y, radius):
    """绘制五角星"""
    turtle.setpos(x, y)
    pos1 = turtle.pos()
    turtle.circle(-radius, 72)
    pos2 = turtle.pos()
    turtle.circle(-radius, 72)
    pos3 = turtle.pos()
    turtle.circle(-radius, 72)
    pos4 = turtle.pos()
    turtle.circle(-radius, 72)
    pos5 = turtle.pos()
    turtle.color('yellow', 'yellow')
    turtle.begin_fill()
    turtle.goto(pos3)
    turtle.goto(pos1)
    turtle.goto(pos4)
    turtle.goto(pos2)
    turtle.goto(pos5)
    turtle.end_fill()


def main():
    """主程序"""
    #设置画笔速度
    turtle.speed(12)
    # 抬起笔
    turtle.penup()
    x, y = -270, -180
    # 画国旗主体
    width, height = 540, 360
    draw_rectangle(x, y, width, height)
    # 画大星星
    pice = 22
    center_x, center_y = x + 5 * pice, y + height - pice * 5
    turtle.goto(center_x, center_y)
    turtle.left(90)
    turtle.forward(pice * 3)
    turtle.right(90)
    draw_star(turtle.xcor(), turtle.ycor(), pice * 3)
    x_poses, y_poses = [10, 12, 12, 10], [2, 4, 7, 9]
    # 画小星星
    for x_pos, y_pos in zip(x_poses, y_poses):
        turtle.goto(x + x_pos * pice, y + height - y_pos * pice)
        turtle.left(turtle.towards(center_x, center_y) - turtle.heading())
        turtle.forward(pice)
        turtle.right(90)
        draw_star(turtle.xcor(), turtle.ycor(), pice)
    # 隐藏海龟
    turtle.ht()
    # 显示绘图窗口
    turtle.mainloop()

main()

Here Insert Picture Description

Released five original articles · won praise 0 · Views 120

Guess you like

Origin blog.csdn.net/weixin_45058912/article/details/104293709