菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
0
0

Python 教程-Python 安装

原创
05/13 14:22
阅读数 822

在 Windows 上安装

访问 https://www.python.org/downloads/ 并下载最新版本。在撰写时当前最新是 3.8 。在安装的时候和其他软件一样,无脑式下一步。需要注意的是如果在Windows 7环境下安装Python 3.x,需要先安装Service Pack 1补丁包(可以通过一些工具软件自动安装系统补丁的功能来安装),安装过程建议勾选“Add Python 3.x to PATH”(将Python 3.x添加到PATH环境变量)并选择自定义安装,在设置“Optional Features”界面最好将“pip”、“tcl/tk”、“Python test suite”等项全部勾选上。强烈建议选择自定义的安装路径并保证路径中没有中文。安装完成会看到“Setup was successful”的提示。

如果想要更改安装路径,单击 Customize installation(自定义安装),然后单击 Next(下一步)并输入 C:\python35(或者其它合适的位置) 作为安装位置。

安装完成以后你还需检查是否有 Python 的环境变量

在 Mac OS X 上安装

macOS也自带了Python 2.x版本,可以通过Python的官方网站提供的安装文件(pkg文件)安装Python 3.x的版本。默认安装完成后,可以通过在终端执行python命令来启动2.x版本的Python解释器,启动3.x版本的Python解释器需要执行python3命令。

对于 Mac OS X 用户,使用 Homebrew:自行安装 python3.

brew install  python

运行Python程序

确认Python的版本

可以Windows的命令行提示符中键入下面的命令。

python --version

或者是在Linux或macOS系统的终端中键入下面的命令。

python3 --version

编写Python源代码

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

    print('hello, world!')

运行 Python 程序

切换到源代码所在的目录并执行下面的命令,看看屏幕上是否输出了"hello, world!"。

python hello.py

python3 hello.py

注释

又称 注解,编程语言的一个重要的组成部分,是代码的解释或者说是代码的说明,用于解释代码的作用,从而增强代码的可读性,特别是在项目中,良好的注释习惯极为重要,你想想你接手别人的代码没有注释是非常痛苦的,当然也可以将代码中不需要运行的代码注释掉,方便调试。注释在随猿代码进去解析器的时候会被移除掉,不会在目标代码中保留,也不会影响性能

  • 用于添加说明文字,增强代码可读性
  • 用于调试程序,排查代码错误

代码告诉你方式,注释告诉你原因.
这对程序的读者非常有用,这样他们就可以很容易地理解程序正在做什么。请记住,在六个月之后,读者可能就是你自己!

程序员最讨厌的两件事情,

  1. 别人代码不写注释
  2. 自己写代码要写注释

分类

  1. 单行注释 - 以#和空格开头的部分
  2. 多行注释 - 三个引号开头,三个引号结尾
print('hello, world!')
# print("你好,世界!")
‘’‘
print('hello, world!')
print('hello, world!')
print('hello, world!')
''' 
print('hello, world!')

高能时刻

"""
绘制小猪佩奇
"""
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()

发表评论

0/200
0 点赞
0 评论
收藏
为你推荐 换一批