python实操(一)

五星红旗的绘制

  • 库:辅助做相应的一些操作,减少工作的重复性,降低代码量和难度。封装好各种操作,不用重复造轮子。
  • turtle 绘图库
  • django flask tornado 制作网站后台
  • pandas numpy 数据分析
  • scipy 科学计算
  • pygame 制作游戏
  • tensorflow pytorch keras 人工智能
  • pyspark pyhive 大数据
  • pymysql 数据库
  • Python与其他编程语言相比,变量类型是差不多的。(多行字符串用三引号 表示)
  • list 列表(数据变化频繁,读取速度比较慢)
  • dict 字典(键值对,键是可hash的,键是不可变对象)
  • tuple 元组(数据不变化,会有多种数据,如身份证号码,读取数据快)
  • set 集合

Python逻辑语句

  • if条件语句
  • 嵌套语句
  • for循环语句

函数

  • def 表示函数

图像处理

  • 单通道:黑白/灰度
  • 双通道:RGB

本次实操运用到一个简单的Python绘图库——turtle库
一些常用方法

  • pensize 设置画笔粗细(根据参数调整)
  • pencolor 设置画笔颜色(根据参数调整)
  • forward 朝前走(100个像素点)
  • right 从当前位置右转弯
  • left 从当前位置左转弯
  • speed 设置画笔速度
  • penup 放下画笔,结束绘图

Python语句——for 循环
for i in range() i为整型变量,从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()

在这里插入图片描述

发布了5 篇原创文章 · 获赞 0 · 访问量 120

猜你喜欢

转载自blog.csdn.net/weixin_45058912/article/details/104293709