---
# System prepended metadata

title: PYTHON

---

# PYTHON

######

> This note is yours, feel free to play around.  :video_game: 
> Type on the left :arrow_left: and see the rendered result on the right. :arrow_right: 

## :memo: TEST 1: 輸入(轉整數)、輸出、運算子

### test1- 1: 
![](https://i.imgur.com/6BxKxrU.png)
```python=
ch, en, ma = int(input()), int(input()), int(input())          # int() 將文字強制轉整數
avg = (ch*4+en*3+ma*3)/10
print(avg)

```
### test1- 2: ![](https://i.imgur.com/peZqsGY.png)
```python=
x1, y1, x2, y2 = float(input()), float(input()), float(input()), float(input())  # float() 將文字強制轉浮點數(俗稱小數)
dist = ((x2-x1)**2+(y2-y1)**2)**1/2
print(dist)

```
:::info
## Note:
>- 整數(integer)：正負整數及0，可使用int()函數將字串轉成整數
>- 浮點數(float)：帶有小數的數值，可使用float()函數將字串轉成浮點數
>- 字串(string)：用單引號(')或雙引號(")含括，如:"Good"，可使用str()函數將數值轉成字串
>- 布林值(boolean)：只有True和False(注意大小寫)，True代表成立、False代表不成立**
:::
## :memo: TEST 2:判斷結構
### TEST2-1: ![](https://i.imgur.com/8WsUonF.png)
```python=
n = int(input())
if (n%2==0):
    print("偶數")
else:
    print("奇數") 
```
### TEST2-2: ![](https://i.imgur.com/eRhy2Gv.png)
```python=
k= int(input())
if (k%3==0):
  print("good")
elif (k%3==1):
  print("soso")
else: 
  print("bad")

```

### TEST2-3: ![](https://i.imgur.com/eEcYdCE.png)
```python=
y = int(input())
if ((y%400==0) or (y%4==0 and y%100!=0)):
    print("閏年")
else:
    print("平年")
```
## :memo: TEST 3:重覆結構
![](https://i.imgur.com/hvgty2x.png)
```python=
n = int(input())          # 整數版本寫法
sum = 0                   
while (n!=10):           # 直到n變成0為止
    sum = sum + n%10     # 重覆一: 將n的個位數加至總和sum
    n //=  10      # 重覆二: 將n的個位數去除

print(sum)
```
### TEST3-2:![](https://i.imgur.com/rljRJiW.png)
```python=
n = input()               # 字串版本寫法
sum = 0                   
for x in n:               # 走訪字串n的每個字元，存到變數x
  sum += int(x)    # 將變數x轉成整數加至總和sum
 
print(sum)
```
### TEST3-3:![](https://i.imgur.com/PvAlzcV.png)
```python=
import random                    # 匯入random模組
ans =  random.randint(1,100)     # 利用random模組的randint()函數，從1到100之間取一個亂數
print('請猜一個1~100的數字')
play = True
while(play):
    guess = int(input('來隨便猜一個數字吧: ')) 
    if (guess>ans):
        print('喔你猜得太大囉')
    elif (guess<ans):
        print('喔你猜得太小囉')
    else:
        print('恭喜你猜對了！')
        play = False
```
### TEST3-4:![](https://i.imgur.com/6ErJXuq.png)
```python=
n = int(input())
flag = 0                        # flag值為0代表質數，1代表合數
for i in range(2,n):   # 從2到n-1，若有一個數可以把n整除，則n為合數
    if (n%i==0):
        flag = 1
        break                   # break語法用來跳出迴圈

if (flag==0):
    print("質數") 
else:
    print("合數")           
```
# :memo: TEST 4:string、list 
### TEST4-1: 
![](https://i.imgur.com/Zvb5elb.png)
```python=
num = input().split()      # num = ['20', '30', '40']
ans = 0
for x in num:
    ans += int(x)

print(ans)
```
### TEST4-2:![](https://i.imgur.com/glrAnyh.png)
```python=
ans = 0
for x in input().split():
    ans += int(x) 
print(ans)
```
### TEST4-3:![](https://i.imgur.com/EOiJa3h.png)
```python=
num = [int(x) for x in input().split()]         # 此稱之為List Comprehension 列表建構
print(sum(num))
```
:::info
## Note:
**列表建構 (List Comprehension)**
![](https://i.imgur.com/aA4J7Rm.png)
:::

### TEST4-4:
![](https://i.imgur.com/MOrVa8c.png)
```python=
print(sum([int(x) for x in input().split()]))
```
### TEST4-5:![](https://i.imgur.com/s2NDGw9.png)
```python=
print(sum([int(x) for x in input()]))
```

# :memo: TEST 4:string、list 
# :memo: TEST 4:string、list 