#循环
#①for循环,格式为:for 变量名 in 序列:
#for循环的执行次数等于序列里面的元素的个数,适用于知道执行多少次的情况
#序列,可以是range可以是容器:range(10),就是0到9之间的10个数字,也可以写成range(0,10),是一样意思
#序列步长,range(1,100,10),每个10个元素获取一个元素的意思
#for循环遍历range
for s in range(1,10,3):
print(s)
#for循环遍历容器,将容器中的数值一个个的取出来
#for循环遍历列表,列表是有顺序的,可以通过索引数来遍历
names=["王一","赵二","张三","李四"]
a=input("请输入你想查找的名字")
c=0
for b in range(len(names)):
if a==names[b]:
c=1
# break
#else:
# c=0
if c==1:
print(a,"在班级中")
else:
print(a,"不在班级中")
#for循环遍历字典,只获取键,字典是无序的就没有索引,就无法通过索引长度来遍历
info={"name":"张三","age":"18","adress":"河北"}
for e in info:
print(e)
print(info[e])
g=0
for f in range(1,11):
g+=1.2
print("第",f,"年还款1.2万","共还款",round(g,2),"万")
#for循环经典问题,最大最小值
#python自带函数max(),min(),可以直接获取最大最小值,但是我们不用用for循环试下
score=[88,22.92,38,91,37,93]
for a in score:
maxs=0
if a>=maxs:
maxs=a
print("最大分数为",maxs)
1 4 7 请输入你想查找的名字张三 张三 在班级中 name 张三 age 18 adress 河北 第 1 年还款1.2万 共还款 1.2 万 第 2 年还款1.2万 共还款 2.4 万 第 3 年还款1.2万 共还款 3.6 万 第 4 年还款1.2万 共还款 4.8 万 第 5 年还款1.2万 共还款 6.0 万 第 6 年还款1.2万 共还款 7.2 万 第 7 年还款1.2万 共还款 8.4 万 第 8 年还款1.2万 共还款 9.6 万 第 9 年还款1.2万 共还款 10.8 万 第 10 年还款1.2万 共还款 12.0 万
最后
以上就是勤恳音响最近收集整理的关于python基础语法之--for循环的全部内容,更多相关python基础语法之--for循环内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复