# 2023-04-22 Python入門
###### tags: `python`
## 函式
#### 比大小
```python=
a1 = 3
a2 = 4
b1 = 5
b2 = 6
c1 = 7
c2 = 8
# if a1 < a2:
# print(a1)
# elif a2 < a1:
# print(a2)
# else:
# print('a1 = a2')
# if b1 < b2:
# print(b1)
# elif b2 < b1:
# print(b2)
# else:
# print('b1 = b2')
# if c1 < c2:
# print(c1)
# elif c2 < c1:
# print(c2)
# else:
# print('c1 = c2')
# 函式定義,如果沒有呼叫,不會被執行
def compare(a, b):
if(a < b):
print(a)
elif(b < a):
print(b)
else:
print(f'一樣大,都是{a}')
compare(a1, a2)
compare(b1, b2)
compare(c1, c2)
```
#### 寫一函式sum, 傳入一個list加總後回傳
```python=
def sum(data): # [4, 5, 6]
total = 0
for n in data:
total += n # total = total + n
return total # 把total內的資料回傳給呼叫端
a = [1 , 2 ,3]
b = [3, 4, 5, 6]
a_data = sum(a) # 呼叫sum函式,且傳入a變數,產生的資料存放到a_data變數
b_data = sum(b)
print(f'a list總合為: {a_data}')
print(f'b list總合為: {b_data}')
```
#### 關鍵字引數
```
def show(a, b, c):
print(f'a={a}')
print(f'b={b}')
print(f'c={c}')
show(1, 2, c = 3) # 關鍵字引數
show('aaron', 'andy', 'apple')
```
#### 參數預設值
```python=
def showMe(name, age, job = '無業'): # job改為參數預設值
print(f'我是{name}, 今年{age}歲, 工作是{job}')
showMe(job='程式設計', name='aaron', age=18)
showMe('aaron', 18, '程式設計')
showMe('aaron', 18)
print('1', '2', '3', '4', '5', sep='', end='')
print('6', '7', '8', '9', '0', sep='')
```
#### 多個涵式呼叫
```python=
def func1(a, b):
return a + b
def func2(a, b):
return a * b
def func3(a, b):
return a - b
print( func1( func2(1, 2) , func3(2, 1) ) )
# 2 1
```
> **補充:**
> 如果同一行程式碼有多個函式呼叫,呼叫順序為由內到外,有左到右
#### 一級函式
```python=
def show1(a):
print(f'我是{a}')
def show2(a):
print(f'今年{a}歲')
def show3(a):
print(f'工作是{a}')
def show(a, b, c):
a('aaron')
b(18)
c('Develper')
show(show1, show2, show3)
```
#### lambda
```python=
# def max(a1, a2):
# if a1 > a2:
# return a1
# else:
# return a2
# return a1 if a1 > a2 else a2
max = lambda a1, a2: a1 if a1 > a2 else a2
print(max(7, 9))
```
#### lambda函式呼叫
```
print((lambda a, b, c: a + b * c)('1', '2', 3))
```
## 下載台灣銀行匯率資料
```python=
# WebDriver下載網址
# https://chromedriver.chromium.org/downloads
from selenium import webdriver
# 設定定位條件用的模組
from selenium.webdriver.common.by import By
# import定位不到元素時的例外
from selenium.common.exceptions import NoSuchElementException
import time
# 初始化webdriver
driver = webdriver.Chrome('chromedriver')
# 設定網頁載入時間(10秒)
driver.implicitly_wait(10)
# 連線到台灣銀行匯率網頁
driver.get('https://rate.bot.com.tw/xrt?Lang=zh-TW')
# 印出網頁標題
print(driver.title)
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(3)
try:
# 定位元素(下載 Excel (CSV) 檔的超連結標籤), 如果有定位到, download_csv變數就是該元素物件
download_csv = driver.find_element(By.LINK_TEXT, '下載 Excel (CSV) 檔')
# 印出定位道的超連結標前的href屬性質
print('超連結為:', download_csv.get_attribute('href'))
# 點擊超連結
download_csv.click()
# 等五秒,讓下載完成
time.sleep(5)
except NoSuchElementException:
print('定位不到下載CSV按鈕')
# 關閉webdriver釋放記憶體
driver.quit()
```
> **參考講義:**
> https://hackmd.io/@aaronlife/python-topic-selenium
#### 函式小細節
```python=
sum = 3
print(sum)
# del sum
a = [1, 2, 3, 4, 5]
# 偵測變數是否為函式
print('sum=', callable(sum))
b = sum(a)
print(b)
```
## 模組
###### rock.py
```python=
import random
def rock_paper_scissors():
pc = random.randint(0, 2)
pc_str = ['剪刀', '石頭', '布'][pc]
print(f'電腦:{pc_str}')
player = int(input('請猜拳(0=剪刀, 1=石頭, 2=布):'))
if (pc == 0 and player == 1) or (pc == 1 and player == 2) or (pc == 2 and player == 0):
print('你贏了')
elif (pc == 0 and player == 2) or (pc == 1 and player == 0) or (pc == 2 and player == 1):
print('你輸了')
else:
print('平手')
a = 3
```
###### game.py
```python=
from rock import rock_paper_scissors
from rock import a
rock_paper_scissors()
print(a)
```
## 物件
```python=
# 定義Car類別
class Car:
# 方法
def run(self):
print('開車')
# 方法
def fill(self, gas):
print('加油')
self.gas = gas
car = Car() # 透過Car建立Car物件並存放到car變數
# 呼叫car物件的方法
car.run()
car.fill(23)
#建立第二個Car物件
car1 = Car() # 透過Car類別來建立物件並存放到car1變數
car1.fill(99)
# car和car1這兩個式完全獨立的互不干擾
print(car.gas)
print(car1.gas)
```