#range当做:范围列表[],列表中的元素是数字,且可控的有效数字范围 #一般与for 配合使用 # for i in range(1,10): #[1,2,3,4...9] # print(i) # for i in range(1,101): # # print(i) # for i in range(11): # # print(i) # for i in range(1,10,2): # [1,3,5,7,9] # print(i) # for i in range(10,0,-1): # print(i) # for i in range(10,-2,-1): # print(i) l1 = [1,2,3,'alex',[2,3,'wusir'],7] for i in l1: if type(i) == list: for j in i: print(j) else: print(i)
个人理解 range的用法和切片索引差不多,有异曲同工之妙.或者这个就是按照索引和切片来的吧.
l1 = [1,2,3,'alex',[2,3,'wusir'],7] l3=l1[4]="2\n3\nwusir" for i in l1: print(i)
上面是我用for语句替换的列表中小列表的元素的代码,用于练习.和理解索引和切片的概念,以及应用