繼續完成python之道
講者:楊斯丞
時間:12/29
地點:LE5A
大綱
- What's Function?
- Why Function?
- How Function?
What's Function?

Why Function?
-
程式的重複利用性 -
程式的易讀性 -
程式的易除錯性 -
程式的一致性 -
程式的模組化
How Function?
def test(x):
"""This is test function"""
x += 1
return xdef 是定義函數的關鍵字
test 是函數名稱
(): 代表裡面可以放參數,並且準備開始寫子代碼
"""This is test function""" 是註解或是說明功能
x += 1 代碼區或是程序處理邏輯
return x 定義返回值牛刀小試-1
1. 公元年分除以4不可整除,為平年。 2. 公元年分除以4可整除但除以100不可整除,為閏年。 3. 公元年分除以100可整除但除以400不可整除,為平年。
def check_leap_year(year):牛刀小試-1
1. 公元年分除以4不可整除,為平年。 2. 公元年分除以4可整除但除以100不可整除,為閏年。 3. 公元年分除以100可整除但除以400不可整除,為平年。
def check_leap_year(year):
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
print("閏年")
else:
print("平年")區域變數v.s.全域變數
預設引數
def sum(a, b, c = 0):
return a + b + c
print(sum(10, 20, 30)) # 顯示 60
print(sum(10, 20)) # 顯示 30關鍵字引數
def sum(a, b, c = 0):
return a + b + c
print(sum(c = 30, a = 10, b = 20)) # 顯示 60不訂長度引數
def sum(*numbers):
total = 0
for number in numbers:
total += number
return total
print(sum(1, 2)) # 顯示 3
print(sum(1, 2, 3)) # 顯示 6
print(sum(1, 2, 3, 4)) # 顯示 10Python的套件管理
還有一點點的虛擬環境
pip install <套件名稱>pip uninstall <套件名稱>安裝
解除安裝
python II
By Lamuyang
python II
- 56