---
# System prepended metadata

title: Python

---

# Python
## 目錄
[TOC]
## 引入函式
```python=
    #m1.py
import m2 #類似將m2 include進來
m2.printSelf() #使用m2中的函式printSelf()

    #m2.py
def printSelf():
	print('In m2')
```
```python=
import xmath as math #為 xmath 模組取別名為 math
from xmath import min  # 只引入xmath中的min函式
```
c
```python=
x = 3
y = 4.124
z = "Python"
#宣告一個變數
x, y, z = 2, 4.124, "Python"
x = y = z = "Blue"
#一次宣告多個變數
var = None
#宣告一個變數不給初始值
```
## 基礎運算子
![](https://i.imgur.com/BOnWJB2.png)
![](https://i.imgur.com/ctz32n2.png)
![](https://i.imgur.com/11IcV2G.png)
![](https://i.imgur.com/YceBXJE.png)
![](https://i.imgur.com/WjBpxEc.png)
![](https://i.imgur.com/ZQo1Qoz.png)
![](https://i.imgur.com/uwOCQW6.png)

## 基本輸入輸出
```python=
name = input('請輸入你的名稱：')
print('歡迎', name)
```
> 輸出不換行
> python輸出後面會自動加上\n
```python=
print("請輸入你的名稱：",end = '')
name = input()
print('歡迎', name)
```
### practice
- [ 小練習1 ]
**Q:輸入三個成績並顯示其算術平均**
```python=
a = input('a的成績:')
b = input('b的成績:')
c = input('c的成績:')
ave = (int(a)+int(b)+int(c))/3
print("平均為:",end = '')
print(ave)
```
- [ 小練習2 ]
**Q:輸入五個成績，算出其總和和平均**
```python=
a = input('GRADE1:')
b = input('GRADE2:')
c = input('GRADE3:')
d = input('GRADE4:')
e = input('GRADE5:')
total = int(a)+int(b)+int(c)+int(d)+int(e)
ave = int(total)/5
print("總合為:",end = '')
print(total)
print("平均為:",end = '')
print(ave)
```
- [ 小練習3 ]
**Q:輸入身高 體重，算出BMI**
```python=
height = float(input('身高(cm):'))
weight = float(input('體重(kg):'))
height = height / 100
BMI = weight / (height * height)
print("BMI = ",end = '')
print(BMI)
```
- [ 小練習4 ]
**Q:輸入長 寬 高，算出長方體面積**
```python=
lenth = float(input("長:"))
width = float(input("寬:"))
hei = float(input("高:"))
ans = (lenth*width*2)+(lenth*hei*2)+(width*hei*2)
print("面積:" ,end = '')
print(ans)
```
- [ 小練習5 ]
**Q:輸入直徑，算出圓面積**
```python=
r = float(input("直徑:"))
r = r/2
ans = r*r*3.14
print("面積:" , end = '')
print(ans)
```
## 條件判斷
### if...elif...else
```python=
height = float(input("身高:"))
if height >= 180 :
    print("tall")
elif height >= 170 :
    print("soso")
else :
    print("short")

```
![](https://i.imgur.com/pjs3sRu.png)
### practice
- [ 小練習1 ]
**Q:輸入分數判斷是否及格(>60) 被當 還是死當(<40)**
```python=
score = float(input("分數:"))
if score >= 60 :
    print("及格")
elif score >= 40 :
    print("被當")
else :
    print("死當")
```
- [ 小練習2 ]
**Q:輸入數字判斷其為基數或偶數**
```python=
num = int(input())
if num % 2 == 0 :
    print("偶數")
else :
    print("基數")
```
- [ 小練習3 ]
**Q:輸入數字判斷是整數還是浮點數**
```python=
num = float(input())
test = int(num)
if num == test:
    print("整數")
else:
    print("浮點數")
```
- [ 小練習4 ]
**Q:輸入數字判斷是否為3或7的倍數**
```python=
num = int(input())
if num % 3 == 0 :
    print("yes")
else :
    if num % 7 == 0 :
        print("yes")
    else :
        print("no")
```
- [ 小練習5 ]
**Q:輸入數字判斷是否為3和7的公倍數**
```python=
num = int(input())
if num % 3 == 0 :
    if num %7 == 0 :
        print("yes")
    else :
        print("no")
else :
    print("no")
```
- [ 小練習6 ]
**Q:輸入兩數字，輸出其最大值**
```python=
num1 = int(input())
num2 = int(input())
if num1 > num2 :
    print(num1)
else :
    print(num2)
```
## 結構化程式
### while loop
```python=
i = 0
while i < 101:
    print(i)
    i = i + 1
```
![](https://i.imgur.com/DbdAz6i.png)
### practice
- [ 小練習1 ]
**Q:輸入任意兩數，計算出其最大公因數**
```python=
i = 1
j = 0
a = int(input())
b = int(input())
while i <= a and i <= b :
    if a%i == 0 and b%i == 0:
        j = i
    i = i + 1
print(j)
```
- [ 小練習2 ]
**Q:輸入一個整數，其剛好介在兩個數平方之間，求哪兩數**
```python=
a = int(input())
i = 0
while i**2 <= a :
    i = i+1
print(i-1)
print(i)
```
- [ 小練習3 ]
**Q:輸入任意數字，將其數字全部倒轉**
```python=
a = int(input())
i = 10
while a > 0 :
    print(a%i,end="")
    a = int(a/i)
```
### for loop
```python=
for i in range(5,100,50):
    print(i)
```
![](https://i.imgur.com/RG3MBlq.png)
### practice
- [ 小練習1 ]
**Q:輸出1加到10的總和**
```python=
sum = 0
for i in range(1,11,1):
    sum = sum + i
print(sum)
```
- [ 小練習2 ]
**Q:輸出0到20之所有偶數**
```python=
for i in range(0,21,2):
    print(i,end=" ")
```
- [ 小練習3 ]
**Q:從100輸出到0**
```python=
for i in range(100,-1,-1):
    print(i,end=" ")
```
- [ 小練習3 ]
**Q:從100輸出到0**
```python=
for i in range(100,-1,-1):
    print(i,end=" ")
```
- [ 小練習4 ]
**Q:輸入一個整數，計算小於等於此整數所有數的平方和**
```python=
a = int(input())
sum = 0 
for i in range(1,a+1,1):
    sum = sum + i**2
print(sum)
```
- [ 小練習5 ]
**Q:輸出9.9乘法表**
```python=
for i in range(1,10):
    for j in range(1,10):
        print(i,end="")
        print("*",end="")
        print(j,end="")
        print("=",end="")
        print(i*j)
```
- [ 小練習6 ]
**Q:輸入一個整數，輸出邊長為該整數之三角形**
```python=
a = int(input())
for i in range(1 , a+1 , 1):
    for j in range(1,a-i+1,1):
        print(" ",end="")
    for k in range(1,i+1,1):
        print("*",end="")
    print()
```
## array陣列
### 一維陣列
```python=
a = [0]*5
print(a)
```


| 0    |  0   |   0  |  0   |    0 |
| --- | --- | --- | --- | --- |

```python=
a = [1,2,3,4,5]
print(a)
```
| 1   |  2   |   3  |  4   |    5 |
| --- | --- | --- | --- | --- |
### practice
- [ 小練習1 ]
**Q:利用字元陣列儲存名字**
```python=
name = [0]*5
name = input()
for i in range(4):
    print(name[i],end = '')
```
- [ 小練習2 ]
**Q:輸入一數字，檢查陣列中有無相同數字**
```python=
arr = [0,1,2,3,4,5,6,7,8]
num = int(input())
check = False

for i in range (9):
    if(arr[i] == num):
        check = True

print(check)
```
- [ 小練習3 ]
**Q:設定人數，輸入成績取其平均**
```python=

```






![](https://i.imgur.com/qPGyPuC.png)

```python=
a = input()
a = a.split(' ')
count = 0
for i in range(len(a)):
    a[i] = int(a[i])
for i in range (len(a)):
    count = count + a[i]
print(count)
```





