ncuma_函式.pptx

NCU MCL
NCU MCLSoftware Developer at NCU MCL
函 式
簡要 python 學習講義
函式 (一)
函式:有著某特定功能的獨立程式區塊
 語法
 範例:階乘函式
2
def fn( arg1 , arg2 , ... ) : # fn:函式名稱 arg1, arg2 ...
body # 函式內執行區塊
def factorial( n ) :
"""輸入正整數計算階乘"""
p = 1
for i in range(2,n+1) : p *= i
return p
print( ’函式功能:’ , factorial.__doc__ )
for n in range(1,4) :
print( str(n) + ’! =’ , factorial(n) )
函式 (二)
3
執行函式後輸出:
函式功能:輸入正整數計算階乘
1! = 1
2! = 2
3! = 6
 回傳資料:
使用 return arg 回傳 arg 值後離開函式,一個函式可有多個 return
式子,若無 return 則回傳 None
 若重複定義相同名稱的函式,程式執行最後定義的函式
 函式說明:
緊接在函式名稱之後的字串(若有跨列則需使用三個引號),此字串為函式說明
文字,可使用「函式名稱.__doc__」取得字串,即 factorial.__doc__
函式參數設定 (一):位置對應
 參數位置對應:位置對應
參數使用方式:
4
def power( a = 10 , n = 1 ) :
p = 1
for i in range(n) : p *= a
return p
函式呼叫參數設定 運算結果 說明
power(3,2) 9 a = 3 , n = 2
power(3) 3 a = 3 , n = 1(預設值)
power() 10 a = 10(預設值) , n = 1(預設值)
國立中央大學數學系
函式參數設定 (二)
 預設值的設定順序由末尾往前設定
5
# 錯誤:末尾的 n 沒有設定預設值
def power( a = 10 , n ) :
....
# 正確:預設值由參數列末尾位置往前設定
def power( a , n = 1 ) :
....
國立中央大學數學系
函式參數設定:名稱對應
 參數名稱對應:不管參數設定次序,直接以參數名稱傳遞數值
6
# 計算年齡
def age( byear , cyear = 2017 ) :
return cyear - byear
函式呼叫參數設定 運算結果 說明
age(byear=2000) 17 byear = 2000, cyear = 2017(預設值)
age(cyear=2016,byear=2000) 16 byear = 2000, cyear = 2016
age(cyear=2016) 錯誤 byear 沒有設定
age(2017,byear=2000) 錯誤 byear 有兩個選擇
age(byear=2000,2020) 錯誤 見以下說明
國立中央大學數學系
函式要在使用前定義 (一)
 串列元素 n 次方:無回傳(等同回傳 None)
7
# 更動串列參數數值
def powers( foo , n = 1 ) : # (1) 需先定義函式
for i in range(len(foo)) :
foo[i] = foo[i]**n
return
a = [2,3]
powers(a,4) # (2) 才能使用
print(a) # 輸出 [16, 18]
函式要在使用前定義 (二)
 串列元素 n 次方:有回傳
8
# 沒有變更串列參數數值
def powers( foo , n = 1 ) : # 先定義函式
return [ c**n for c in foo ]
a = [2,3]
b = powers(a) # a = [2, 3] b = [2, 3]
c = powers(a,3) # a = [2, 3] c = [8, 27]
d = powers( foo=a , n=3 ) # a = [2, 3] d = [8, 27]
e = powers( n=3, foo=a ) # a = [2, 3] e = [8, 27]
大型程式的開發方式 (一)
 大型程式的開發方式:倒裝寫法
1. 檔案前端定義主函式 main()
2. 程式執行步驟寫於主函式內
3. 程式其餘函式定義於主函式之後
4. 檔案末尾執行主函式 main()
9
大型程式的開發方式 (二)
10
def main() : # (1) 定義主函式 main()
while True : # (2) 程式步驟寫在主函式內
n = int( input("> ") )
for i in range(1,n+1) :
a = list(range(0,i))
for x in powers(a,2) :
print( x , end=" " )
print()
# 產生 foo 串列元素的 n 次方串列
def powers( foo , n = 1 ) : # (3) 其餘函式定義於主函式之後
return [ c**n for c in foo ]
main() # (4) 最後執行主函式
輸出:
> 3 > 4
1 1
1 4 1 4
1 4 9 1 4 9
1 4 9 16
1 of 10

Recommended

Ppt 120-126 by
Ppt 120-126Ppt 120-126
Ppt 120-126hungchiayang1
4.9K views7 slides
Ppt 120-126 by
Ppt 120-126Ppt 120-126
Ppt 120-126hungchiayang1
274 views7 slides
Ch9 by
Ch9Ch9
Ch9Alisha Smile
131 views77 slides
Ch9 教學 by
Ch9 教學Ch9 教學
Ch9 教學hungchiayang1
467 views32 slides
Ch 8 by
Ch 8Ch 8
Ch 8BMG2011
749 views75 slides
Ch5 教學 by
Ch5 教學Ch5 教學
Ch5 教學hungchiayang1
1.1K views50 slides

More Related Content

Similar to ncuma_函式.pptx

第4章函数 by
第4章函数第4章函数
第4章函数summerfeng
294 views53 slides
Appendix B 教學 by
Appendix B 教學Appendix B 教學
Appendix B 教學hungchiayang1
315 views9 slides
Ch5 by
Ch5Ch5
Ch5Alisha Smile
85 views81 slides
Ptyhon 教學 003 函數 by
Ptyhon 教學 003 函數Ptyhon 教學 003 函數
Ptyhon 教學 003 函數信宏 陳
751 views14 slides
Ppt 138-142 by
Ppt 138-142Ppt 138-142
Ppt 138-142hungchiayang1
8.4K views5 slides
Ppt 136-140 by
Ppt 136-140Ppt 136-140
Ppt 136-140hungchiayang1
270 views5 slides

Similar to ncuma_函式.pptx(20)

第4章函数 by summerfeng
第4章函数第4章函数
第4章函数
summerfeng294 views
Ptyhon 教學 003 函數 by 信宏 陳
Ptyhon 教學 003 函數Ptyhon 教學 003 函數
Ptyhon 教學 003 函數
信宏 陳751 views
函數微分_範例.pptx by mclmath
函數微分_範例.pptx函數微分_範例.pptx
函數微分_範例.pptx
mclmath40 views
ncuma_函數微分計算.pptx by NCU MCL
ncuma_函數微分計算.pptxncuma_函數微分計算.pptx
ncuma_函數微分計算.pptx
NCU MCL2.3K views
lambda/closure – JavaScript、Python、Scala 到 Java SE 7 by Justin Lin
lambda/closure – JavaScript、Python、Scala 到 Java SE 7lambda/closure – JavaScript、Python、Scala 到 Java SE 7
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
Justin Lin2K views
竞赛中C++语言拾遗 by 乐群 陈
竞赛中C++语言拾遗竞赛中C++语言拾遗
竞赛中C++语言拾遗
乐群 陈1.3K views
C程式-函式與巨集 by 艾鍗科技
C程式-函式與巨集C程式-函式與巨集
C程式-函式與巨集
艾鍗科技42.6K views
ncuma_Taylor 多項式.pptx by NCU MCL
ncuma_Taylor 多項式.pptxncuma_Taylor 多項式.pptx
ncuma_Taylor 多項式.pptx
NCU MCL36 views
C語言 第4章 基本輸出與輸入功能 by shademoon
C語言 第4章 基本輸出與輸入功能C語言 第4章 基本輸出與輸入功能
C語言 第4章 基本輸出與輸入功能
shademoon8.3K views
实验一 Mathematica软件简介 by Xin Zheng
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介
Xin Zheng1.7K views
实验一 Mathematica软件简介 by guestfe33f0e
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介
guestfe33f0e1.2K views
Python入門:5大概念初心者必備 2021/11/18 by Derek Lee
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18
Derek Lee227 views

More from NCU MCL

函數畫圖_習題4.pptx by
函數畫圖_習題4.pptx函數畫圖_習題4.pptx
函數畫圖_習題4.pptxNCU MCL
402 views1 slide
數值積分法_3.pptx by
數值積分法_3.pptx數值積分法_3.pptx
數值積分法_3.pptxNCU MCL
1.2K views1 slide
數值積分法_2.pptx by
數值積分法_2.pptx數值積分法_2.pptx
數值積分法_2.pptxNCU MCL
64 views1 slide
數值積分法_1.pptx by
數值積分法_1.pptx數值積分法_1.pptx
數值積分法_1.pptxNCU MCL
74 views1 slide
數值求根習題_1.pptx by
數值求根習題_1.pptx數值求根習題_1.pptx
數值求根習題_1.pptxNCU MCL
89 views1 slide
函數微分習題_3.pptx by
函數微分習題_3.pptx函數微分習題_3.pptx
函數微分習題_3.pptxNCU MCL
539 views3 slides

More from NCU MCL(20)

函數畫圖_習題4.pptx by NCU MCL
函數畫圖_習題4.pptx函數畫圖_習題4.pptx
函數畫圖_習題4.pptx
NCU MCL402 views
數值積分法_3.pptx by NCU MCL
數值積分法_3.pptx數值積分法_3.pptx
數值積分法_3.pptx
NCU MCL1.2K views
數值積分法_2.pptx by NCU MCL
數值積分法_2.pptx數值積分法_2.pptx
數值積分法_2.pptx
NCU MCL64 views
數值積分法_1.pptx by NCU MCL
數值積分法_1.pptx數值積分法_1.pptx
數值積分法_1.pptx
NCU MCL74 views
數值求根習題_1.pptx by NCU MCL
數值求根習題_1.pptx數值求根習題_1.pptx
數值求根習題_1.pptx
NCU MCL89 views
函數微分習題_3.pptx by NCU MCL
函數微分習題_3.pptx函數微分習題_3.pptx
函數微分習題_3.pptx
NCU MCL539 views
SymPy 在微積分上的應用_3.pptx by NCU MCL
SymPy 在微積分上的應用_3.pptxSymPy 在微積分上的應用_3.pptx
SymPy 在微積分上的應用_3.pptx
NCU MCL28 views
SymPy 在微積分上的應用_2.pptx by NCU MCL
SymPy 在微積分上的應用_2.pptxSymPy 在微積分上的應用_2.pptx
SymPy 在微積分上的應用_2.pptx
NCU MCL27 views
SymPy 在微積分上的應用_1.pptx by NCU MCL
SymPy 在微積分上的應用_1.pptxSymPy 在微積分上的應用_1.pptx
SymPy 在微積分上的應用_1.pptx
NCU MCL32 views
極座標畫圖_3.pptx by NCU MCL
極座標畫圖_3.pptx極座標畫圖_3.pptx
極座標畫圖_3.pptx
NCU MCL12 views
極座標畫圖_2.pptx by NCU MCL
極座標畫圖_2.pptx極座標畫圖_2.pptx
極座標畫圖_2.pptx
NCU MCL15 views
極座標畫圖_1.pptx by NCU MCL
極座標畫圖_1.pptx極座標畫圖_1.pptx
極座標畫圖_1.pptx
NCU MCL12 views
Taylor 多項式_3.pptx by NCU MCL
Taylor 多項式_3.pptxTaylor 多項式_3.pptx
Taylor 多項式_3.pptx
NCU MCL8 views
Taylor 多項式_2.pptx by NCU MCL
Taylor 多項式_2.pptxTaylor 多項式_2.pptx
Taylor 多項式_2.pptx
NCU MCL11 views
Taylor 多項式_1.pptx by NCU MCL
Taylor 多項式_1.pptxTaylor 多項式_1.pptx
Taylor 多項式_1.pptx
NCU MCL9 views
微分方程式求解_3.pptx by NCU MCL
微分方程式求解_3.pptx微分方程式求解_3.pptx
微分方程式求解_3.pptx
NCU MCL53 views
微分方程式求解_2.pptx by NCU MCL
微分方程式求解_2.pptx微分方程式求解_2.pptx
微分方程式求解_2.pptx
NCU MCL30 views
微分方程式求解_1.pptx by NCU MCL
微分方程式求解_1.pptx微分方程式求解_1.pptx
微分方程式求解_1.pptx
NCU MCL28 views
牛頓迭代法_3.pptx by NCU MCL
牛頓迭代法_3.pptx牛頓迭代法_3.pptx
牛頓迭代法_3.pptx
NCU MCL18 views
牛頓迭代法_2.pptx by NCU MCL
牛頓迭代法_2.pptx牛頓迭代法_2.pptx
牛頓迭代法_2.pptx
NCU MCL17 views

ncuma_函式.pptx