Python函式學習

2021-10-04 04:24:36 字數 1880 閱讀 7642

def

hello

(name)

:return

'hello, '

+ name +

'!'print

(hello(

'holly'))

defhello

(name)

:print

('hello,'

,name)

hello(

'holly!'

)

輸出結果為hello,holly!

稍微複雜一點的例子有

求長方體的體積

def

volume

(length,width,height)

:return length * width * height

l =2

w =3

h =5

print

('length='

,l,'width='

,w,'height='

,h,'volume='

,volume(l,w,h)

)

pythoncode python3 holly_03_19.py

length=

2 width=

3 height=

5 volume=

30

呼叫函式。只有呼叫函式,才能讓函式執行。巢狀關係,一步步執行

def

reduce

(arg1, agr2)

:print

("123"

)return agr2-arg1

defhello()

:print

("hello"

)def

sum( arg1, arg2 )

:# 返回2個引數的和."

total = arg1 + arg2 #30

print

("函式內 : "

, total)

mc = multiply(arg1,arg2)

# 2,這裡呼叫multiply函式

return total + mc

defmultiply

(arg1, arg2)

:print

("hhhh"

)

hello(

)#200

return arg1*arg2 +

reduce

(arg1,arg2)

# 呼叫sum函式

total =

sum(10,

20)# 1,先呼叫sum函式,找到對應的函式,沒有呼叫的暫時不返回值

print

("函式外 : "

, total)

➜  pythoncode python3 holly_03_19.py

函式內 :

30hhhh

hello

123函式外 :

240

return後面沒有指定值,返回為none

def

test()

:print

('this is a dog'

)return

print

('this is a cat'

)x = test(

)print

(x)

➜  pythoncode python3 holly_03_19.py

this is a dog

none

python 函式學習

今兒再網上看了下別人總結的python 函式,功能挺強大的,c有的功能都有,下面就記些它的功能點 1 定義,格式跟c不一樣,概念是一樣的。def 函式名 引數列表 函式語句 return 返回值 2 函式可以命別名,很方便啊,c語言我記憶中只有指標可以。def sum list result 0 f...

Python函式學習

1 def 語句和引數 def hello name print hello name hello alice hello bob 如果執行這個程式,輸出看起來像這樣 hello alice hello bob 在這個程式的 hello 函式定義中,有乙個名為 name 的變元 變元 是乙個 變數,...

Python函式學習

1.簡化 2,呼叫方便,修改方便 3.呼叫引數,形引數,與位置引數。關鍵引數,位置引數只能發在關鍵引數之後 4.預設引數 5.引數組 args 元組引數 6 接受字典 kwargs 當同時使用時必須放到引數的最後 程式執行的從檔案的上邊到下邊的執行 乙個變數只在函式中生效。外部訪問不到 在檔案頂層宣...