用書 PYTHON王者歸來 作者洪錦魁
遞回式函數 recursive
函數呼叫自己的意思,每次呼叫自己的時候,範圍都會越來越小,且必須也有個可以終止的條件來結束遞迴函數。
最常見的應用是處理正整數的階層。
n!=1*2*3
def math1(n):
if n== 1:
return 1
else:
return n*math1(n-1)
A =1
print("A =1",math1(A))
A=5
print("A =5",math1(A))
執行結果
預設最大遞迴次數是1000次
sys.setrecursuinlimit()可設定最大遞迴次數
import sys
print(sys.getrecursionlimit())
執行結果
區域變數和全域變數
影響範圍在限定函數 為 區域變數
影響範圍在整個程式 為 全域變數
def func():
print("Gobal Variabl",msg) #Gobal Variabl
msg = 100
print("main program",msg)
func()
執行結果
def func():
msg = 50 #local Variabl
print("local Variabl",msg) #ocal Variabl
msg = 100 #Gobal Variabl
print("main program",msg)
func()
執行結果
注意事項
區域變數內容無法在其他函數引用
區域變數內容無法在主程式印用
在函數內不能更改全域變數的值
若要在函數內存取或修改全域變數值,需在函數內使用global宣告變數
nonlocal變數
和global 相同
global 是指最上層變數,nonlocal指的是上一層變數
def func():
nonlocal_var = 50
def inner_func():
global global_var
nonlocal nonlocal_var
global_var =100
nonlocal_var= 25
inner_func()
print("global_var:",global_var) #global_var: 1000
print("nonlocal_var",nonlocal_var) #nonlocal_var 2525
global_var = 1000
nonlocal_var = 2525
print("global_var:",global_var) # global_var: 100
print("nonlocal_var",nonlocal_var) # nonlocal_var 2525
func() #global_var: 100 nonlocal_var 25
print("global_var:",global_var) #lobal_var: 100
print("nonlocal_var",nonlocal_var) #nonlocal_var 2525
執行結果
匿名函數lambda
可以有許多參數,但是只能有一個程式碼表達式
#1
def math(x,y,z):
value = (x+y)*z/2
return value
print(math(2,3,4))
#2
math = lambda x,y,z: (x+y)*z/2
print(math(2,3,4))
執行結果
使用LAMBDA匿名函數的理由是存在一個函數的內部
def math(b):
return lambda x: 2*x+ b # 2x+b
value1 = math(3) #b
print(value1(10)) #x
value2 = math(1) #b
print(value2(6)) #x
執行結果
def classA(classmates,func):
def classA(classmates,func):
for classmate in classmates:
print(func(classmate))
def pr_name(name):
return "My name is " + name.title()
classmate_name =["nini","lucy","penny"]
classA(classmate_name,pr_name)
"..............................................."
def classB(classmates,func):
for classmate in classmates:
print(func(classmate))
classmate_namB =["bibi","pucy","kenny"]
classB(classmate_namB, lambda name:"My name is "+ name.title())
執行結果
def math1(x):
return x if (x % 3 == 0) else None
list1 = [1,2,3,4,5,6,7,8,9,10,11,12]
filter_list = filter(math1,list1)
filter_list1 = list(filter(math1,list1))
print("三的倍數:",filter_list1)
print("三的倍數:",filter_list)
print("三的倍數:",[item for item in filter_list] )
filter_list2 = list(filter(lambda x:(x % 3 == 0),list1))
print("三的倍數:",filter_list2)
執行結果
map()
list1 = [1,2,3,4,5,6,7,8,9,10,11,12]
squarelist = list(map(lambda x :x*5,list1))
print(squarelist)
執行結果
reduce()
from functools import reduce
def string_to_int(string):
def func(x,y):
return 10*x+y
def char_to_number(string):
print("string= ",type(string),string)
dic1 = {"1":1,"2":2,"3":3,"4":4,"5":5}
num = dic1[string]
print("number= ",type(num),num)
return num
return reduce(func,map(char_to_number,string))
string1 ="1234"
x = string_to_int(string1) + 10
print("x= ",x)
def string_to_int(string):
def char_to_number(string):
return {"1":1,"2":2,"3":3,"4":4,"5":5}[string]
return reduce(lambda x,y:10*x+y ,map(char_to_number,string))
string1 ="1234"
x = string_to_int(string1) + 10
print("x= ",x)
pass 將未完成函數放在PASS上
range()
n = start
while n< stop:
yield n
n+=step
print(type(myrange))
for x in myrange(1,9):
print(x)
執行結果