迴圈

      在〈迴圈〉中有 1 則留言

為什麼要用迴圈

因為懶. 假設要計算 1+2+3+…+1000的總合, 如果沒有迴圈的話, 用手打會打到骨折. 但如果有迴圈, 則可以叫電腦依一定的次數, 重複執行.

for in

for in可以將list, tuple裏的值一個一個印出來, 如下所示

students=["Thomas", "John","Kevin"]
for x in students:
    print(x)

range

若要計算1+2+3+…+10, 可以寫成如下

sum=0
for x in [1,2,3,4,5,6,7,8,9,10]:
    sum+=x;
print(sum)

但如果要計算 1+2+3+…+100呢, 那還得了. 此時就需使用range()產生list了. 比如 range(101), 就會產生 0-100之間的集合

sum=0
for x in range(101):
    sum+=x;
print(sum)

while

第二種迴圈為 while 條件, 如下代碼

i=1
sum=0
while i<=100:
    sum+=i
    i=i+1
print(sum)

break

當某條件成立時, 使用break, 則會直接跳離 while迴圈

i=0
sum=0
while True:
    i+=1
    if(i>100):
        break
    sum+=i
print(sum)

continue

當某條件成立時, 使用continue, 則會跳到while之處, 如下

i=0
sum=0
while i<100:
    i+=1
    if(i%2==0):
        continue
    sum+=i
print(sum)

巢狀迴圈(Nested)

下面代碼, 列出 九九乘乘法的演算

for i in range(1, 10):
    for j in range(1, 10):
        print("%2d*%2d=%2d  " % (i, j, i*j), end='')
    print()

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

Exam1

1
2
3
….
100
for i in range(1, 101):
    print("%d" % i)

Exam2

列印
1 2 3 4 5 6 7 8 9 10
for i in range(1, 11):
    print("%d " % i, end='')

Exam3

列印
* * * * *
for i in range(5):
    print("* ", end='')

或不使用迴圈
print("* " * 5)

Exam4

列印
* * * * *
* * * * *
* * * * *
for i in range(3):
    for j in range(5):
        print("* ", end='')
    print()

Exam5

列印
1  2  3  4  5  6  7  8  9
2  4  6  8 10 12 14 16 18
3  6  9 12 15 18 21 24 27
4  8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
for i in range(1, 10):
for j in range(1, 10):
print("%2d " % (i*j), end='')
print()

%2d : 靠右對齊,%02d左邊補0

Exam6

*
* *
* * *
* * * *
* * * * *
for i in range(5):
    for j in range(i+1):
        print("* ", end='')
    print()

Exam7

* * * * *
* * * *
* * *
* *
*
for i in range(5):
    for j in range(5-i):
        print("* ", end='')
    print()

Exam8

        *
      * *
    * * *
  * * * *
* * * * *
for i in range(5):
    for j in range(4-i):
        print("  ", end='')
    for j in range(i+1):
        print("* ", end='')
    print()

Exam9

< < < < <
> > > > >
< < < < <
> > > > >
< < < < <
for i in range(5):
    for j in range(5):
        if(i%2==0):
            print("< ", end='')
        else:
            print("> ", end='')
    print()

Exam10

A A A A A
B B B B B
C C C C C
A A A A A
B B B B B
for i in range(5):
    for j in range(5):
        if i%3==0:
            print("A ", end='')
        elif i%3==1:
            print("B ", end='')
        else:
            print("C ", end='')
    print()

Exam11

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
a b c d e f g h i j k l m n o p q r s t u v w x y z
start=65
for i in range(start, start+26):
    print("%c " % i, end='')
print()
start=97
for i in range(start, start+26):
    print("%c " % i, end='')
print()

1 thought on “迴圈

  1. Teodoro Murfin

    I’m startled by your capability to make even the most ordinary subjects captivating. Kudos to you!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *