新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中fact函數(shù)是什么及如何使用?
雖然已經(jīng)和大家講述過很多函數(shù)了,但是因?yàn)樵陧?xiàng)目搭建里,函數(shù)是起著十分重要的作用,所以需要用到的函數(shù)有很多,而絕大部分函數(shù),大家經(jīng)常使用過程中,輕松運(yùn)用使用,但是還有少部分函數(shù),大家卻未見過,也不知道怎么去使用,因此,針對(duì)非常見型函數(shù),比如“fact函數(shù)”小編會(huì)細(xì)致給大家講解。

往期內(nèi)容回顧——函數(shù)的定義與調(diào)用
在Python中,函數(shù)是這樣定義的:
def helloPrint(name): print(‘hello’+name)
Python函數(shù)定義的基本語法如下:
def name(parameter1, parameter2, . . .): Body
調(diào)用fact函數(shù)即可得到階乘值了:
>>> def fact(n): ... """ Return the factorial of the given number. """ ?--- ... r = 1 ... while n > 0: ... r = r * n ... n = n - 1 ... return r ?--- ...
雖然Python函數(shù)都帶有返回值,但是否使用這個(gè)返回值則由寫代碼的人決定:
>>> fact(4) ?--- 24 ?--- >>> x = fact(4) ?--- >>> x 24 >>>
實(shí)例講解:
#在函數(shù)內(nèi)部,可以調(diào)用其他函數(shù)。如果一個(gè)函數(shù)在內(nèi)部調(diào)用自身本身,這個(gè)函數(shù)就是遞歸函數(shù)。 #例如計(jì)算階乘n! = 1 x 2 x 3 x ... x n def fact(n): if n == 1: return n else: return n * fact(n-1) print(fact(5)) #120
大家現(xiàn)在應(yīng)該清楚關(guān)于fact函數(shù)是如何使用了吧,結(jié)合基礎(chǔ)的函數(shù)知識(shí),以及實(shí)際的操作,相信便于大家理解。學(xué)會(huì)以后,記得運(yùn)用在實(shí)際操作上哦~
文章題目:創(chuàng)新互聯(lián)Python教程:python中fact函數(shù)是什么及如何使用?
本文網(wǎng)址:http://www.dlmjj.cn/article/dphsgoj.html


咨詢
建站咨詢
