第1章 起步

本章目标:安装 Python 环境、安装文本编辑器、运行第一个 Python 程序。


1.1 搭建编程环境

1.1.1 Python 2 和 Python 3

  • Python 有两个主要版本:Python 2(旧版)和 Python 3(新版)
  • 本书以 Python 3 为主,同时指出 Python 2 的重大差别
  • 建议:优先安装并使用 Python 3

1.1.2 运行 Python 代码片段

Python 自带的终端解释器,可以不用保存完整程序,直接试运行代码片段。

>>> print("Hello Python interpreter!")
Hello Python interpreter!
  • >>> 是 Python 提示符,表示可以在后面输入 Python 命令
  • 代码清单中带 >>> 的表示来自终端会话

1.1.3 Hello World 程序

只需一行代码:

print("Hello world!")

如果这行代码能正确运行,你编写的任何 Python 程序都将如此。


1.2 在不同操作系统中搭建 Python 编程环境

Python 是跨平台语言,所有主流操作系统都能运行。

1.2.1 在 Linux 系统中搭建

1. 检查 Python 版本

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

退出 Python:Ctrl + Dexit()

检查 Python 3:

$ python3
Python 3.5.0 (default, Sep 17 2015, 13:05:18)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

2. 安装文本编辑器(Geany)

$ sudo apt-get install geany

3. 配置 Geany 使用 Python 3

菜单 Build → Set Build Commands,修改:

  • 编译命令:python3 -m py_compile "%f"
  • 执行命令:python3 "%f"

4. 编写并运行 Hello World

创建文件夹 python_work,文件 hello_world.py

print("Hello Python world!")

运行:菜单 Build → Execute、单击 Execute 图标或按 F5

输出:

Hello Python world!
------------------
(program exited with code: 0)
Press return to continue

5. 在终端会话中运行 Python 代码

>>> print("Hello Python interpreter!")
Hello Python interpreter!
>>>

1.2.2 在 OS X 系统中搭建

1. 检查 Python 版本

$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>>

2. 安装文本编辑器(Sublime Text)

下载地址:http://sublimetext.com/3

3. 配置 Sublime Text 使用 Python 3

查看 Python 3 路径:

$ type -a python3
python3 is /usr/local/bin/python3

菜单 Tools → Build System → New Build System,输入:

{
    "cmd": ["/usr/local/bin/python3", "-u", "$file"],
}

保存为 Python3.sublime-build

4. 运行 Hello World

print("Hello Python world!")

运行:Tools → BuildCommand + B

输出:

Hello Python world!
[Finished in 0.1s]

1.2.3 在 Windows 系统中搭建

1. 安装 Python

  • 访问 http://python.org/downloads/
  • 下载 Python 3 安装程序
  • 务必勾选 “Add Python to PATH”

2. 启动 Python 终端会话

C:\> python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 22:15:05) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

如果提示 'python' is not recognized,需手动指定路径:

C:\> C:\Python35\python

3. 安装文本编辑器(Geany)

下载地址:http://geany.org/

4. 配置 Geany

菜单 Build → Set Build Commands

  • 编译命令:C:\Python35\python -m py_compile "%f"
  • 执行命令:C:\Python35\python "%f"

5. 运行 Hello World

print("Hello Python world!")

运行:菜单 Build → Execute、单击 Execute 图标或按 F5


1.3 解决安装问题

如果程序无法运行,尝试以下方案:

  1. 查看 traceback — Python 会显示错误追踪信息,提供线索
  2. 休息一下再试 — 少一个冒号、引号不匹配都可能导致失败
  3. 推倒重来 — 删除 hello_world.py 重新创建
  4. 请人帮忙 — 让别人按步骤重做一遍,你可能遗漏了一小步
  5. 查阅在线资源 — https://www.nostarch.com/pythoncrashcourse/
  6. 网上求助 — 见附录 C
  7. Python 社区对初学者非常友好,不要怕提问

1.4 从终端运行 Python 程序

1.4.1 在 Linux 和 OS X 系统中

~$ cd Desktop/python_work/
~/Desktop/python_work$ ls
hello_world.py
~/Desktop/python_work$ python hello_world.py
Hello Python world!

关键命令:

  • cd — 切换目录(change directory)
  • ls — 列出文件(list)
  • python hello_world.py — 运行 Python 程序

1.4.2 在 Windows 系统中

C:\> cd Desktop\python_work
C:\Desktop\python_work> dir
hello_world.py
C:\Desktop\python_work> python hello_world.py
Hello Python world!

关键命令:

  • cd — 切换目录
  • dir — 列出文件(directory)
  • python hello_world.py — 运行 Python 程序

如果未配置 PATH,需指定完整路径:

C:\Desktop\python_work> C:\Python35\python hello_world.py
Hello Python world!

动手试一试

1-1 python.org

浏览 Python 主页(http://python.org/),寻找感兴趣的主题。

1-2 输入错误

hello_world.py 中故意添加一个输入错误(如将 print 拼写为 PRINT),运行观察错误信息。试找一个不会导致错误的输入错误。

1-3 无穷的技艺

如果你编程技艺无穷,你打算开发什么样的程序?花点时间描绘三个你想创建的程序。


代码块汇总

# Hello World — 第一个 Python 程序
print("Hello world!")
# 终端会话中运行 Python 代码片段
>>> print("Hello Python interpreter!")
Hello Python interpreter!
# hello_world.py — 教材实际使用的程序
print("Hello Python world!")

常见错误 / 陷阱

错误类型 说明 解决方法
NameError 变量名拼写错误或未定义 检查变量名拼写
SyntaxError 语法错误,如 print 首字母大写 Python 区分大小写,print 必须全小写
命令找不到 PATH 未配置(Windows) 手动指定 Python 完整路径
编辑器无法运行 未配置正确的 Python 版本 设置编译/执行命令为 python3

复习要点

  • Python 有两个主要版本:2 和 3,本书使用 Python 3
  • .py 扩展名标识 Python 程序文件
  • print() 函数将内容打印到屏幕
  • >>> 是 Python 终端提示符
  • 文件夹命名建议:小写 + 下划线(如 python_work
  • Linux/OS X 终端用 cd + ls,Windows 用 cd + dir
  • traceback 是 Python 提供的错误追踪信息
  • 退出 Python 解释器:Ctrl + D(Linux/macOS)或 Ctrl + Z 再回车(Windows),或 exit()
Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐