I. The Background of Python's Birth
1. How Was Python Born?
Python was designed and developed by the Dutch computer scientist Guido van Rossum.
In the late 1980s, Guido was working at the Centrum Wiskunde & Informatica (CWI) in the Netherlands. At the time, he was involved in a programming language project called ABC.
ABC was a high-level language that emphasized ease of learning and readability, and it sought to make programming convenient even for non-professional programmers.
ABC had many design ideas worth learning from:
- Concise syntax with an emphasis on code readability.
- Support for high-level data structures.
- An interactive programming environment.
- A focus on expressive power.
- Minimizing unnecessary language complexity.
However, ABC also had some limitations:
- Limited extensibility.
- Insufficient ability to integrate with existing systems.
- Inadequate flexibility in supporting system interfaces and low-level operations.
- Difficulty meeting the more diverse practical needs of developers.
Guido wanted to design a language that retained ABC's ease of use while offering stronger extensibility and practicality.
During the Christmas of 1989, he began developing this new language in his spare time.
In 1991, the first public version of Python was officially released.
2. The Origin of the Name "Python"
The name Python does not come from the snake.
Guido was a fan of the British comedy troupe Monty Python, so he named the new language Python.
The name reflects the designer's hope that Python would not only be a serious engineering tool, but would also preserve a relaxed, friendly, and creative development experience.
3. The Python Timeline
Python's development has gone through several important stages.
| Time | Key Event | Significance |
|---|---|---|
| 1989 | Guido begins designing Python | The language project starts |
| 1991 | Python 0.9.0 is released | The first public version |
| 1994 | Python 1.0 is released | The language gradually matures |
| 2000 | Python 2.0 is released | Introduces improvements such as list comprehensions and garbage collection |
| 2001 | The Python Software Foundation is established | Creates a long-term governance and maintenance organization |
| 2008 | Python 3.0 is released | Drives the modernization of the language design |
| 2010 | Python 2.7 is released | An important later release in the Python 2 series |
| 2020 | Official support for Python 2 ends | The community fully shifts to Python 3 |
| 2021 | Python 3.10 is released | Introduces structural pattern matching |
| 2022 | Python 3.11 is released | Improvements to interpreter performance and error diagnostics |
| 2023 | Python 3.12 is released | Introduces improvements such as more flexible generic syntax |
| 2024 | Python 3.13 is released | Advances capabilities such as the experimental free-threaded build |
| 2025 | Python 3.14 is released | Continued improvements to the interpreter, type system, and concurrency capabilities |
It is worth noting that Python's evolution is not simply about adding syntax features; it is a continuous balancing act among readability, performance, security, compatibility, and developer experience.
4. The Evolution of Major Python Versions
The most important dividing line in Python's versions is between Python 2 and Python 3.
Python 2 was the mainstream version widely used in the early years, supporting a large number of web, automation, and scientific computing projects for a long time.
Its typical characteristics include:
- Using the
printstatement. - Legacy issues in how strings and Unicode are handled.
- Behaviors such as integer division that differ from Python 3.
- A large amount of compatibility code in the ecosystem.
Python 3 is the series of versions that systematically adjusted the language design.
The main changes include:
print()became a built-in function.- Text strings use Unicode by default.
- Integer division behavior is more consistent.
- The standard library structure and APIs were adjusted.
- Type annotations, asynchronous programming, and modern syntax continue to evolve.
Python 2 and Python 3 are not fully compatible. Official support for Python 2 ended on January 1, 2020, and new projects should use a supported version of Python 3.
II. Python's Design Philosophy
Python's design philosophy emphasizes readability, simplicity, and practicality.
These ideas are not merely promotional slogans; they are reflected in the language's syntax, standard library, data model, and engineering practice.
1. The Zen of Python
The Zen of Python is the most representative expression of Python's design philosophy, summarized by Tim Peters.
In the Python interactive environment, you can run:
import this
The output includes:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
These principles can be summarized into several core directions.
2. Readability First (Readability Counts)
Python treats code readability as an important design goal.
For example, in Python:
if user_age >= 18:
print("允许访问")
else:
print("拒绝访问")
The code structure has a fairly direct correspondence with the business logic.
Compare with C-style languages:
if (user_age >= 18) {
printf("允许访问\n");
} else {
printf("拒绝访问\n");
}
Both can express the same logic, but Python uses indentation to express code blocks and does not require extra braces.
Python's readability is also reflected in:
- Syntax keywords are relatively natural.
- Container operations are concise.
- Built-in functions are abundant.
- The standard library provides unified common interfaces.
- It encourages clear naming and code organization.
Readability not only affects individual development experience but also directly impacts team collaboration, code review, and long-term maintenance costs.
3. Explicit over Implicit (Explicit is Better than Implicit)
Python tends to make program behavior more explicit.
For example:
# Explicit conversion
number = int("123")
# Explicit check
if value is not None:
print(value)
This makes program behavior easier to understand than relying on many implicit conversions.
Python does not completely lack implicit behavior.
For example:
result = 10 + 2.5
print(result) # 12.5
When an integer and a floating-point number are added, Python performs conversion according to numeric type rules.
Therefore, what this principle really emphasizes is:
- Do not let important behavior depend on hard-to-notice implicit rules.
- Express intent as explicitly as possible for ambiguous operations.
- Enable readers of the code to understand what the program actually does.
4. Simple over Complex (Simple is Better than Complex)
Python tends to accomplish common tasks through simple language features.
For example, swapping two variables:
a = 10
b = 20
a, b = b, a
print(a, b)
No extra temporary variable needs to be defined.
Another example, list comprehensions:
numbers = [1, 2, 3, 4, 5]
squares = [x * x for x in numbers]
print(squares)
Compared with manually creating a list and appending elements one by one, the expression is more compact.
But simple does not mean the shorter the code, the better.
For example:
result = [f(x) for x in data if g(x) and h(x) and k(x)]
If an expression is too complex, splitting it into multiple steps is actually easier to maintain.
What Python pursues is reducing unnecessary complexity, not sacrificing readability for extreme brevity.
5. One Obvious Way (There Should Be One Obvious Way)
Python tends to provide clear, natural solutions for common problems.
For example, iterating over a list:
names = ["Alice", "Bob", "Charlie"]
for name in names:
print(name)
Iterating over a dictionary:
user = {
"name": "Alice",
"age": 25
}
for key, value in user.items():
print(key, value)
These operations share a consistent, intuitive form of expression.
However, this does not mean Python has only one way to implement things.
For example, map(), filter(), and list comprehensions from functional programming can all accomplish some similar tasks.
What Python emphasizes more is that, for common tasks, clear expressions that follow community conventions should be preferred.
6. Practicality Beats Purity
Python's design does not pursue absolute formal uniformity.
When strict adherence to a theoretical principle makes practical development difficult, Python tends to consider actual engineering needs instead.
For example:
- Python supports object-oriented, functional, and procedural programming at the same time.
- It allows object attributes to be added dynamically.
- It supports runtime reflection and dynamic imports.
- It provides flexible metaprogramming mechanisms.
- It allows the use of native extensions to improve performance.
This flexibility enables Python to adapt to a variety of application scenarios.
But flexibility also means developers need to establish reasonable engineering standards to avoid excessive dynamism making code difficult to maintain.
7. Errors Should Not Be Silently Ignored
Python encourages explicit exception handling.
For example:
try:
result = 10 / 0
except ZeroDivisionError as error:
print(f"计算失败:{error}")
For errors that cannot be anticipated, they should not be ignored casually:
try:
process_data()
except Exception:
pass
This style of writing may hide real problems, making the program appear to continue running while the data is already in an incorrect state.
A more reasonable approach is:
- Catch specific exceptions that you can handle.
- Record necessary error information.
- Recover from recoverable errors.
- For unrecoverable errors, terminate promptly or raise them upward.
8. Namespaces Are an Excellent Design
Python places great importance on namespaces.
Namespaces are used to organize the mapping relationship between names and objects.
For example:
import math
import random
print(math.sqrt(16))
print(random.randint(1, 10))
math and random provide different namespaces, preventing a large number of function names from being mixed together directly.
Namespaces help to:
- Reduce naming conflicts.
- Organize modules and packages.
- Improve code readability.
- Support modular development of large projects.
III. Core Language Design Features of Python
Design philosophy is ultimately reflected in language mechanisms.
Understanding these mechanisms helps explain why Python uses its current syntax and execution model.
1. Indentation Defines Code Blocks
Python uses indentation to indicate code blocks rather than relying on braces.
score = 85
if score >= 60:
print("考试通过")
if score >= 80:
print("成绩优秀")
print("程序结束")
Indentation is not only code formatting but also part of Python syntax.
Incorrect indentation can directly cause an IndentationError.
A common convention is to use four spaces for one indentation level and avoid mixing tabs and spaces.
2. Dynamic Type System
Python uses a dynamic typing mechanism.
Variable names do not need to be declared with a fixed type in advance; a name can be bound to objects of different types during runtime.
value = 100
print(type(value))
value = "Python"
print(type(value))
Output:
<class 'int'>
<class 'str'>
It is important to understand accurately:
- Python objects have types.
- Variable names themselves do not have fixed type declarations.
- A name can be bound to objects of different types at different times.
- Type-related operations are usually checked at runtime.
Dynamic typing improves expressive flexibility, but it also requires developers to value testing, type annotations, and runtime data validation.
3. Everything Is an Object
Python's core data model emphasizes objects.
Integers, strings, functions, classes, and modules can all participate in program execution as objects.
For example:
number = 100
name = "Python"
def greet():
return "Hello"
print(type(number))
print(type(name))
print(type(greet))
Functions can also be assigned to variables:
def say_hello():
print("Hello Python")
action = say_hello
action()
This design enables Python to conveniently support:
- Higher-order functions.
- Closures.
- Decorators.
- Dynamic attributes.
- Reflection.
- Metaprogramming.
4. Variable Names Are Separate from Objects
In Python, variables are more accurately name bindings rather than values in fixed memory locations.
a = [1, 2, 3]
b = a
b.append(4)
print(a)
print(b)
Output:
[1, 2, 3, 4]
[1, 2, 3, 4]
The reason is that a and b reference the same list object.
You can use id() to observe object identity:
print(id(a))
print(id(b))
The two results are the same, indicating that they point to the same object.
This mechanism is foundational to understanding mutable and immutable objects, argument passing, and shallow copying in Python.
5. Automatic Memory Management
Python provides automatic memory management.
Developers usually do not need to manually call malloc() and free() to manage object memory as they would in C.
Taking CPython as an example, its memory management mainly involves:
- Reference counting.
- Cyclic garbage collection.
- Memory allocators.
- Object deallocation and memory reuse.
For example:
data = [1, 2, 3]
data = None
When the original list is no longer referenced, it may become unreachable and be reclaimed at an appropriate time.
Note that the Python language specification does not require all implementations to use CPython's reference counting mechanism. Different Python implementations may adopt different memory management strategies.
6. Multi-Paradigm Programming
Python is not limited to a single programming paradigm.
Procedural programming:
def calculate_total(prices):
total = 0
for price in prices:
total += price
return total
Object-oriented programming:
class ShoppingCart:
def __init__(self):
self.items = []
def add_item(self, price):
self.items.append(price)
def total(self):
return sum(self.items)
Functional programming:
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x * x, numbers))
All three approaches can be used in Python.
Developers can choose an appropriate programming paradigm based on the business structure, without being forced to use a single style throughout the project.
IV. Design Differences Between Python and Other Programming Languages
Python's design characteristics become more apparent when compared with other languages.