# ### for循环 # 循环 遍历 迭代 # 把列表的元素一一的拿出来遍历 listvar = ["黄雄大","黄文","黄仪正","黄少正","黄广雄","黄将用"] # 用len获取列表的长度 res = len(listvar) print(res) #(1) while 的遍历方式 """ i = 0 while i < 0 len(listvar): '''0 1 2 3 4 5''' res = listvar[i] print(res) i+=1 """ setvar = {"黄雄大","黄文","黄仪正","黄少正","黄广雄","黄将用"} #while 循环有它的局限性,不能够遍历集合这样的数据类型, #所以for 循环应用而生 特指用来遍历的 ''' while i < len(setvar): res = setvar[i] print(res) i+=1 ''' ''' 容器类型数据是可迭代对象的一种,后期我们还要学迭代器 生成器 range对象 for变量 in 可迭代的对象 print(变量) ''' """ 代码解析: 把容器类型里面的每一个元素,依次的拿出来赋给变量i, 直到所有数据依次遍历完毕,退出循环 """ #遍历列表 container = ["黄雄大","黄文","黄仪正","黄少正","黄广雄","黄将用"] #遍历元组 container = ("黄雄大","黄文","黄仪正","黄少正","黄广雄","黄将用") #遍历集合 container = {"黄雄大","黄文","黄仪正","黄少正","黄广雄","黄将用"} #遍历字典 contaner = {"top":"亚瑟","moddle":"王军","bottom":"黄雄达"} #遍历字符串 container = "我爱你,亲爱的滴菇娘,见到你我就心慌" for i in container: print(i) # (3)遍历等长的二级容器 listvar = [("王建林","王思聪","王美丽"),["马云","马华腾","马美丽"],["王宝强","马蓉","宋小宝"]] # 变量的解包 a,b = 1,2 print(a,b) a,b = [3,4] print(a,b) a,b ={5,6} print(a,b) for a,b,c in listvar: ''' a,b,c = ("王建林","王思聪","王美丽") a,b,c = ["马云","马华腾","马美丽"] a,b,c = ["王宝强","马蓉","宋小宝"] ''' print(a,b,c) #(4)遍历不等长的二级容器 listvar = [("王建林","王思聪","王美丽"),["马云","马华腾"],["王宝强"]] for i in listvar: for j in i: print(j) #(5)range 对象 """ range(start ,end, step) start:开始值 end: 结束值 step:步长 使用方式和切片语法类似,高位end那个值取不到,取到end之前的那个值 """ #1个参数的情况,range(5) =>0 1 2 3 4默认从0 开始,高位5 取不到,最大取到4 print(range(5),type(range)) res = list(range(5)) #2个参数的情况 for i in range(3,10): print(i) """正序""" for i in range(2,11,2): print(i) '''倒序''' #打印9~1 9 8 7 6 5 4 3 2 1 for i in range(9,0,-1): print(i)