【学习笔记】《Python编程 从入门到实践》第1章:Python环境搭建与Hello World(完整版)

【学习笔记】《Python编程 从入门到实践》第1章:Python环境搭建与Hello World(完整版)
搭建编程环境1.1.1 Python 2 和 Python 3Python 有两个主要版本Python 2旧版和 Python 3新版本书以 Python 3 为主同时指出 Python 2 的重大差别建议优先安装并使用 Python 31.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. 退出 PythonCtrl D或exit()检查 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 geany3. 配置 Geany 使用 Python 3菜单Build → Set Build Commands修改编译命令python3 -m py_compile %f执行命令python3 %f4. 编写并运行 Hello World创建文件夹python_work文件hello_world.pyprint(Hello Python world!)运行菜单Build → Execute、单击 Execute 图标或按F5输出Hello Python world! ------------------ (program exited with code: 0) Press return to continue5. 在终端会话中运行 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/33. 配置 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-build4. 运行 Hello Worldprint(Hello Python world!)运行Tools → Build或Command B输出Hello Python world! [Finished in 0.1s]1.2.3 在 Windows 系统中搭建1. 安装 Python访问 http://python.org/downloads/下载 Python 3 安装程序务必勾选 Add Python to PATH2. 启动 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\python3. 安装文本编辑器Geany下载地址http://geany.org/4. 配置 Geany菜单Build → Set Build Commands编译命令C:\Python35\python -m py_compile %f执行命令C:\Python35\python %f5. 运行 Hello Worldprint(Hello Python world!)运行菜单Build → Execute、单击 Execute 图标或按F51.3 解决安装问题如果程序无法运行尝试以下方案查看 traceback— Python 会显示错误追踪信息提供线索休息一下再试— 少一个冒号、引号不匹配都可能导致失败推倒重来— 删除 hello_world.py 重新创建请人帮忙— 让别人按步骤重做一遍你可能遗漏了一小步查阅在线资源— https://www.nostarch.com/pythoncrashcourse/网上求助— 见附录 CPython 社区对初学者非常友好不要怕提问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 directoryls— 列出文件listpython 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— 列出文件directorypython 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!)