Day01.初识Python

一.Python的历史

  1. 1989年圣诞节:Guido von Rossum开始写Python语言的编译器。
  2. 1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语言实现的(后面),可以调用C语言的库函数。在最早的版本中,Python已经提供了对“类”,“函数”,“异常处理”等构造块的支持,还有对列表、字典等核心数据类型,同时支持以模块为基础来构造应用程序。
  3. 1994年1月:Python 1.0正式发布。
  4. 2000年10月16日:Python 2.0发布,增加了完整的垃圾回收,提供了对Unicode的支持。与此同时,Python的整个开发过程更加透明,社区对开发进度的影响逐渐扩大,生态圈开始慢慢形成。
  5. 2008年12月3日:Python 3.0发布,它并不完全兼容之前的Python代码,不过因为目前还有不少公司在项目和运维中使用Python 2.x版本,所以Python 3.x的很多新特性后来也被移植到Python 2.6/2.7版本中。

目前我使用的Python 3.8.x的版本是在2020年发布的,Python的版本号分为三段,形如A.B.C。其中A表示大版本号,一般当整体重写,或出现不向后兼容的改变时,增加A;B表示功能更新,出现新功能时增加B;C表示小的改动(例如:修复了某个Bug),只要有修改就增加C。如果对Python的历史感兴趣,可以阅读名为《Python简史》的网络文章。

二.Python的优缺点

2.1 Python的优点

  1. 简单明了,学习曲线低,比很多编程语言都容易上手。
  2. 开放源代码,拥有强大的社区和生态圈,尤其是在数据分析和机器学习领域。
  3. 解释型语言,天生具有平台可移植性,代码可以工作于不同的操作系统。
  4. 对两种主流的编程范式(面向对象编程和函数式编程)都提供了支持。
  5. 代码规范程度高,可读性强,适合有代码洁癖和强迫症的人群。

2.2 Python的缺点

  1. 执行效率稍低,对执行效率要求高的部分可以由其他语言(如:C、C++)编写。
  2. 代码无法加密,但是现在很多公司都不销售卖软件而是销售服务,这个问题会被弱化。
  3. 在开发时可以选择的框架太多(如Web框架就有100多个),有选择的地方就有错误。

三、Python其他方面

3.1 Python的应用领域

目前Python在Web应用后端开发、云基础设施建设、DevOps、网络数据采集(爬虫)、自动化测试、数据分析、机器学习等领域都有着广泛的应用。

主要还是Python的生态好,而且现在很多库都是由C/C++写的,这样的话,Python的执行效率低在某些地方得到的部分的改善,而丰富的库,可以在更多的领域使用,使得Python的使用率在近几年在快速的升高。

3.2 Python的解释器

想要开始Python编程之旅,首先得在自己使用的计算机上安装Python解释器环境,下面将以安装官方的Python解释器为例,讲解如何在不同的操作系统上安装Python环境。官方的Python解释器是用C语言实现的,也是使用最为广泛的Python解释器,通常称之为CPython。除此之外,Python解释器还有Java语言实现的Jython、C#语言实现的IronPython以及PyPy、Brython、Pyston等版本,有兴趣的读者可以自行了解。

3.3 编写Python源代码

可以用文本编辑工具(推荐使用Sublime、Visual Studio Code等高级文本编辑工具)编写Python源代码并用py作为后缀名保存该文件,代码内容如下所示。

个人使用的是PyCharm,一个python的主流(个人感觉吧)编辑器,用起来还是蛮舒服的。

3.4 Python代码中的注释

注释是编程语言的一个重要组成部分,用于在源代码中解释代码的作用从而增强程序的可读性和可维护性,当然也可以将源代码中不需要参与运行的代码段通过注释来去掉,这一点在调试程序的时候经常用到。注释在随源代码进入预处理器或编译时会被移除,不会在目标代码中保留也不会影响程序的执行结果。

  1. 单行注释(以#和空格开头的部分)
  2. 多行注释(三个引号开肉,三个引号结尾)

类似于:

# Python还是可以的。
"""  Python还是可以的。 
"""

3.5 Python编辑器

  1. IDLE(自带的集成开发工具)
  2. IPython(更好的交互式编程工具)
  3. Subline Text(高级文本编辑器)
  4. Visual Studio Code(综合性的编辑器)
  5. PyCharm(Python开发神器)

3.6 Python之禅

在电脑的交互端输入 python 进入python交互界面,再输入 import this。会出现如下:
Python之禅
然后我就偷偷懒的谷歌翻译了一下:

  • 由 Tim Peters 撰写的Python。

  • 优美胜过丑陋。

  • 显式胜于隐式。

  • 简单胜于复杂。

  • 复杂胜于繁杂。

  • 扁平胜于嵌套。

  • 稀疏胜于密实。

  • 可读性很重要。

  • 特殊情况还不足以打破规则。

  • 尽管实用性胜过纯度。

  • 错误绝不能默默传递。

  • 除非明确地保持沉默。

  • 面对模棱两可,拒绝猜测的诱惑。

  • 应该有一种-最好只有一种-明显的方式来做到这一点。

  • 尽管除非您是荷兰人,否则一开始这种方式可能并不明显。

  • 现在总比没有好。

  • 尽管从来没有什么比现在“正确”更好。

  • 如果实现难以解释,那是个坏主意。

  • 如果实现易于解释,则可能是个好主意。

  • 命名空间是一个很棒的主意-让我们做更多这些吧!

3.7 学习使用turtle在屏幕上绘制图形

turtle是Python内置的一个非常有趣的模块,特别适合对计算机程序设计进行初体验的小伙伴,它最早是Logo语言的一部分,Logo语言是Wally Feurzig和Seymour Papert在1966发明的编程语言。

tutle的详细函数介绍请看 turtle库官方文档

  1. 一个简单的非同心正方形
import random

for i in range(10):
    turtle.pensize(3)
    turtle.pencolor("red")
    
    turtle.forward(100 - 10*i)  # 向前行进 100 个单位
    turtle.right(90)  # 向顺时针方向转动的度数
    turtle.forward(100 - 10*i) 
    turtle.right(90)  
    turtle.forward(100 - 10*i) 
    turtle.right(90) 
    turtle.forward(100 - 10*i)
    turtle.right(90)
    
turtle.mainloop()

2.绘制国旗

import turtle

def draw_rectangle(x, y, width, height):
    # 绘制矩形
    turtle.goto(x, y)  # 从(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)  # 画个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.fd(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()

if __name__ == "__main__":
    main()

3.绘制小猪佩奇

from turtle import *


def nose(x,y):
    """画鼻子"""
    penup()
    # 将海龟移动到指定的坐标
    goto(x,y)
    pendown()
    # 设置海龟的方向(0-东、90-北、180-西、270-南)
    setheading(-30)
    begin_fill()
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i <90:
            a = a + 0.08
            # 向左转3度
            left(3)
            # 向前走
            forward(a)
        else:
            a = a - 0.08
            left(3)
            forward(a)
    end_fill()
    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    # 设置画笔的颜色(红, 绿, 蓝)
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()
    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()


def head(x, y):
    """画头"""
    color((255, 155, 192), "pink")
    penup()
    goto(x,y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300, -30)
    circle(100, -60)
    circle(80, -100)
    circle(150, -20)
    circle(60, -95)
    setheading(161)
    circle(-300, 15)
    penup()
    goto(-100, 100)
    pendown()
    setheading(-30)
    a = 0.4
    for i in range(60):
        if 0<= i < 30 or 60 <= i < 90:
            a = a + 0.08
            lt(3) #向左转3度
            fd(a) #向前走a的步长
        else:
            a = a - 0.08
            lt(3)
            fd(a)
    end_fill()


def ears(x,y):
    """画耳朵"""
    color((255, 155, 192), "pink")
    penup()
    goto(x, y)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 54)
    end_fill()
    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 56)
    end_fill()


def eyes(x,y):
    """画眼睛"""
    color((255, 155, 192), "white")
    penup()
    setheading(90)
    forward(-20)
    setheading(0)
    forward(-95)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
    color((255, 155, 192), "white")
    penup()
    seth(90)
    forward(-25)
    seth(0)
    forward(40)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()


def cheek(x,y):
    """画脸颊"""
    color((255, 155, 192))
    penup()
    goto(x,y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()


def mouth(x,y):
    """画嘴巴"""
    color(239, 69, 19)
    penup()
    goto(x, y)
    pendown()
    setheading(-80)
    circle(30, 40)
    circle(40, 80)


def setting():
    """设置参数"""
    pensize(4)
    # 隐藏海龟
    hideturtle()
    colormode(255)
    color((255, 155, 192), "pink")
    setup(840, 500)
    speed(10)


def main():
    """主函数"""
    setting() 
    nose(-100, 100)
    head(-69, 167)
    ears(0, 160)
    eyes(0, 140)
    cheek(80, 10)
    mouth(-20, 30)
    done()


if __name__ == '__main__':
    main()

4.绘制好看的闹钟

import turtle
from datetime import *
 
# 抬起画笔,向前运动一段距离放下
def Skip(step):
    turtle.penup()
    turtle.forward(step)
    turtle.pendown()
 
 
def mkHand(name, length):
    # 注册Turtle形状,建立表针Turtle
    turtle.reset()
    Skip(-length * 0.1)
    # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
    turtle.begin_poly()
    turtle.forward(length * 1.1)
    # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
    turtle.end_poly()
    # 返回最后记录的多边形。
    handForm = turtle.get_poly()
    turtle.register_shape(name, handForm)
 
 
def Init():
    global secHand, minHand, hurHand, printer
    # 重置Turtle指向北
    turtle.mode("logo")
    # 建立三个表针Turtle并初始化
    mkHand("secHand", 135)
    mkHand("minHand", 125)
    mkHand("hurHand", 90)
    secHand = turtle.Turtle()
    secHand.shape("secHand")
    minHand = turtle.Turtle()
    minHand.shape("minHand")
    hurHand = turtle.Turtle()
    hurHand.shape("hurHand")
 
    for hand in secHand, minHand, hurHand:
        hand.shapesize(1, 1, 3)
        hand.speed(0)
 
        # 建立输出文字Turtle
    printer = turtle.Turtle()
 
    # 隐藏画笔的turtle形状
    printer.hideturtle()
    printer.penup()
 
 
def SetupClock(radius):
    # 建立表的外框
    turtle.reset()
    turtle.pensize(7)
    turtle.pencolor("#ff5500")
    turtle.fillcolor("green")
 
    for i in range(60):
        Skip(radius)
        if i % 5 == 0:
            turtle.forward(20)
            Skip(-radius - 20)
 
            Skip(radius + 20)
            if i == 0:
                turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
            elif i == 30:
                Skip(25)
                turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
                Skip(-25)
            elif (i == 25 or i == 35):
                Skip(20)
                turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
                Skip(-20)
            else:
                turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))
            Skip(-radius - 20)
        else:
            turtle.dot(5)
            Skip(-radius)
        turtle.right(6)
 
 
def Week(t):
    week = ["星期一", "星期二", "星期三",
            "星期四", "星期五", "星期六", "星期日"]
    return week[t.weekday()]
 
def Date(t):
    y = t.year
    m = t.month
    d = t.day
    return "%s-%d-%d" % (y, m, d)
 
def Tick():
    # 绘制表针的动态显示
    t = datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second / 60.0
    hour = t.hour + minute / 60.0
    secHand.setheading(6 * second)
    minHand.setheading(6 * minute)
    hurHand.setheading(30 * hour)
 
    turtle.tracer(False)
 
    printer.forward(65)
    printer.write(Week(t), align="center",
                  font=("Courier", 14, "bold"))
    printer.back(130)
    printer.write(Date(t), align="center",
                  font=("Courier", 14, "bold"))
    printer.home()
    turtle.tracer(True)
 
    # 100ms后继续调用tick
    turtle.ontimer(Tick, 100)
 
 
def main():
    # 打开/关闭龟动画,并为更新图纸设置延迟。
    turtle.tracer(False)
    Init()
    SetupClock(160)
    turtle.tracer(True)
    Tick()
    turtle.mainloop()

if __name__ == "__main__":
    main()

5.绘制一朵玫瑰花

import turtle
import time

turtle.speed(5)

# 设置初始位置  
turtle.penup()  
turtle.left(90)  
turtle.fd(200)  
turtle.pendown()  
turtle.right(90)

# 花蕊  
turtle.fillcolor("red")  
turtle.begin_fill()  
turtle.circle(10,180)  
turtle.circle(25,110)  
turtle.left(50)  
turtle.circle(60,45)  
turtle.circle(20,170)  
turtle.right(24)  
turtle.fd(30)  
turtle.left(10)  
turtle.circle(30,110)  
turtle.fd(20)  
turtle.left(40)  
turtle.circle(90,70)  
turtle.circle(30,150)  
turtle.right(30)  
turtle.fd(15)  
turtle.circle(80,90)  
turtle.left(15)  
turtle.fd(45)  
turtle.right(165)  
turtle.fd(20)  
turtle.left(155)  
turtle.circle(150,80)  
turtle.left(50)  
turtle.circle(150,90)  
turtle.end_fill()  


# 花瓣1  
turtle.left(150)  
turtle.circle(-90,70)  
turtle.left(20)  
turtle.circle(75,105)  
turtle.setheading(60)  
turtle.circle(80,98)  
turtle.circle(-90,40)  
  

# 花瓣2  
turtle.left(180)  
turtle.circle(90,40)  
turtle.circle(-80,98)  
turtle.setheading(-83)  


# 叶子1  
turtle.fd(30)  
turtle.left(90)  
turtle.fd(25)  
turtle.left(45)  
turtle.fillcolor("green")  
turtle.begin_fill()  
turtle.circle(-80,90)  
turtle.right(90)  
turtle.circle(-80,90)  
turtle.end_fill()  


turtle.right(135)  
turtle.fd(60)  
turtle.left(180)  
turtle.fd(85)  
turtle.left(90)  
turtle.fd(80)  


# 叶子2  
turtle.right(90)  
turtle.right(45)  
turtle.fillcolor("green")  
turtle.begin_fill()  
turtle.circle(80,90)  
turtle.left(90)  
turtle.circle(80,90)  
turtle.end_fill()  


turtle.left(135)  
turtle.fd(60)  
turtle.left(180)  
turtle.fd(60)  
turtle.right(90)  
turtle.circle(200,60) 

turtle.hideturtle()
turtle.mainloop()

6.绘制一棵树
注: 这个代码可能需要十秒(我电脑比较垃圾)才能出现效果,别直接关了。

from turtle import *
from random import *
from math import *

def tree(n,l):
    pd()#下笔
    #阴影效果
    t = cos(radians(heading()+45))/8+0.25
    pencolor(t,t,t)
    pensize(n/3)
    forward(l)#画树枝

    if n>0:
        b = random()*15+10 #右分支偏转角度
        c = random()*15+10 #左分支偏转角度
        d = l*(random()*0.25+0.7) #下一个分支的长度
        #右转一定角度,画右分支
        right(b)
        tree(n-1,d)
        #左转一定角度,画左分支
        left(b+c)
        tree(n-1,d)
        #转回来
        right(c)
    else:
        #画叶子
        right(90)
        n=cos(radians(heading()-45))/4+0.5
        pencolor(n,n*0.8,n*0.8)
        circle(3)
        left(90)
        #添加0.3倍的飘落叶子
        if(random()>0.7):
            pu()
            #飘落
            t = heading()
            an = -40 +random()*40
            setheading(an)
            dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
            forward(dis)
            setheading(t)
            #画叶子
            pd()
            right(90)
            n = cos(radians(heading()-45))/4+0.5
            pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4)
            circle(2)
            left(90)
            pu()
            #返回
            t=heading()
            setheading(an)
            backward(dis)
            setheading(t)
    pu()
    backward(l)#退回

bgcolor(0.5,0.5,0.5)#背景色
ht()#隐藏turtle
speed(10)#速度 1-10渐进,0 最快
tracer(0,0)
pu()#抬笔
backward(100)
left(90)#左转90度
pu()#抬笔
backward(300)#后退300
tree(12,100)#递归7层
done()

总结

文中出现的主要内容详情请见Python-100-Days,个别的代码(直接复制就能用)也是给出了相关链接。

第一次看这么多东西,感觉还是受益良多。

其实那个turtle库的话(最开始多看看官方文档总是不错的,特别是对于某些函数的细节部分),还是得去实践一下,最好是拿支笔画画,然后在不断调参数吧。

不像很多的大佬,直接敲出来。我还是先去摸索摸索,再试试。

今天是第一天(1/100),加油!溜了遛了,哈哈哈。

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值