惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Recent Announcements
Recent Announcements
雷峰网
雷峰网
The GitHub Blog
The GitHub Blog
罗磊的独立博客
月光博客
月光博客
J
Java Code Geeks
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
U
Unit 42
C
Check Point Blog
Martin Fowler
Martin Fowler
有赞技术团队
有赞技术团队
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog

博客园_首页

Plist 二进制格式 Milvus 和 PGVector,哪个更好? OpenClaw 已过时?在 VS Code 中运行 Hermes Agent! 第30篇文章:一个大三计科生的自白 Manim如何在数学公式中完美显示中文? Docker 部署 RocketMQ 5 并发编程核心概念辨析 C#事务处理最佳实践:别再让“主表存了、明细丢了”的破事发生 CLI 是什么?为什么大厂突然集体卷命令行? 【从0到1构建一个ClaudeAgent】协作-自主Agent UIImageView 设置图片不生效的原因排查 最小二乘问题详解20:无先验约束下的增量式SFM自由网平差 痞子衡嵌入式:大话双核i.MXRT1180之XIP应用里借助MU实现可靠Flash IAP的方法 AI Chat 封装, SemanticKerne.AiProvider.Unified 已发布 Windows下右键编辑js文件无法打开记事本——在注册表中使用环境变量 在后台服务中使用 Scoped 服务,为什么总是报错? H200 安装驱动并使用sglang启动模型 wireshark 抓包Trap上报告警内容 我用 AI 辅助开发了一系列小工具(2):图片压缩工具 [A Primer On MC and CC] 2.1 Memory Consistency 1 - 指令重排序和 SC 模型 Oracle数据库SCN推进技术详解与实践指南 玩转控件:封装个带图片的Label控件 Claude Code 4.7 真正该升级的不是模型,而是你的工作流 前端小白一句话,AI 帮我做了个颜值拉满的桌面媒体播放器。当代码不再是门槛,一句话编程就是现实。 5. WorkBuddy: 小龙虾的灵魂三件套,让你的小龙虾不只是工具 SQLite 分片方案实战:三种分片策略的深度对比 告别简陋 UI!一款基于 Fluent Design 和基于 WinUI 的开源免费、现代化的 Avalonia UI 控件库 关于二进制排列组合枚举的总结 AI开发-python-LangGraph框架(3-27-LangGraph从零实现大模型智能决策工作流) ElasticSearch主分片和副本分片概念详解
【学习笔记】《Python编程 从入门到实践》第1章学习笔记:Pyt...
lunzi_fly · 2026-05-21 · via 博客园_首页

第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

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

复习要点