常用示例

字符串操作

# 字符串拼接
greeting = "Hello" + " " + "World"  # 输出: Hello World
Python_Logo

列表操作

# 列表推导式
squares = [x**2 for x in range(10)]  # 输出: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
List_Manipulation

字典操作

# 字典遍历
person = {"name": "Alice", "age": 30}
for key, value in person.items():
    print(f"{key}: {value}")  # 输出: name: Alice / age: 30
Dictionary_Example

进阶主题

装饰器

# 简单装饰器
def my_decorator(func):
    def wrapper():
        print("装饰器执行前")
        func()
        print("装饰器执行后")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")
Decorator_Illustration

上下文管理器

# 文件操作上下文管理器
with open("example.txt", "r") as file:
    content = file.read()
Context_Manager

了解更多Python基础语法 👉