Skip to content

Python 基础

数据类型

python
# 字符串
name = "Python"

# 列表
numbers = [1, 2, 3, 4, 5]

# 字典
person = {
    "name": "张三",
    "age": 25
}

# 集合
unique = {1, 2, 3, 4, 5}

函数定义

python
def greet(name, greeting="你好"):
    """问候函数"""
    return f"{greeting}, {name}!"

# 调用
message = greet("小明")
print(message)  # 你好, 小明!

异步编程

python
import asyncio

async def fetch_data():
    await asyncio.sleep(1)
    return "数据"

async def main():
    result = await fetch_data()
    print(result)

asyncio.run(main())

文件操作

python
# 读取文件
with open("file.txt", "r", encoding="utf-8") as f:
    content = f.read()

# 写入文件
with open("output.txt", "w", encoding="utf-8") as f:
    f.write("Hello, World!")

基于 VitePress 构建