函式即變數

2022-10-09 01:03:09 字數 1357 閱讀 8990

函式即變數:

前向引用:pythn**從上往下執行,遇到函式會再記憶體中為其劃分空間,並將函式作為字串存入,但不會執行,知道函式被呼叫才會執行。所以幾個函式之間並無先後之分,函式a後面寫函式b,函式a也可以呼叫函式b
# 前向引用

def foo():

print(

'from foo')

bar()

def bar():

print(

'櫻桃小丸子')

foo()

遞迴:"問路" 

遞迴特性:

1. 必須有明確的結束條件,return

2. 每次進入更深一層遞迴時,問題規模相對上次遞迴都應有所減少

3. 遞迴效率不高,遞迴層次過多導致棧(記憶體)溢位(再計算機中,函式呼叫時通過棧(atsck)這種資料結構實現的,每當進入乙個函式呼叫,棧就會加一層棧幀,每當函式返回,棧就會減一層棧幀。由於棧的大小不是無限的,所以,遞迴呼叫的次數過多,會導致棧溢位。)

def calc(n):

print(n)

calc(n)

calc(

10)

def calc(n):

print(n)

if int(n/2)==0:

return n

res=calc(int(n/2))==0

return res

calc(10)

person_list=('

張三','

李四','

王五','

蔣六','趙七'

)def ask_way(person_list):

if len(person_list)==0

:

return

'這些人都不知道

'person=person_list.pop(0

)

if person=='張三'

:

return

'樓下超市就有賣吃的,但是沒有生活用品

'print(

'hi,%s 你知道**有賣吃的嗎?

' %person)

print(

'%回答道:我不知道,但是我可以幫你問一下

' %(person,person_list))

time.sleep(1)

print(

'%s問的結果是:%res

' %(person,res))

return

resres=ask_way(person_list)

print(res)

python的高階函式和函式即變數

1 高階函式 高階函式 定義 把函式作為引數作為另乙個函式的引數 deftest a,b return a b deftest 1 f,c return f c print test 1 test 1,2 5 執行結果 10 2 函式即變數 def foo print in the foo bar ...

python3 函式即變數的使用

函式即變數的意思是函式被使用時後面不用 類似變數的使用,具體如下面的示例 def say name print name hi say hi 你好!1 defadd 2print 新增商品 3def view 4print 檢視商品 5def delete 6print 刪除商品 78 choice...

New Pythoner即學即會的常見函式集結

1 abs 求絕對值 print abs 54 542 round m,n 四捨五入 m為需要四捨五入的數,n為保留小數 print round 2.45676583,3 2.457 3 max 求最大值 print max 23 43,75 16,89 3,2 88 894 min 求最小值 pr...