Files
2025PY/day15/01-作业.py
2025-05-22 16:50:44 +08:00

92 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 1. 列表求和
lst = [1, 2, 3, 4, 5]
total = sum(lst)
print(f"列表的和是 {total}")
# 2. 找出列表中的最大值
lst = [1, 2, 3, 4, 5]
max_value = max(lst)
print(f"列表中的最大值是 {max_value}")
# 3. 找出列表中的最小值
lst = [3, 1, 4, 1, 5]
min_value = min(lst)
print(f"列表中的最小值是 {min_value}")
# 4. 列表元素去重
lst = [1, 2, 2, 3, 4, 4, 5]
unique_lst = list(set(lst))
print(f"去重后的列表是 {unique_lst}")
# 5. 计算列表中的元素个数
lst = [1, 2, 3, 4, 5]
count = len(lst)
print(f"列表中元素的个数是 {count}")
# 6. 反转列表
lst = [1, 2, 3, 4, 5]
reversed_lst = lst[::-1]
print(f"反转后的列表是 {reversed_lst}")
# 7. 合并两个列表
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
merged_lst = lst1 + lst2
print(f"合并后的列表是 {merged_lst}")
# 8. 计算列表中元素的平均值
lst = [1, 2, 3, 4, 5]
average = sum(lst) / len(lst)
print(f"列表的平均值是 {average}")
# 9. 查找列表中指定元素的索引
lst = [10, 20, 30, 40, 50]
element = 30
if element in lst:
index = lst.index(element)
print(f"元素 {element} 的索引是 {index}")
else:
print(f"元素 {element} 不在列表中")
# 10. 列表中的元素求积
lst = [1, 2, 3, 4]
product = 1
for num in lst:
product *= num
print(f"列表元素的积是 {product}")
# 11.判断列表是否为空
lst = []
if not lst:
print("列表为空True")
else:
print("列表为空False")
# 12. 删除列表中的指定元素
lst = [1, 2, 3, 2, 4]
element_to_remove = 2
while element_to_remove in lst:
lst.remove(element_to_remove)
print(f"删除后的列表是 {lst}")