在开发语言里面我们也经常使用for循环,主要是用来遍历一些集合类型的数据。这里我们演示下for循环:
1)遍历列表的数据
nos = [4,2,1,3,5,6,7,8,10] for no in nos: print(no)
2)顺序遍历
nos = [4,2,1,3,5,6,7,8,10] for i in range(len(nos)): print(nos[i])
3)遍历数字
for i in range(10): print(i)
4)遍历字典
a = {"name": "张三", "age": 15, "sex": "男"} for k, v in a.items(): print(k, v)
备注:
1、以上就是常用的for循环使用。 2、如果遍历字典的话需要遍历的对象是xx.items() 3、遍历数值的话,需要使用range。 4、遍历的时候不需要写i++.
还没有评论,来说两句吧...