您好,登錄后才能下訂單哦!
這篇文章主要介紹Python中的函數(shù)與變量有什么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
首先大家先來看一看這個(gè)代碼演示,理解下Python中的函數(shù)與變量的關(guān)系:
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print "You have %d cheeses!" % cheese_count
print "You have %d boxes of crackers!" % boxes_of_crackers
print "Man that's enough for a party!"
print "Get a blanket.\n"
print "We can just give the function numbers directly:"
cheese_and_crackers(20, 30)
print "OR, we can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)
print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)
print "And we can combine the two, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
通過這個(gè)案例,我們可以發(fā)現(xiàn)函數(shù) cheese_and_crackers 傳遞很多的參數(shù),然后在函數(shù)里把它們打印出來。我們可以在函數(shù)里用變量名,可以在函數(shù)里做運(yùn)算,甚至可以將變量和運(yùn)算結(jié)合起來。從一方面來說,函數(shù)的參數(shù)和我們的生成變量時(shí)用的 = 賦值符類似。事實(shí)上,如果你可以用 = 給一個(gè)東西命名,你也就可以將其作為參數(shù)傳遞給一個(gè)函數(shù)。
下面我們?cè)賮砜纯磩倓偩帉懙拇a的終端輸出結(jié)果:
$ python ex19.py
We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man that's enough for a party!
Get a blanket.
OR, we can use variables from our script:
You have 10 cheeses!
You have 50 boxes of crackers!
Man that's enough for a party!
Get a blanket.
We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man that's enough for a party!
Get a blanket.
And we can combine the two, variables and math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.
以上是“Python中的函數(shù)與變量有什么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。