---
title: 基礎回顧 - Python 教學
description: 中興大學資訊研究社1091學期程式分享會主題社課
tags: Python
---
<!--觀看次數-->

###### [Python 教學/](/@NCHUIT/py)
# 基礎回顧
> [name=VJ][time= 109,5,4]
---
## 資料型態與轉型
###### *詳細參閱: [Python內建資料型態](https://docs.python.org/zh-tw/3/library/stdtypes.html)*
| `int` | `float` | `str` | `bool` |
| ----- | ------- | ----- | ------ |
| 整數 | 浮點數 | 字串 | 布林值 |
| `list` | `dict` | `tuple` | `set` |
| ------ | ------ | ------- | ----- |
| 串列 | 字典 | 元組 | 集合 |
---
```python=!
a = 100
b = True #首字母要大寫
c = 'word' #注意 Python 的字串用''或""都可以
d = [-1,False,c] #想塞什麼就塞什麼
e = {'mon':'mother','dad':'father'}
f = int(7942)
g = bool(0) #同 g = False 另 0,'',None 以外的變數會轉為 True
h = float(True) #同 h = 1.0
i = str() #空字串
j = list() #空串列
k = dict() #空字典
#...
```
---
## 輸出和輸入
```python=!
print ('輸出內容')
a = input ('輸入提示') #input() 在程式中被當作一個字串變數
print ('你輸入了:', a))
```
---
### 練習 1
step1:
在colab 使用 input() 函式 輸入,並 使用 type() 觀察輸入進去的資料型態
step2:
想辦法使你的輸入轉為 整數 型態
並印出來
>hint: input(),type()
---
## 運算元
Python 的運算遵循四則運算,會先乘除後加減
| 算數運算 | 加 | 減 | 乘 | 除 |
| -------- | --- | --- | --- | --- |
| 符號 | + | - | \* | / |
| 整除 | 取餘 | 次方 |
| ---- | ---- | ---- |
| // | % | ** |
---
| 邏輯運算 | 且 | 或 | 非 | 比較位置 |
| -------- | --- | --- | --- | ---- |
| 符號 | and | or | not | is |
| 算數邏輯運算 | 大於 | 小於 | 等於 |
| ------------ | ---- | ---- | ---- |
| 符號 | > | < | == |
| 不小於 | 不大於 | 不等於 |
| ------ | ------ | ------ |
| >= | <= | != |
---
### 練習 2.1
反向BMI 計算機
輸入 BMI與身高,而後印出體重

---
## 條件運算
| 語法 | 說明 |
| ------ | --- |
| `if():` | ``()``中條件成立時執行 |
| `elif():` | 上一個`if()`或`elif()`不成立時``()``中條件成立時執行,可無限接續 |
| `else:` | 上一個`if()`或`elif()`不成立時執行 |
---
### 練習 2.2
閏年判斷,輸入1個整數讓程式判斷是否為閏年
閏年定義: 4的倍數是閏年,但100的倍數不是閏年,400的倍數是閏年
輸入:西元年分
輸出:是否為閏年
```python=
year=int(input("yera="))
if(year%400==0):
print('YES')
elif(year%100==0):
print('NO')
elif year%4==0:
print('YES')
else:
print('NO')
```
---
## 串列 `list`
| 語法 | 說明 |
| --------------------------- | ------------------ |
|`x = [<元素1>,<元素2>,...]`| 定義串列 |
| `x[<索引值>]` | 存取元素 |
| `x[<索引值>:<索引值>]` | 取範圍串列 |
---
| 函數 | 說明 |
| ----------------------- | ------------------ |
| `x.append(<元素>)` | 在串列最後新增元素 |
| `x.insert(<索引值>,<元素>)` | 插入元素 |
| `x.remove(<元素>)` | 移除指定元素 |
| `x.pop([<索引值>(選填)])` | 移除指定索引元素 |
---
## 字典 `dict`
| 語法 | 說明 |
| ----------------------------------- | ------------------ |
| `d = {<索引>:<內容物>,...}` | 定義字典 |
| `d[<索引>] = 內容物` | 新增或覆蓋現有索引與值 |
| `d.setdefault(<索引>,<內容物>)` | 不覆蓋新增索引與值 |
|`del d[<索引>]`| 刪除一對鍵值 |
---
## for 迴圈
### range 用法
例
```python=!
for i in range(10):
print (i)
```
印出 0~9
---
### list 用法
例
```python=!
LIST=[1,2,3,4,5,6]
for x in LIST:
print(x)
```
印出 list 中所有的元素
---
### dict 用法
例
```python=!
DICT={'a':10,'b':20,'c':30}
for i in DICT.keys():
print(i,':',DICT[i])
```
```python=!
DICT={'a':10,'b':20,'c':30}
for k,v in DICT.items():
print(k,':',v)
```
印出 dict 中所有的索引跟值
---
### 練習3
輸入 n
輸出 n個"防風林"並且中間由"外還有"隔開
---
例
input 3
output:
防風林
外還有
防風林
外還有
防風林
```python=
n=int(input("n="))
if n<=0:
print('小於一')
for i in range(n-1):
print('防風林')
print('外還有')
print('防風林')
```
---
### 練習4
輸入 n
使用for 迴圈製作一個
包函所有小於n的偶數的一個串列
並輸出
>hint 使用取餘數,if() 與 append
```python=!
n=int(input("n="))
l = []
for i in range(n):
if (i+1)%2 == 0:
l.append(i+1)
print(l)
```
---
## function
例
```python=
def foo():
print('hello')
foo()
```
---
### 參數 與 回傳
例
```python=
def add(a,b):
c=a+b
return c
```
a b 為參數
c 為回傳值
---
### 練習5
把 練習4 製作為 function
輸入改為參數
輸出改為回傳
並且重複執行
---
# 綜合練習
https://zerojudge.tw/Problems
可以去上面找題目做
簡單的練習
https://zerojudge.tw/ShowProblem?problemid=a005
"a005: Eva 的回家作業"
提示:如何分割以空白隔開的輸入
```python=
(a,b,c)=input().split(' ')
a=int(a)
b=int(b)
c=int(c)
print(a,b,c)
```
只要還有輸入就執行
```python=
while(input()):
pass
```
<style>hr{display:none;}</style>