Pythonの循環文---99乗算表Python版

2634 ワード

ナレッジポイント1ループ:特定のコードを繰り返し実行し、構文:
while   (                 ) :
         ,    1
         ,    2
         ,    3
    
        (   +1)

例:
i = 0
while i < 5:
    print("Python,ni hao")
    i = i + 1
print("       ,i=%d" % i)

注意:
デッドサイクルを防ぐ.必ずカウンタの変更があります.
作業1:積算計算0~100間の数字積算合計
    
resut = 0
i = 0 
while i <= 100:
    result += i
    i += 1
print("      =%d" % result)

ジョブ2:0~100の偶数を加算
result = 0
i = 0 
while i <= 100
    if i % 2 == 0:
        result += i
    i += 1
print("0~100      =%d" % result)

知識点2,break,continue
breakは、現在のループを終了し、残りの操作を実行しません.
i = 0
while i < 55 :
    if i==4:
        break
        print(i)
    i += 1
print("wnabu,i=%d" % i)

結果:
wnabu,i=4

continue、ある条件が満たされた場合、後のコードは実行されません.
例:4という数字をスキップして読まない
i = 0
while i < 10 :
    if i==4:
        #         ,              ---         
        i += 1
        continue

    print(i)
    i += 1
print("wnabu,i=%d" % i)

実行結果
0
1
2
3
5
6
7
8
9
wnabu,i=10

拡張:
print(i,end=“--”)
i = 0
while i < 10 :
    if i==4:
        #         ,              ---         
        i += 1
        continue

    print(i,end="---")
    i += 1
print("wnabu,i=%d" % i)

 
0---1---2---3---5---6---7---8---9---wnabu,i=10

ナレッジポイント3、ループネスト(条件ネストと同様)
whileの中にはwhileもあります.ループネストです.
例:乗算テーブルのソースコードPython版
i = 1
while i < 10 :
    j=1
    while j <= i :
        print("%d * %d = %d" % (i,j,i*j) , end="\t")
        j += 1
    print("")
    i += 1
print("    ,   ")

結果:
1 * 1 = 1	
2 * 1 = 2	2 * 2 = 4	
3 * 1 = 3	3 * 2 = 6	3 * 3 = 9	
4 * 1 = 4	4 * 2 = 8	4 * 3 = 12	4 * 4 = 16	
5 * 1 = 5	5 * 2 = 10	5 * 3 = 15	5 * 4 = 20	5 * 5 = 25	
6 * 1 = 6	6 * 2 = 12	6 * 3 = 18	6 * 4 = 24	6 * 5 = 30	6 * 6 = 36	
7 * 1 = 7	7 * 2 = 14	7 * 3 = 21	7 * 4 = 28	7 * 5 = 35	7 * 6 = 42	7 * 7 = 49	
8 * 1 = 8	8 * 2 = 16	8 * 3 = 24	8 * 4 = 32	8 * 5 = 40	8 * 6 = 48	8 * 7 = 56	8 * 8 = 64	
9 * 1 = 9	9 * 2 = 18	9 * 3 = 27	9 * 4 = 36	9 * 5 = 45	9 * 6 = 54	9 * 7 = 63	9 * 8 = 72	9 * 9 = 81	
    ,   

拡張、エスケープ文字
\t,,\*
表が適用されない場合は、列のテキストを垂直方向に配置します.
エスケープ文字
\\
スラッシュ記号
\'
一重引用符
\"
二重引用符
折り返し
\t
よこほうこうタブ
\r
リターンマッチ