---
# System prepended metadata

title: 11/9 Python基礎程式設計(三) Python Basic Tutorial (3)
tags: [2023社課共筆]

---

# 11/9 Python基礎程式設計(三) Python Basic Tutorial (3)
> [name=陳昱逢][time=19:00][color=black]
:::warning
今天主要課程為Python中的==Input== & ==Loop==

Today's main course covers ==Input== and ==Loop== in Python. 
:::
## :beginner: Input and Variables
:::success
在課堂上，此時你可能會有一個問題：如何輸入資料？ 如果沒有輸入怎麼辦？我真的需要電腦告訴我 if 之後的條件嗎？ 在本章中，您將學習如何使用Python進行輸入和變量，這將使上面的程式碼更加通用
At this point in the class, you might have a question: 'How do I input data? What if there's no input?' Do I really need the computer to tell me the condition after if? In this chapter, you will learn how to use Python for input and variables, which will make the above code more versatile
:::
![image](https://hackmd.io/_uploads/SJef3dqQT.png)
:::info
* Python 預設的 input 的資料型態是字串，若我們想做其他操作，須將它變為其他資料型態
* Python's default input data type is a string. If we want to perform other operations, we must change it to other data types.
:::
![image](https://hackmd.io/_uploads/B1nh2_q76.png)
:::info
* 因為 a 是字串，所以a + 4是錯的
* 但 
a = input()
print(a+'4')
可以
若輸入123，則輸出1234
* Because a is a string, a + 4 is wrong
* but 
a = input()
print(a+'4')
Valid
If 123 is entered, 1234 will be output
:::
![image](https://hackmd.io/_uploads/Hk51aOqma.png)
:::info
* 現在將 a 改為整數，若 a 輸入 5， a + 4 = 9
* Now change a to an integer. If a is entered as 5, a + 4 = 9
:::
![image](https://hackmd.io/_uploads/HkQl1F57p.png)

![image](https://hackmd.io/_uploads/r12MJKqXp.png)
:::info
* a = a + b => a += b
* a = a - b => a -= b
* a = a * b => a *= b
* a = a / b => a/= b
* a = a ** b => a **= b
* a = a // b => a //= b
* a = a % b => a %= b
:::
![image](https://hackmd.io/_uploads/SkjXxY9Q6.png)
:::info
Result : a = 2 * 3 = 6, b = 2 * '2' = 22 (2個2)
:::
![image](https://hackmd.io/_uploads/ryLP-Y5ma.png)
:::warning
自主練習
input : ++a++, ++b++
output : "a x b = ++ans++"
a, b 是我們輸入的值，當我們輸入後，計算 axb 的值，並 print "a x b = ++答案++"
:::
## :triangular_flag_on_post: Loop
:::success
有時，我們需要重複執行相同的程式碼。當然，你可以複製和貼上，但這可能很枯燥乏味。此外，對於複雜的程式碼，視情況而定，重複次數可能會有所不同。因此，Python引入了兩個
循環類型 : 
* while
* for

Sometimes, we need to execute the same code repeatedly. Sure, you can copy and paste, but that can be tedious. Additionally, for complex code, the number of repetitions may vary depending on the situation. Therefore, Python introduces two Loop type:
* while
* for
:::
![image](https://hackmd.io/_uploads/H1A4mtqXp.png)
:::info
* while 可看作一直重複的 if
* 我們通常會在 while 中放入一直改變的值，使其在某時刻能終止while迴圈
* 若沒有可終止while迴圈的條件，則會變成無窮迴圈
* while can be thought of as an if that repeats
* We usually put a changing value in the while so that it can terminate the while loop at a certain moment
* If there is no condition that can terminate the while loop, it will become an infinite loop.
:::
![image](https://hackmd.io/_uploads/r1pZEK576.png)
:::info
* endless loop
:::
![image](https://hackmd.io/_uploads/BJdr4t5mp.png)
:::info
* for迴圈可搭配 list 做使用
* list 裡可有不同的資料型態 
* for loop can be used with list
* There can be different data types in the list
```
for i in [3,8,3,[1,2],'a']: 
    print(i)
```
:::
![image](https://hackmd.io/_uploads/H1iyvF9mT.png)

![image](https://hackmd.io/_uploads/B1XeOYqXa.png)

:::info
* 若設定 for i in range(a) => i 會從 0 開始，並停在 i = a - 1，所以總共跑了(a-1)+1 = a次
* 若設定 for i in range(a, b) => i 會從 a 開始，並停在 i = b - 1，所以總共跑了b - a次 
* If you set for i in range(a) => i, it will start from 0 and stop at i = a - 1, so it will run (a-1)+1 = a times in total.
* If you set for i in range(a, b) => i, it will start from a and stop at i = b - 1, so it will run b - a times in total.
:::
![image](https://hackmd.io/_uploads/B1lzdK5mp.png)
:::info
* 若設定 for i in range(a, b, c) => i 會從 a 開始，接著 i = a+kc, k 是整數，最後停在比 b 小的位置
* 此程式印出 
==3== 
3+2===5==
3+2+2===7==

* If you set for i in range(a, b, c) => i will start from a, then i = a+kc, k is an integer, and finally stop at a position smaller than b
* This program prints
==3==
3+2===5==
3+2+2===7==
:::
![image](https://hackmd.io/_uploads/rJTttt5mp.png)

![image](https://hackmd.io/_uploads/BkxjKFcX6.png)
:::info
* continue : 跳到下個迴圈
* break : 終止迴圈
* continue: jump to the next loop
* break: terminate the loop
:::
![image](https://hackmd.io/_uploads/Sk9e9F97p.png)

![image](https://hackmd.io/_uploads/r1bH5Y5Qa.png)

:::info
* 第一個的程式碼的意思是 i 為 0 到 2，先印出一次，若為偶數，跳到下一個，若為奇數，再印出一次
* 第二個的程式碼的意思是 i 為 0 到 3，先印出一次，若為偶數，跳脫迴圈，若為奇數，再印出一次
* The meaning of the first code is that if i is 0 to 2, print it once. If it is an even number, jump to the next loop. If it is an odd number, print it again.
* The meaning of the second code is that if i is 0 to 3, print it once. If it is an even number, jump out of the loop. If it is an odd number, print it again.
:::
![image](https://hackmd.io/_uploads/Bku7stcQp.png)
:::info
* 迴圈也可以是多層的，愈裡面的迴圈愈先執行
* Loops can also be multi-layered. The loops inside are executed first.
:::
![image](https://hackmd.io/_uploads/H1ggioY5Qa.png)
:::warning
若要你做一個99乘法表，要怎麼做呢
If you want to make a 99 multiplication table, how do you do it?
:::
![image](https://hackmd.io/_uploads/H1LknK5Xa.png)

![image](https://hackmd.io/_uploads/SJNW2FcXT.png)

![image](https://hackmd.io/_uploads/BkNznYcmT.png)
##  回饋表單！Feedback Form!
:::info
[表單連結](https://forms.gle/y93zPCThQbsXwpos7)  :arrow_left: 
請幫我們填寫表單！讓我們的程式教學更加進步～
:::