本文共 1350 字,大约阅读时间需要 4 分钟。
2.2 流程控制-for序列
2.3 流程控制-for字典
a = '123456'for i in a:print (i, end = ' ')> 1 2 3 4 5 6 ####################l = [1, 2, 3, 4, 5]for i in l:print (i, end = ' ')> 1 2 3 4 5 ####################for i in for i in range(2,10,2):print (i, end = ' ')> 2 4 6 8 ##################### 列表重写for i in [i**2 for i in range(0,10) if i%2 != 0 ]:print (i, end = ' ')> 1 9 25 49 81 ##################### 1+2+3+......+99+100sum = 0for i in range(0,101):sum = sum + iprint (sum)> 5050
d = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4}for k in d:print (k,d[k])> a 0> b 1> c 2> d 3> e 4###########################d = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4}for k, v in d.items():print (k, v)> a 0> b 1> c 2> d 3> e 4############################# 99乘法表for i in range(1,10):for j in range(1,i + 1): print("%d*%d=%d"% (j, i, i * j), end=" ")print()
i = 0for a in range(1,5):print()for b in range(1, 5): for c in range(1, 5): for d in range(1, 5): if ( a != b and a != c and a != d and b != c and b != d and c != d): print('%s%s%s%s' % (a, b, c, d), end=' ') i += 1print ()print ('能组成%s个互不相同且无重复的数.'%i)
转载于:https://blog.51cto.com/13542406/2054904