DAY3 循环判断语句

📅 2026/7/12 3:36:20 👁️ 阅读次数 📝 编程学习
DAY3 循环判断语句

@浙大疏锦行

一、知识点

1.1循环语句

range()生成序列,用于控制for循环次数(左闭右开)

range(stop) # 从 0 开始到 stop-1

range(start, stop) # 从 start 开始到 stop-1

range(start, stop, step) # 从 start 开始,步长为 step,到 stop-1

#循环 for tech in tech_list: # 逐个取出每个元素 print(tech) # 打印每个技术名称 #判断 if temperature > 35: print("红色预警:高温天气!") elif temperature >= 28: print("黄色预警:天气炎热") elif temperature >= 20: print("绿色提示:适宜温度") else: print("蓝色预警:注意保暖")

二、题目

age_str="25" height_str="1.75" score=95 age=int(age_str) height=float(height_str) score_str=str(score) total=age+score print(f'年龄(整数):{age},类型:{type(age)}') print(f'身高(浮点数):{height},类型:{type(height)}') print(f'分数(字符串):{score_str},类型:{type(score_str)}') print(f'年龄+分数:{total}')
password="python123" min_length=8 length=len(password) str6=password[:6] first=password[0] last=password[-1] judge=str6=='python' print(f'密码:{password}') print(f'密码长度:{length}') if length>=8: print("长度符合要求") else: print("长度不符合要求") print(f'首字符:{first},尾字符:{last}') print(f'前6个字符:{str6}') print(f'前6个字符是否为python:{judge}')
scores=[85,92,78,65,95,88] excellent_count=0 total_score=0 for i in scores: total_score+=i if(i>=90) : excellent_count+=1 length=len(scores) average_score=total_score/length print(f'优秀分数个数:{excellent_count}\n分数总和:{total_score}\n平均分数:{average_score:.3f}')