Python3 教程

如何查看 Python 版本、写出并运行第一个 Python3 程序,以及后续学习路径。

Beginner
Key Points先用 `python --version` 确认版本,再写 hello.py 用 `python hello.py` 运行;Python 3 与 2 不兼容,本教程全部基于 Python 3。

查看 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.

Python
import sys

print(sys.version_info.major)
print(sys.version_info >= (3, 8))
print(isinstance(sys.version_info, tuple))
Output
3
True
True
On Windows python 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.

Python
# hello.py
print("Hello, World!")
print('Hello, Python3!')
print("Hello", "World", sep=", ")
Output
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.

Python
print(1 + 1)
print(len("hello"))
print(type(3.14).__name__)
print("a" * 3)
Output
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.

  1. Basic syntax and data types — the grammar and the built-in types
  2. Control flow — if / while / for
  3. Functions and modules — organise code into reusable units
  4. Objects and errors — model data, handle failure

Friends & Partners