## 列表的基础操作
# 1. 创建包含三个字符串的列表
tech_list = ["Python", "Java", "Go"]
# 2. 获取第一个元素
first_tech = tech_list[0]
# 3. 向列表末尾添加新元素
tech_list.append("JavaScript")
# 4. 修改第二个元素
tech_list[1] = "Ruby"
# 5. 移除元素"Go"
tech_list.remove("Go")
# 6. 计算列表当前长度
current_length = len(tech_list)
# 7. 使用f-string打印结果
print(f"第一个技术是: {first_tech}")
print(f"当前列表长度: {current_length}")
print(f"最终列表内容: {tech_list}")
第一个技术是: Python 当前列表长度: 3 最终列表内容: ['Python', 'Ruby', 'JavaScript']
tech_list = ["Python","Java","Go"]
first_tech = tech_list[0]
tech_list.append("JavaScript")
tech_list[1]="Ruby"
tech_list.remove("Go")
current_length = len(tech_list)
print(f"第一个技术是:{first_tech}")
print(f"当前列表长度:{current_length}")
print(f"最终列表内容:{tech_list}")
第一个技术是:Python 当前列表长度:3 最终列表内容:['Python', 'Ruby', 'JavaScript']
循环for语句
计算1+100的和 用for循环来写
total = 0
for num in range(1,101):# range(1,101)表示从1到100(包含100)
total += num
print(f"1到1oo的和是:{total}")
1到1oo的和是:5050
判断语句
温度预警系统
1. 定义一个变量temperature存储当前温度(整数)
2. 根据以下条件判断并打印预警信息:
- 高于35度:打印"红色预警:高温天气!"
- 28-35度:打印"黄色预警:天气炎热"
- 20-27度:打印"绿色提示:适宜温度"
- 低于20度:打印"蓝色预警:注意保暖"
temperature = 25 # 可以修改这个值测试不同情况
if temperature > 35:
print("红色预警:高温天气!")
elif temperature >= 28:
print("黄色预警:天气炎热")
elif temperature >= 20:
print("绿色提示:适宜温度")
else:
print("蓝色预警:注意保暖")
定义一个包含整数的列表 scores,赋值为 [85, 92, 78, 65, 95, 88]。
初始化两个变量:excellent_count 用于记录分数大于等于 90 的个数,初始值为 0;total_score 用于累加所有分数,初始值为 0。
使用 for 循环遍历 scores 列表中的每一个分数。
在循环内部:
将当前分数累加到 total_score 变量上。
使用 if 语句判断当前分数是否大于等于 90。如果是,则将 excellent_count 变量加 1。
循环结束后,计算平均分 average_score(总分除以分数的个数)。
使用 f-string 分三行打印出以下信息:
优秀分数(>=90)的个数。
所有分数的总和。
所有分数的平均分(结果包含3位小数)。
# 1. 定义分数列表
scores = [85, 92, 78, 65, 95, 88]
# 2. 初始化变量
excellent_count = 0
total_score = 0
# 3. 遍历列表并计算
for score in scores:
total_score += score # 4a. 累加总分
if score >= 90: # 4b. 统计优秀分数
excellent_count += 1
# 5. 计算平均分
average_score = total_score / len(scores)
# 6. 打印结果
print(f"优秀分数个数: {excellent_count}")
print(f"分数总和: {total_score}")
print(f"平均分数: {average_score:.3f}")
优秀分数个数: 2 分数总和: 503 平均分数: 83.833
@浙大疏锦行