python 函式式程式設計和引數

2021-08-22 05:01:14 字數 1531 閱讀 1234

#return的三種情況,如果不寫,返回none,如果寫乙個就返回寫的那個,如果多個,就放在乙個元組裡返回,還有如果返回是要給函式沒有括號,就返回這個函式。如果有括號,就返回這個函式執行後的結果,然後在返回返回值。

def test1():

print("hello")

def test2():

print("hello")

return test1

def test3():

print("hello")

return 1,"hello",["h","e","l","l","o"],(1,2,3,),

def test4():

print("hello")

return test1

def test5():

print("hello")

return test1()

print("1",test1())

print("2",test2())

print("3",test3())

print("4",test4())

print("5",test5())

def test6(x,y):

print(x)

print(y)

test6(y=9,x=8)#關鍵字引數呼叫

test6(8,9)#位置引數呼叫

#test6(x=8,9)#注意著會報錯,關鍵字引數不能寫在位置引數前面。因為執行的時候首先要看到位置引數,比如把8傳進去,然後才能考慮其他引數情況(注意必須把位置位置引數連續傳完,才能傳其他形式引數)

test6(8,y=9)

def test7(x,y=2):#預設引數

print(x,y)

test7(2)

def test8(*args):#以*開始後面args可以隨意寫,表示可以接受多個形參作為輸入,結果把這些返回值放在乙個元組裡面

print(args)

test8(1,2,3,4,5,6)

test8(*[1,2,3,4,5])#[1,2,3,4,5]=args

def test9(*args,**kwargs):#**args把n個關鍵字引數轉換成字典的方式,位置引數不能寫在關鍵字引數之後

print(kwargs)#如果是return,就是把kwargs放在字典裡返回

print(args)

test9(9,name="gongwei",age=24,***="m")

test9(**)

**運**況:

hello

1 none

hello

2 hello

3 (1, 'hello', ['h', 'e', 'l', 'l', 'o'], (1, 2, 3), )

hello

4 hello

hello

5 none89

8989

2 2(1, 2, 3, 4, 5, 6)

(1, 2, 3, 4, 5)

(9,)

()

012 python函式式程式設計 函式引數

python中的函式引數很靈活,具體體現在傳遞引數有多種形式。為了提高函式呼叫的可讀性,在函式呼叫時可以使用關鍵字引數呼叫 usr bin python coding utf 8 descrition create funcation def print area width,height area...

python中函式和函式式程式設計

def funx x,y,z print x,y,z funx 1,hello true 位置引數 funx z he y is x boy 關鍵字引數執行結果 f untitled2 venv scripts python.exe f untitled2 chinese demo1.py 1 he...

函式式程式設計與引數

函式 def定義,有返回值 過程 def定義,沒有返回值 函式式程式設計是 抽象程度很高的程式設計正規化,純粹的函式式程式語言編寫的函式沒有變數。所以,任意函式,只要輸入是確定的,輸出就是確定的,就像數學裡的函式,變數和因變數的關係是明確的。python對函式式程式設計提供部分支援,由於python...