Skip to content

Python 基础

数据类型

基本类型

python
# 数字
a = 10          # int
b = 3.14        # float

# 字符串
s = "Hello Python"

# 布尔值
flag = True     # 或 False

# 空值
n = None

数据结构

python
# 列表(List)
my_list = [1, 2, 3, "a", "b"]

# 元组(Tuple)- 不可变
my_tuple = (1, 2, 3)

# 字典(Dict)
my_dict = {"name": "Alice", "age": 25}

# 集合(Set)
my_set = {1, 2, 3}

函数定义

python
def greet(name, greeting="Hello"):
    """函数文档字符串(docstring)"""
    return f"{greeting}, {name}!"

# 调用
greet("World")
greet("Python", "Hi")

基于 MIT 许可发布