owned this note
owned this note
Published
Linked with GitHub
---
title: 回顧複習 - Python 教學
description: 中興大學資訊研究社1091學期程式分享會主題社課
tags: Python
---
#### meet.google.com/czn-pbav-zvr
### md.nchuit.cc/py/
# 回顧複習
> [name=VJ][time= 109,12,1]
> [name=Hui][time= 110,11,25]
---
## 資料型態與轉型
###### *詳細參閱: [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與身高,而後印出體重
```python=
def reverse_BMI(BMI,H):
return BMI*(H**2)
```
---
## 條件運算
| 語法 | 說明 |
| ------ | --- |
| `if():` | ``()``中條件成立時執行 |
| `elif():` | 上一個`if()`或`elif()`不成立時``()``中條件成立時執行,可無限接續 |
| `else:` | 上一個`if()`或`elif()`不成立時執行 |
---
### 練習 2.2
閏年判斷,輸入1個整數讓程式判斷是否為閏年
閏年定義: 4的倍數是閏年,但100的倍數不是閏年,400的倍數是閏年
輸入:西元年分
輸出:是否為閏年
```python=
def leap_year(year):
if (year%4 == 0 and year%100 != 0 ) or year%400 == 0:
return True
return False
```
---
## 串列 `list`
| 語法 | 說明 |
| --------------------------- | ------------------ |
|`x = [<元素1>,<元素2>,...]`| 定義串列 |
| `x[<索引值>]` | 存取元素 |
| `x[<索引值>:<索引值>]` | 取範圍串列 |
---
| 函數 | 說明 |
| ----------------------- | ------------------ |
| `x.append(<元素>)` | 在串列最後新增元素 |
| `x.insert(<索引>,<元素>)` | 插入元素 |
| `x.remove(<元素>)` | 移除指定元素 |
| `x.pop(<索引值(選填)>)` | 移除指定索引元素 |
---
## 字典 `dict`
| 語法 | 說明 |
| ----------------------------------- | ------------------ |
| `d = {<索引>:<值>,<索引>:<值>,...}` | 定義字典 |
| `d[<索引>] = 值` | 新增或覆蓋現有索引與值 |
| `d.setdefault(<索引>,<值>)` | 不覆蓋新增索引與值 |
---
## for 迴圈
### range 用法
ex:
```python=
for i in range(10):
print(i)
```
印出 0~9
---
### list 用法
ex:
```python=
list=[1,2,3,4,5,6]
for x in list:
print(x)
```
印出 list 中所有的元素
---
### dict 用法
ex:
```python=
dic={'a':10,'b':20,'c':30}
for i in dic.keys():
print(i,':',dic[i])
```
```python=
dict={'a':10,'b':20,'c':30}
for k,v in dict.items():
print(k,':',v)
```
印出 dict 中所有的索引跟值
---
### 練習3
輸入 n
輸出 n個"防風林"並且中間由"外還有"隔開
---
ex:
input 3
output:
防風林
外還有
防風林
外還有
防風林
---
### 練習4
輸入 n
使用for 迴圈製作一個
包函所有小於n的偶數的一個串列
並輸出
>hint: 使用取餘數,if() 與 append
---
## function
ex:
```python=
def foo():
print('hello')
foo()
```
---
### 參數 與 回傳
ex:
```python=
def add(a,b):
c=a+b
return c
```
a b 為參數
c 為回傳值
---
### 練習5
把 練習4 製作為 function
輸入改為參數
輸出改為回傳
並且重複執行
---
# 綜合練習
## leetcode 使用教學
https://leetcode.com/
>leetcode 網址





> 1 runcode 初步測試你的程式是不是對的
> 2 Sumit 繳交程式
## two sum
```python=
class solution:
def twoSum(self,nums: List[int], target: int) -> List[int]: #規定了參數的型別與名稱,方便後續使用
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[j] == target - nums[i]:
return [i, j]
```
## 再試試看這題題
https://leetcode.com/problems/sqrtx/
先不要理這下面這行
> "Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5."
## valid-parentheses
https://leetcode.com/problems/valid-parentheses/
檢查括號是否成對
## climbing-stairs
https://leetcode.com/problems/climbing-stairs/
樓梯有幾種走法,各位高中應該都教過(費氏數列)
## toeplitz-matrix
https://leetcode.com/problems/toeplitz-matrix/
檢查矩陣的數值
## 其他題目
https://hackmd.io/xBIWdqktQPyAQBEUfyKAlw
<style>hr{display:none;}</style>