Python 数据类型
列表(List)
列表是Python中最常用的数据类型,用于存储多个有序的元素。
创建列表
# 空列表
empty_list = []
empty_list2 = list()
# 包含元素的列表
numbers = [1, 2, 3, 4, 5]
fruits = ["苹果", "香蕉", "橙子"]
mixed = [1, "hello", 3.14, True]
print(numbers) # [1, 2, 3, 4, 5]
print(fruits) # ['苹果', '香蕉', '橙子']
列表操作
# 访问元素
fruits = ["苹果", "香蕉", "橙子", "葡萄"]
print(fruits[0]) # 苹果(第一个元素)
print(fruits[-1]) # 葡萄(最后一个元素)
# 切片操作
print(fruits[1:3]) # ['香蕉', '橙子']
print(fruits[:2]) # ['苹果', '香蕉']
print(fruits[2:]) # ['橙子', '葡萄']
# 修改元素
fruits[1] = "草莓"
print(fruits) # ['苹果', '草莓', '橙子', '葡萄']
列表方法
numbers = [1, 2, 3]
# 添加元素
numbers.append(4) # 末尾添加
numbers.insert(0, 0) # 指定位置插入
numbers.extend([5, 6]) # 添加多个元素
print(numbers) # [0, 1, 2, 3, 4, 5, 6]
# 删除元素
numbers.remove(0) # 删除指定值
popped = numbers.pop() # 删除并返回最后一个元素
del numbers[0] # 删除指定索引的元素
print(numbers) # [2, 3, 4, 5]
print(popped) # 6
# 其他方法
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print(numbers.count(1)) # 2(统计1的个数)
print(numbers.index(4)) # 2(查找4的索引)
numbers.sort() # 排序
print(numbers) # [1, 1, 2, 3, 4, 5, 6, 9]
numbers.reverse() # 反转
print(numbers) # [9, 6, 5, 4, 3, 2, 1, 1]
元组(Tuple)
元组类似于列表,但是不可变(immutable)。
创建元组
# 空元组
empty_tuple = ()
empty_tuple2 = tuple()
# 包含元素的元组
coordinates = (3, 4)
colors = ("红", "绿", "蓝")
single_item = (42,) # 单元素元组需要逗号
print(coordinates) # (3, 4)
print(colors) # ('红', '绿', '蓝')
元组操作
point = (10, 20, 30)
# 访问元素
print(point[0]) # 10
print(point[-1]) # 30
# 切片
print(point[1:]) # (20, 30)
# 元组解包
x, y, z = point
print(f"x={x}, y={y}, z={z}") # x=10, y=20, z=30
# 元组不可修改
# point[0] = 5 # 这会报错:TypeError
元组的用途
# 函数返回多个值
def get_name_age():
return "张三", 25
name, age = get_name_age()
print(f"姓名:{name},年龄:{age}")
# 作为字典的键
locations = {
(0, 0): "原点",
(1, 1): "右上",
(-1, -1): "左下"
}
print(locations[(0, 0)]) # 原点
字典(Dictionary)
字典是键值对的集合,类似于其他语言中的哈希表或映射。
创建字典
# 空字典
empty_dict = {}
empty_dict2 = dict()
# 包含数据的字典
person = {
"name": "张三",
"age": 30,
"city": "北京"
}
# 使用dict()函数
person2 = dict(name="李四", age=25, city="上海")
print(person) # {'name': '张三', 'age': 30, 'city': '北京'}
print(person2) # {'name': '李四', 'age': 25, 'city': '上海'}
字典操作
student = {"name": "小明", "age": 18, "grade": "高三"}
# 访问值
print(student["name"]) # 小明
print(student.get("age")) # 18
print(student.get("phone", "未知")) # 未知(默认值)
# 修改和添加
student["age"] = 19 # 修改
student["phone"] = "1234567" # 添加
print(student)
# {'name': '小明', 'age': 19, 'grade': '高三', 'phone': '1234567'}
# 删除
del student["grade"] # 删除键值对
phone = student.pop("phone") # 删除并返回值
print(student) # {'name': '小明', 'age': 19}
print(phone) # 1234567
字典方法
scores = {"数学": 95, "英语": 88, "语文": 92}
# 获取所有键、值、键值对
print(scores.keys()) # dict_keys(['数学', '英语', '语文'])
print(scores.values()) # dict_values([95, 88, 92])
print(scores.items()) # dict_items([('数学', 95), ('英语', 88), ('语文', 92)])
# 遍历字典
for subject in scores:
print(f"{subject}: {scores[subject]}")
for subject, score in scores.items():
print(f"{subject}: {score}")
# 字典合并
new_scores = {"物理": 90, "化学": 87}
scores.update(new_scores)
print(scores)
# {'数学': 95, '英语': 88, '语文': 92, '物理': 90, '化学': 87}
集合(Set)
集合是无序的、不重复的元素集合。
创建集合
# 空集合
empty_set = set() # 注意:{}创建的是空字典
# 包含元素的集合
numbers = {1, 2, 3, 4, 5}
fruits = {"苹果", "香蕉", "橙子"}
# 从列表创建集合(自动去重)
list_with_duplicates = [1, 2, 2, 3, 3, 3, 4]
unique_numbers = set(list_with_duplicates)
print(unique_numbers) # {1, 2, 3, 4}
集合操作
colors = {"红", "绿", "蓝"}
# 添加元素
colors.add("黄")
colors.update(["紫", "橙"]) # 添加多个元素
print(colors) # {'红', '绿', '蓝', '黄', '紫', '橙'}
# 删除元素
colors.remove("橙") # 删除指定元素(不存在会报错)
colors.discard("白") # 删除指定元素(不存在不报错)
popped = colors.pop() # 随机删除一个元素
print(colors)
# 集合运算
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(set1 | set2) # {1, 2, 3, 4, 5, 6} 并集
print(set1 & set2) # {3, 4} 交集
print(set1 - set2) # {1, 2} 差集
print(set1 ^ set2) # {1, 2, 5, 6} 对称差集
数据类型转换
类型转换函数
# 转换为列表
tuple_data = (1, 2, 3)
list_data = list(tuple_data)
print(list_data) # [1, 2, 3]
# 转换为元组
list_data = [4, 5, 6]
tuple_data = tuple(list_data)
print(tuple_data) # (4, 5, 6)
# 转换为集合
list_with_duplicates = [1, 1, 2, 2, 3]
set_data = set(list_with_duplicates)
print(set_data) # {1, 2, 3}
# 字符串转列表
text = "hello"
char_list = list(text)
print(char_list) # ['h', 'e', 'l', 'l', 'o']
实用转换示例
# 统计字符出现次数
text = "hello world"
char_count = {}
for char in text:
if char != ' ': # 忽略空格
char_count[char] = char_count.get(char, 0) + 1
print(char_count) # {'h': 1, 'e': 1, 'l': 3, 'o': 2, 'w': 1, 'r': 1, 'd': 1}
# 去除列表中的重复元素并保持顺序
def remove_duplicates(lst):
seen = set()
result = []
for item in lst:
if item not in seen:
seen.add(item)
result.append(item)
return result
numbers = [1, 2, 2, 3, 1, 4, 3, 5]
unique_numbers = remove_duplicates(numbers)
print(unique_numbers) # [1, 2, 3, 4, 5]
嵌套数据结构
列表嵌套
# 二维列表(矩阵)
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[1][2]) # 6(第2行第3列)
# 学生信息列表
students = [
{"name": "张三", "age": 18, "scores": [85, 90, 78]},
{"name": "李四", "age": 19, "scores": [92, 88, 95]},
{"name": "王五", "age": 18, "scores": [78, 85, 88]}
]
# 计算每个学生的平均分
for student in students:
avg_score = sum(student["scores"]) / len(student["scores"])
print(f"{student['name']}的平均分:{avg_score:.1f}")
字典嵌套
# 公司组织结构
company = {
"name": "科技公司",
"departments": {
"技术部": {
"manager": "张经理",
"employees": ["程序员A", "程序员B", "测试员C"]
},
"销售部": {
"manager": "李经理",
"employees": ["销售员D", "销售员E"]
}
}
}
# 访问嵌套数据
tech_manager = company["departments"]["技术部"]["manager"]
print(f"技术部经理:{tech_manager}")
# 遍历所有部门
for dept_name, dept_info in company["departments"].items():
print(f"{dept_name}经理:{dept_info['manager']}")
print(f"员工:{', '.join(dept_info['employees'])}")
实践练习
练习1:学生成绩管理
# 创建学生成绩管理系统
students_scores = {}
def add_student(name, scores):
students_scores[name] = scores
def get_average(name):
if name in students_scores:
scores = students_scores[name]
return sum(scores) / len(scores)
return None
def get_top_student():
if not students_scores:
return None
top_student = None
highest_avg = 0
for name, scores in students_scores.items():
avg = sum(scores) / len(scores)
if avg > highest_avg:
highest_avg = avg
top_student = name
return top_student, highest_avg
# 测试
add_student("张三", [85, 90, 78, 92])
add_student("李四", [92, 88, 95, 89])
add_student("王五", [78, 85, 88, 90])
print(f"张三的平均分:{get_average('张三'):.1f}")
top_name, top_score = get_top_student()
print(f"最高分学生:{top_name},平均分:{top_score:.1f}")
练习2:文本分析
def analyze_text(text):
# 转换为小写并分割单词
words = text.lower().split()
# 统计词频
word_count = {}
for word in words:
# 移除标点符号
clean_word = word.strip('.,!?;:"')
if clean_word:
word_count[clean_word] = word_count.get(clean_word, 0) + 1
# 找出最常见的词
most_common = max(word_count.items(), key=lambda x: x[1])
return {
"total_words": len(words),
"unique_words": len(word_count),
"word_frequency": word_count,
"most_common": most_common
}
# 测试
text = "Python is great. Python is easy to learn. Learning Python is fun!"
result = analyze_text(text)
print(f"总词数:{result['total_words']}")
print(f"唯一词数:{result['unique_words']}")
print(f"最常见的词:{result['most_common'][0]}(出现{result['most_common'][1]}次)")
总结
本课我们学习了Python的复合数据类型:
- 列表(List):有序、可变的元素集合
- 元组(Tuple):有序、不可变的元素集合
- 字典(Dictionary):键值对的映射
- 集合(Set):无序、不重复的元素集合
- 类型转换:不同数据类型之间的转换
- 嵌套结构:复杂数据的组织方式
下一课预告
在下一课中,我们将学习Python的控制结构,包括:
- 条件语句(if/elif/else)
- 循环语句(for/while)
- 循环控制(break/continue)
- 异常处理基础
💡 小贴士:选择合适的数据类型是编程的重要技能。列表适合需要修改的有序数据,元组适合不变的数据,字典适合键值映射,集合适合去重和集合运算。
📚 文章对你有帮助?请关注我的公众号,万分感谢!
获取更多优质技术文章,第一时间掌握最新技术动态

关注公众号
第一时间获取最新技术文章

添加微信
技术交流 · 问题答疑 · 学习指导
评论讨论
欢迎留下你的想法和建议