---
# System prepended metadata

title: wk05_1005_Loop

---

# wk05_1005_Loop

1. range 函式
    -認識 range 函式
    -range 函式的語法
2. for 迴圈
    -巢狀 for 迴圈
    -break 命令
    -continue 命令
3. while 迴圈

## {inclass_practice}


```python
print("**" * 3)


print("i love u. \n " * 3)
```

    ******
    i love u. 
     i love u. 
     i love u. 
     
    


```python
print("*" * 1)
print("*" * 2)
print("*" * 3)
print("*" * 4)
print("*" * 5)
```

    *
    **
    ***
    ****
    *****
    


```python
for i in [1, 2, 3, 4, 5] :
    print("@" * i)
```

    @
    @@
    @@@
    @@@@
    @@@@@
    


```python
a1 = 100
a2 = 100
a3 = 80
# 不同方式的資料儲存
a_list = [100, 100, 80]
# 從零開始

print(a1, a2, a3)
print(a_list)

print(a_list[0])
```

    100 100 80
    [100, 100, 80]
    100
    


```python
# 1加到3
n = 3

ttl = 0+1
ttl = ttl + 2
ttl = ttl + 3

print(ttl)
```

    6
    


```python
n = 3 

ttl = 0
# 乘法不能用 0
for i in[1,2,3,4,5,6,7,8,9,10] :
    ttl = ttl + i
    
 
print(ttl)
```

    55
    


```python
print(list(range(5)))


print(list(range(3, 10)))


print(type(range(5)))
print(type(list(range(3, 10))))
```

    [0, 1, 2, 3, 4]
    [3, 4, 5, 6, 7, 8, 9]
    <class 'range'>
    <class 'list'>
    


```python
n = 1000

ttl = 0
for i in range(1, 1001) :
    ttl = ttl + i
    
 
print(ttl)
```

    500500
    


```python
n = 2000000

ttl = 0
for i in range(1, 2000001) :
    ttl = ttl + i
    
 
print(ttl)
```

    2000001000000
    


```python
n = int(input("n?"))

ttl = 0
for i in range(1, n+1) :
    ttl = ttl + i
    
 
print(ttl)
```

    n?10
    55
    


```python
n = int(input("n?"))

ttl = 0
for i in range(1, n+1, 2) :
    ttl = ttl + i
    
 
print(ttl)
```

    n?10
    25
    


```python
n = int(input("n?"))

ttl = 0
for i in range(1, n+1) :
    if i%7 == 0 and i%11 == 0 :
        ttl = ttl + i
        
    
 
print(ttl)
```

    n?1000000
    6493993506
    


```python
a = 9
b = 9


for j in range(2, 9+1) :
    print("這是",j,"的乘法")
    for i in range(1, 9+1) :
        print(j, "*", i, "=", j * i)




#print("2*1 = 2")
#print("2*2 = 4")
#print("2*9 = 18")
```

    這是 2 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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
    

## {afterclass_practice}

### 1.執行下列程式，下列結果何者正確？
list1 = range(5)

print(list(list1))

(A) [1, 2, 3, 4]       (B) [ 1, 2, 3, 4,5]    
(C) [0, 1, 2, 3, 4]    (D) [0, 1, 2, 3, 4,5]



```python
list1 = range(5)
print(list(list1))
```

    [0, 1, 2, 3, 4]
    

ans : (C)

### 2.執行下列程式，下列結果何者正確？
list1=range(6,0,-2)

print(list(list1))

(A) [6, 4, 2]  (B) [6, 0, -2]  (C) [6, 4, 2, 0]  (D) [6, 4, 2, 0, -2]


```python
list1=range(6,0,-2)

print(list(list1))
```

    [6, 4, 2]
    

ans :  (A)

### 3.執行下列程式，結束迴圈後，n 的值為多少？
for n in range(1,5,2):

    print(n,end=" ")  
    
print("結束迴圈後 n=",n)

(A) 1  (B) 3  (C) 5  (D) 7


```python
for n in range(1,5,2):

    print(n,end=" ")  
    
print("結束迴圈後 n=",n)
```

    1 3 結束迴圈後 n= 3
    

ans : (B)

### 4.執行 for 廻圈時，如果想要提前離開廻圈，應使用何種指令？
(A) break   (B) return   (C) exit   (D) pause

ans : (A)

### 5.執行下列程式，下列結果何者正確？
list1 = range(5,-1,-2)

print(list(list1)) 

(A) [5,-1,-2]   (B) [ 5,4,3,2,1,0,-1]   (C) [5, 3, 1]    (D) [5,3,1,-1]


```python
list1 = range(5,-1,-2)

print(list(list1)) 
```

    [5, 3, 1]
    

ans : (C)

### 6.while 廻圈若一開始測試條件就不成立，則 while 內程式區塊將會如何處理？
(A) 執行一次  (B) 一次都不執行  (C) 重複執行  (D) 編譯錯誤

ans : (B)

### 7.下列那一個指令可在廻圈中跳過後面的敘述直接回到廻圈的開頭？

(A) exit   (B) return   (C) pause   (D) continue 

ans : (D)

### 8.執行下列程式，結束迴圈後，sum 的值為多少？
sum = 0

n=8

for i in range(1, n+1,2):

    sum += i
    
print(sum)

(A) 8  (B) 9  (C) 16  (D) 28 


```python
sum = 0
n=8
for i in range(1, n+1,2):
    sum += i
print(sum)  
```

    16
    

ans : (C)

### 9.執行下列程式，可看到多少個「#」字？
n=5

for i in range(1,n): 
    
    for j in range(1,i+1): 
        
        print("#", end="")
        
    print() 
    
(A) 5  (B) 6  (C) 7  (D) 10


```python
n=5

for i in range(1,n): 
    
    for j in range(1,i+1): 
        
        print("#", end="")
        
    print() 
```

    #
    ##
    ###
    ####
    

ans : (D)

### 10.執行下列程式，結束迴圈後，total 的值為多少？
total = i = 1

n=5

while(i<=n):

    total *= i
    
    i+=1 
    
print(total) 

(A) 5	 (B) 24	 (C) 120	(D) 720


```python
total = i = 1

n=5

while(i<=n):

    total *= i
    
    i+=1 
    
print(total) 
```

    120
    

ans : (C)