python怎么调用函数的返回值类型,10、Python基础之函数的调用与返回值
一、函數(shù)參數(shù)
1.1 不定長參數(shù)
當我們定義函數(shù)時,可能需要定義一個函數(shù)能處理比當初聲明時更多的參數(shù),這些參數(shù)叫做不定長參數(shù)。
我們可以在形參前面加上一個 * ,這樣這個形參就可以獲取所有的實參,它將所有的實參保存到一個元組中。
舉例說明:
(1) 假如實參大于形參時,函數(shù)調(diào)用報錯。
def fun(a, b):
print(a + b)
fun(1,2,3)
------------------
fun() takes 2 positional arguments but 3 were given
(2) 不定長參數(shù)使用:
def fun(*a): # 不定長參數(shù)
print(a)
fun(1, 2, 3, 4)
----------------
(1, 2, 3, 4)
不定長參數(shù)特性:
(1) 位置傳參的不定長參數(shù)
只能存在一個一個參數(shù)有 * 號,可以和其他的傳參方式一起配合使用。但是位置參數(shù)不能放在不定長參數(shù)后面。
def fun1(*a, b):
print(a)
r = 0
for i in a:
r += i
print(r)
fun1(1, 3, 5, 7)
----------------
執(zhí)行錯誤:TypeError: fun1() missing 1 required keyword-only argument: 'b'
def fun1(b, *a):
print(a)
r = 0
for i in a:
r += i
print(r)
fun1(1, 3, 5, 7)
-----------------
(3, 5, 7)
15
不定長參數(shù)要放在關鍵字傳參前面
def fun1(c, *a, d):
print(a)
r = 0
for i in a:
r += i
print(r)
fun1(1, 3, 5, d=9)
-------------------
(3, 5)
8
(2) 關鍵字傳參的不定長參數(shù)
2個 * 號表示關鍵字傳參的不定長參數(shù)。
def fun1(**e):
print(e)
fun1(a=1, b=2, c=3, d=4)
-----------------
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
特性:
位置傳參必須放到不定長參數(shù)的前面,關鍵字傳參必須要放到關鍵字傳參的不定長參數(shù)之前:
格式:
def fun1(*g, h **e):
print(e)
print(g)
fun1(1, 2, a=1, b=2, c=3, d=4, h=1)
# h 負責接收關鍵字傳參
# *g 負責接收位置傳參
# **e 負責接收關鍵字傳參的不定長參數(shù)
------------------
{'a': 1, 'b': 3, 'c': 5, 'd': 7}
1
(1, 2)
位置傳參的不定長參數(shù) 與 關鍵字傳參的不定長參數(shù)混合使用時,也叫萬能參數(shù)。
1.2 參數(shù)的拆包
傳遞參數(shù)時,也可以在序列類型的參數(shù)前添加 "*" 號。這樣它將會自動的將序列中元素依次作為參數(shù)傳遞。
要求需要中的元素的個數(shù)必須和形參的個數(shù)一致。
用,是不行的。
def fun(a, b, c):
print(a)
print(b)
print(c)
tuple1 = (1, 3, 5)
fun(tuple1[0], tuple1[1], tuple1[2])
要傳遞元組中的數(shù)據(jù)時,上面的方式比較麻煩,那么有沒有更簡單的方式呢 ?
def fun(a, b, c):
print(a)
print(b)
print(c)
tuple1 = (1, 3, 5)
fun(*tuple1)
----------------------
1
3
5
** 針對不定長參數(shù),假如時一個字典時 ,參數(shù)拆包怎么使用呢 ?
dict1 = {'a': 1, 'b': 3, 'c': 5, 'd': 7}
def fun2(**dict1)
二、函數(shù)返回值 return
2.1 函數(shù)的返回值 《重點》
函數(shù)內(nèi)的變量或者執(zhí)行結果,想要直接到函數(shù)外面去使用,這樣直接時無法使用的。因此,我們就需要把函數(shù)的的變量或者結果,放到外面去使用,這樣就要借助于 return。
def fun(*args):
r = 0
for i in args:
r += i
print(r)
fun(1, 2, 3)
# print(r)
-----------------
print(r) 在函數(shù)內(nèi)定義的,函數(shù)外時無法使用的。
函數(shù)的返回值 return
return r 只能返回 r 的值,而不能返回指定r 的名稱。
def fun(*args):
r = 0
for i in args:
r += i
print(r)
return r # 返回了r 的值,但是外面不能使用r的名稱。
fun(333, 444, 555, 666)
---------------------
1998
1998
return 返回值,可以定義任何數(shù)據(jù)類型:
return [1, 2, 3]
return True
return 1
return {"a": 1, "b": 2, "c": 3}
return 后面不接數(shù)據(jù)時,返回 None。
def fun3(*args):
r = 0
for i in args:
r += i
#print(r)
# return
def fun4():
pass
return fun4
res = fun3(11, 22, 33, 55)
print(res)
------------------------
.fun4 at 0x000001A4EDEF1C80>
函數(shù)沒有定義 return 時,返回的也是None。
def fun3(*args):
r = 0
for i in args:
r += i
res1 = fun3(11, 22, 33, 55)
print(res1)
------------------------
None
2.2 函數(shù)的調(diào)用 fun() 《重點》
函數(shù)的調(diào)用獲得了函數(shù)的返回值的結果。
調(diào)用fun1()
res = fun()
print(res())
res() # res() 就是調(diào)用 fun() 函數(shù)。
結論:
1、 一個函數(shù)如果想要有返回值,必須寫 return xxx。
如果沒有return 和 只有一個return 沒有任何返回的對象,那么這個函數(shù)的返回值就是None。
2、return 后面的代碼都不再執(zhí)行,看到 return 會直接退出函數(shù)。
def fun1():
print(1)
return # return 后面的代碼都不再執(zhí)行,看到return直接退出函數(shù)。
print(2)
print(fun1())
2.2 函數(shù)的練習
def fun(*a):
r = 0
for i in a:
r += i
print(r)
return a
print(fun(1+2, 3))
----------------------
6 # 調(diào)用函數(shù)的結果
(3, 3) # 函數(shù)的返回值
分析:
fun() 表示函數(shù)的調(diào)用
fun(1+2, 3) 等于函數(shù)的返回值, 也就是 a 的值。
重點理解:
函數(shù)參數(shù)的傳遞
函數(shù)的調(diào)用 fun()
函數(shù)的返回值,即 return 返回的值,
函數(shù)的調(diào)用等于什么 ?a (函數(shù)的調(diào)用返回的是函數(shù)返回值的結果)
三、文檔字符串
3.1 長字符串
作用: 保留文本的格式; 作位一個多行注釋。
格式:"""xxx"""
"""
1) 保留文本的格式
2) 作位一個多行注釋。
"""
3.2 文檔字符串
作用: 對函數(shù)以及類進行說明作用及參數(shù)返回的一個文檔說明。
例如 print() 函數(shù)
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
pass
四、作用域
作用域(scope)
作用域,是指變量生效的區(qū)域
python 中一共有2中作用域:全局作用域 和 函數(shù)作用域。
函數(shù)外部的區(qū)域都是全局作用域。
在全局作用域中定義的變量,都是全局變量,全局變量可以在任何地方使用。
函數(shù)的作用域在函數(shù)調(diào)用時創(chuàng)建,在調(diào)用結束后銷毀。
a = 1
def fun():
a = 10
print(a)
fun()
print(a)
-------------
10 # 調(diào)用函數(shù)
1 # 全局變量
練習
用函數(shù)實現(xiàn)一個判斷用戶輸入的年份是否為閏年的程序:
能被400整除的年份
能被4整除,但是不能被100整除的年份
以上2種方法滿足一種即為閏年。
def fun():
while True:
year = int(input("請輸入年份:"))
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print('%d是閏年' % year)
else:
print('%d不是閏年' % year)
fun()
----------------
請輸入年份:2000
2000是閏年
請輸入年份:2001
2001不是閏年
請輸入年份:2020
2020是閏年
請輸入年份:
猴子吃桃問題(遞歸)
猴子第一天摘下若干個桃子,當即吃了一半,還不過癮,有多吃了一個,第二天早上又將剩下的桃子吃掉了一半,又多吃了一個,以后每天早上都吃了前一天剩下的一半零一個。到了第10天早上想再吃時,見到只剩下一個桃子了,請問一共摘了多少個 桃子 ?
思路:
假設總共x 個桃子,每天吃一半多吃一個,那第二天剩下:x/2-1.
依次遞減,到第10天為1個,可以反過來思考,設置一個隊列,令對列從右到左取值,索引為-1的元素=1,然后前一個索引對應的元素則為 (x+1)*2
list.insert(index, (x+1) * 2),到第10個索引對應的的值,即list[0]對應的值就是第一天摘的桃子總數(shù)
def fun1():
i = -1
x = 1
list1 = []
while i >= -10:
if i == -1:
list1.append(x)
#print(list1)
i = i - 1
if i < -1:
x = (x+1) * 2
list1.insert(i-1, x)
i = i-1
print(list1)
return x
print('猴子一共摘了%d個桃子' % fun1())
-------------輸出--------------
[1534, 766, 382, 190, 94, 46, 22, 10, 4, 1]
猴子一共摘了1534個桃子
總結
以上是生活随笔為你收集整理的python怎么调用函数的返回值类型,10、Python基础之函数的调用与返回值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab结构阵列设计,ROM阵列及其
- 下一篇: 判断一个where条件的对错php,sq