#for案例str1 = 'abcde'for s in str1: print(s,end=' ')print()list1= ['张三','李四',5,'赵六']for s in list1: print(s, end=',') print(type(s),end=';')print()list1= ['张三','李四',5,'赵六']for s in list1: if type(s) is str: for a in s: print(a,end=',') else: print(s,end=',')print()#range()函数:可以生成数字列表list1 = list(range(0,10)) # 生成[0,10)范围列表print(list1)list1 = list(range(10)) # 生成[0,10)范围列表print(list1)list1 = list(range(0,20,3)) # 生成[0,20)范围,步长为3的列表print(list1)list1 = list(range(20,0,-3)) # 生成[20,0)范围,步长为-3的列表print(list1)list1 = list(range(9,-1,-1)) # 生成[9,-1)范围,步长为-3的列表print(list1)s = sum(range(1,101))print(s)s = 0 # s表示1-100的和,初始值为0for i in range(1,101): s = s + iprint(s)for i in range(1,10): for j in range(1,i+1): print('%d*%d=%d'%(i,j,i*j),end=" ") print()for i in range(4): for j in range(3-i): print(" ",end='') for k in range(2*i+1): print("*",end='') print()
for循环应用
最新推荐文章于 2023-03-05 09:46:49 发布