ncuma_函式.pptx

NCU MCL
NCU MCLSoftware Developer à 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 sur 10

Recommandé

Ppt 120-126 par
Ppt 120-126Ppt 120-126
Ppt 120-126hungchiayang1
4.9K vues7 diapositives
Ppt 120-126 par
Ppt 120-126Ppt 120-126
Ppt 120-126hungchiayang1
274 vues7 diapositives
Ch9 par
Ch9Ch9
Ch9Alisha Smile
130 vues77 diapositives
Ch9 教學 par
Ch9 教學Ch9 教學
Ch9 教學hungchiayang1
467 vues32 diapositives
Ch 8 par
Ch 8Ch 8
Ch 8BMG2011
747 vues75 diapositives
Ch5 教學 par
Ch5 教學Ch5 教學
Ch5 教學hungchiayang1
1.1K vues50 diapositives

Contenu connexe

Similaire à ncuma_函式.pptx

第4章函数 par
第4章函数第4章函数
第4章函数summerfeng
294 vues53 diapositives
Appendix B 教學 par
Appendix B 教學Appendix B 教學
Appendix B 教學hungchiayang1
315 vues9 diapositives
Ch5 par
Ch5Ch5
Ch5Alisha Smile
85 vues81 diapositives
Ptyhon 教學 003 函數 par
Ptyhon 教學 003 函數Ptyhon 教學 003 函數
Ptyhon 教學 003 函數信宏 陳
751 vues14 diapositives
Ppt 138-142 par
Ppt 138-142Ppt 138-142
Ppt 138-142hungchiayang1
8.4K vues5 diapositives
Ppt 136-140 par
Ppt 136-140Ppt 136-140
Ppt 136-140hungchiayang1
270 vues5 diapositives

Similaire à ncuma_函式.pptx(20)

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

Plus de NCU MCL

函數畫圖_習題4.pptx par
函數畫圖_習題4.pptx函數畫圖_習題4.pptx
函數畫圖_習題4.pptxNCU MCL
229 vues1 diapositive
數值積分法_3.pptx par
數值積分法_3.pptx數值積分法_3.pptx
數值積分法_3.pptxNCU MCL
679 vues1 diapositive
數值積分法_2.pptx par
數值積分法_2.pptx數值積分法_2.pptx
數值積分法_2.pptxNCU MCL
40 vues1 diapositive
數值積分法_1.pptx par
數值積分法_1.pptx數值積分法_1.pptx
數值積分法_1.pptxNCU MCL
46 vues1 diapositive
數值求根習題_1.pptx par
數值求根習題_1.pptx數值求根習題_1.pptx
數值求根習題_1.pptxNCU MCL
82 vues1 diapositive
函數微分習題_3.pptx par
函數微分習題_3.pptx函數微分習題_3.pptx
函數微分習題_3.pptxNCU MCL
526 vues3 diapositives

Plus de NCU MCL(20)

函數畫圖_習題4.pptx par NCU MCL
函數畫圖_習題4.pptx函數畫圖_習題4.pptx
函數畫圖_習題4.pptx
NCU MCL229 vues
數值積分法_3.pptx par NCU MCL
數值積分法_3.pptx數值積分法_3.pptx
數值積分法_3.pptx
NCU MCL679 vues
數值積分法_2.pptx par NCU MCL
數值積分法_2.pptx數值積分法_2.pptx
數值積分法_2.pptx
NCU MCL40 vues
數值積分法_1.pptx par NCU MCL
數值積分法_1.pptx數值積分法_1.pptx
數值積分法_1.pptx
NCU MCL46 vues
數值求根習題_1.pptx par NCU MCL
數值求根習題_1.pptx數值求根習題_1.pptx
數值求根習題_1.pptx
NCU MCL82 vues
函數微分習題_3.pptx par NCU MCL
函數微分習題_3.pptx函數微分習題_3.pptx
函數微分習題_3.pptx
NCU MCL526 vues
SymPy 在微積分上的應用_3.pptx par NCU MCL
SymPy 在微積分上的應用_3.pptxSymPy 在微積分上的應用_3.pptx
SymPy 在微積分上的應用_3.pptx
NCU MCL27 vues
SymPy 在微積分上的應用_2.pptx par NCU MCL
SymPy 在微積分上的應用_2.pptxSymPy 在微積分上的應用_2.pptx
SymPy 在微積分上的應用_2.pptx
NCU MCL26 vues
SymPy 在微積分上的應用_1.pptx par NCU MCL
SymPy 在微積分上的應用_1.pptxSymPy 在微積分上的應用_1.pptx
SymPy 在微積分上的應用_1.pptx
NCU MCL30 vues
極座標畫圖_3.pptx par NCU MCL
極座標畫圖_3.pptx極座標畫圖_3.pptx
極座標畫圖_3.pptx
NCU MCL12 vues
極座標畫圖_2.pptx par NCU MCL
極座標畫圖_2.pptx極座標畫圖_2.pptx
極座標畫圖_2.pptx
NCU MCL15 vues
極座標畫圖_1.pptx par NCU MCL
極座標畫圖_1.pptx極座標畫圖_1.pptx
極座標畫圖_1.pptx
NCU MCL12 vues
Taylor 多項式_3.pptx par NCU MCL
Taylor 多項式_3.pptxTaylor 多項式_3.pptx
Taylor 多項式_3.pptx
NCU MCL7 vues
Taylor 多項式_2.pptx par NCU MCL
Taylor 多項式_2.pptxTaylor 多項式_2.pptx
Taylor 多項式_2.pptx
NCU MCL10 vues
Taylor 多項式_1.pptx par NCU MCL
Taylor 多項式_1.pptxTaylor 多項式_1.pptx
Taylor 多項式_1.pptx
NCU MCL8 vues
微分方程式求解_3.pptx par NCU MCL
微分方程式求解_3.pptx微分方程式求解_3.pptx
微分方程式求解_3.pptx
NCU MCL38 vues
微分方程式求解_2.pptx par NCU MCL
微分方程式求解_2.pptx微分方程式求解_2.pptx
微分方程式求解_2.pptx
NCU MCL28 vues
微分方程式求解_1.pptx par NCU MCL
微分方程式求解_1.pptx微分方程式求解_1.pptx
微分方程式求解_1.pptx
NCU MCL22 vues
牛頓迭代法_3.pptx par NCU MCL
牛頓迭代法_3.pptx牛頓迭代法_3.pptx
牛頓迭代法_3.pptx
NCU MCL18 vues
牛頓迭代法_2.pptx par NCU MCL
牛頓迭代法_2.pptx牛頓迭代法_2.pptx
牛頓迭代法_2.pptx
NCU MCL16 vues

ncuma_函式.pptx