Python3 教程
如何查看 Python 版本、写出并运行第一个 Python3 程序,以及后续学习路径。
查看 Python 版本
Before writing code, confirm the interpreter exists and is recent enough. Run python --version in a shell, or read sys.version_info inside the interpreter. This tutorial assumes Python 3.8 or newer.
import sys
print(sys.version_info.major)
print(sys.version_info >= (3, 8))
print(isinstance(sys.version_info, tuple))3
True
Truepython may be a Microsoft Store stub — try py --version instead. On macOS / Linux the command is usually python3.第一个Python3.x程序
Create hello.py, put one print in it, then run python hello.py. print is a built-in that appends a newline; strings accept single or double quotes.
# hello.py
print("Hello, World!")
print('Hello, Python3!')
print("Hello", "World", sep=", ")Hello, World!
Hello, Python3!
Hello, World交互式与脚本式
Besides files you can use the interactive interpreter to try one line at a time — the fastest way to check syntax or poke at a library. Both modes use the same interpreter.
print(1 + 1)
print(len("hello"))
print(type(3.14).__name__)
print("a" * 3)2
5
float
aaa下一步学什么
Once the environment works, follow the order: basic syntax → data types → type conversion → interpreter and comments → operators, then control flow and functions. Every chapter ships runnable examples — type them out as you read.
- Basic syntax and data types — the grammar and the built-in types
- Control flow — if / while / for
- Functions and modules — organise code into reusable units
- Objects and errors — model data, handle failure
查看 Python 版本
动手之前先确认解释器装好了、版本够新。命令行里执行 python --version,或在解释器内读 sys.version_info。本教程以 Python 3.8 及以上为前提。
import sys
print(sys.version_info.major)
print(sys.version_info >= (3, 8))
print(isinstance(sys.version_info, tuple))3
True
Truepython 可能指向 Microsoft Store 的占位程序,这时改用 py --version;macOS / Linux 上通常是 python3。第一个Python3.x程序
新建 hello.py,写入一行 print,然后在命令行执行 python hello.py。print 是内置函数,输出后自动换行;字符串可以用单引号或双引号。
# hello.py
print("Hello, World!")
print('Hello, Python3!')
print("Hello", "World", sep=", ")Hello, World!
Hello, Python3!
Hello, World交互式与脚本式
除了写文件,也可以直接进交互式解释器敲一行看一行结果 —— 验证语法和试库函数时最快。两种方式跑的是同一个解释器,行为一致。
print(1 + 1)
print(len("hello"))
print(type(3.14).__name__)
print("a" * 3)2
5
float
aaa下一步学什么
环境跑通后按顺序往下走:基础语法 → 基本数据类型 → 数据类型转换 → 解释器与注释 → 运算符,然后进入流程控制与函数。每一章都配可运行示例,建议边看边敲。
- 基础语法与基本数据类型 —— 语法规则与内置类型
- 流程控制 —— if / while / for
- 函数与模块 —— 把代码组织成可复用单元
- 面向对象与异常 —— 建模与容错