Python介绍

Python是一种广泛使用的解释型、高级编程语言。python有3.x和2.x量大版本,并且不兼容(使用3.x)

解释型—编译型:

源文件 —–(编译)→ 可执行文件 —–(运行)→ 程序功能

源文件 —–(解释)→ 程序功能

安装Python环境

python官网下载的3.7.8版本。安装过程中记得选择“Add PYthon 3.x to PATH”选项,会帮助我们将python解释器添加到Path环境变量中。完成后再cmd来检查是否安装成功。

1
2
>python --version
3.7.8

编写代码的工具

交互式环境

cmd中输入python进入到python的交互式环境中。所谓的交互式环境,就是说输一行代码回车,代码马上会被执行,如果有代码产出结果,那么结果就会被显示在窗口中。

如果希望退出交互式环境,可以在交互式环境中输入quit()

或者在开始菜单里搜索IDLE

文本编辑器-vscode

Hello,world

1
print('Hello, world')

书写代码后,将上面的代码命名为hello.py。windows系统中,在当前目录下按住shift再点击右键,会出现“命令行提示符”选项,(出现了windows powershell,没用过)点击打开。输入命令

1
python hello.py

备注:感觉powershell和cmd区别不大,且可以相互转化(暂时不知),背景颜色亮一点??

打开方式:win+R powershell 和cmd一样集成开发环境-PyCharm

注释代码

两种形式的注释:

单行注释:以#和空格开头

多行注释:以三个引号"""开头,三个引号"""结尾

1
2
3
4
5
6
7
# 单行注释:以#和空格开头
"""
多行注释
以三个引号开头
三个引号结尾
"""
print('Hello, world')

PyCharm

多行缩进tab,多行取消缩进shift+tab

Hello,world

这个看着有点呆。。。

默认新建项目test后,存在一个main.py文件(版本。。)

熟悉pycharm

新建

file→new。test_guess.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from random import randint

def play():
    random_init = randint(0,100)

    while True: # 区分大小写
        user_guess = int(input("What number did you guess(0-100)?"))

        if user_guess == random_init:
            print("You found the number ({random_init}). Congrats!")
            break
        #if结束后好像不会合理缩进,需要手动缩进?---缩进敏感,缩进很重要!
        if user_guess < random_init:
            print("Your number is less than the number we guessed.")
            continue
        if user_guess > random_init:
            print("Your number is more than the number we guessed.")
            continue

if __name__ == '__main__':
    play()

运行

  • ctrl+shift+f10
  • 右键 run test_guess
  • 由于程序具备__name__从句,可以点击左侧绿色小箭头,选择 run test_guess

Debug

暂时先不管

代码测试

默认情况下,unittest被用作测试运行器,而还支持其他测试框架,如pytest、nose、doctest、tox和trial。

如以下步骤可以为项目选择pytest测试运行器:

  1. 打开settings/Preferences→Tools→Python Integrated tools
  2. 在默认测试运行器字段选择pytest
  3. 点击ok保存设置

但此次,仍采用默认的测试器unittest。

下面没看懂。。。

搜索与导航

搜索快捷键:

  • 在当前文件中搜索代码段:在 Mac 系统中使用 Cmd+F 键,在 Windows 或 Linux 系统中使用 Ctrl+F 键。

  • 在整个项目中搜索代码段:在 Mac 系统中使用 Cmd+Shift+F 键,在 Windows 或 Linux 系统中使用 Ctrl+Shift+F 键。

  • 搜索类:在 Mac 系统中使用 Cmd+O 键,在 Windows 或 Linux 系统中使用 Ctrl+N 键。

  • 搜索文件:在 Mac 系统中使用 Cmd+Shift+O 键,在 Windows 或 Linux 系统中使用 Ctrl+Shift+N 键。

  • 如果你不知道要搜索的是文件、类还是代码段,则搜索全部:按两次 Shift 键。

导航快捷键:

  • 前往变量的声明:在 Mac 系统中使用 Cmd 键,在 Windows 或 Linux 系统中使用 Ctrl 键,然后单击变量。

  • 寻找类、方法或文件的用法:使用 Alt+F7 键。

  • 查看近期更改:使用 Shift+Alt+C 键,或者在主菜单中点击 View → Recent Changes。

  • 查看近期文件:在 Mac 系统中使用 Cmd+E 键,在 Windows 或 Linux 系统中使用 Ctrl+E 键,或者在主菜单中点击 View → Recent Files。

  • 多次跳转后在导航历史中前进和后退:在 Mac 系统中使用 Cmd+[ / Cmd+] 键,在 Windows 或 Linux 系统中使用 Ctrl+Alt+Left / Ctrl+Alt+Right 键。

更多细节,参见官方文档

版本控制

turtle

海龟。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# setup设置窗体大小及位置,后俩参数可省
turtle.setup(width,height,startx,starty)

# 画笔控制函数
turtle.penup() # 画笔抬起,不画在画布上
turtle.pendown() # 画笔落下,画在画布上
turtle.pensize(width) # 画笔宽度,海龟的腰围
turtle.pencolor(color) # color为颜色字符串或rgb值 
# 字符串pencolor("purple") 小数值pencolor(0.63,0.13,0.94) 元组值pencolor((0.63,0.13,0.94))

# 运动控制函数
turtle.forword(d) # 海龟走直线,d为行进距离,可以为负数
turtle.circle(r,extent=None) # 根据半径r绘制extent角度的弧形,90为1/4
# turtle.circle(100)默认圆心的位置为左侧(上方),若为负,半径在右侧

# 方向控制函数
turtle.sethending(angle) # 改变行进方向,海龟走角度。绝对角度
turtle.left(angle)# 海龟向左转,海龟角度
turtle.right(angle)# 海龟向右转

空间坐标体系

绝对坐标,中心就是xy的中心

海龟坐标,以当前点为中心。

1
2
3
4
5
6
turtle.goto(100,100)
# 让海龟从当前位置到达该位置,单位像素

turtle.fd(d) # 正前方向
turtle.bk(d) # 反方向
turtle.circle(r,angle) #当前位置左侧的某一点为圆心进行曲线

RGB色彩模式

由三种颜色构成的万物色。RGB每色的取值范围0-255整数或0-1小数,白色:255,255,255 1,1,1黄色:255,255,0 1,1,0

默认采用小数值,可切换为整数值。turtle.colormode(mode)->mode=1.0/255为小数值模式和整数值模式。

import

improt turtle下面代码使用库名.函数

from turtle import* 就可以直接使用函数,但有可能会因为同名的函数产生冲突。

import 库名 as 库别名。就可以使用,库别名.函数

画蟒蛇

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#PythonDraw.py
# improt turtle下面代码使用库名.函数
# from turtle import* 就可以直接使用函数,但有可能会因为同名的函数产生冲突
import turtle
turtle.setup(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
    turtle.circle(40, 80)
    turtle.circle(-40, 80)
turtle.circle(40, 80/2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2/3)
turtle.done()

画正方形

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import turtle

turtle.pensize(4)
turtle.pencolor('red')

turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)

turtle.mainloop()

画国旗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
用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()


if __name__ == '__main__':
    main()

画小猪佩奇

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
绘制小猪佩奇
"""
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()

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

参考:PyCharm安装教程及使用教程(2020最新版)