python基礎(chǔ)教程-函數(shù)_第1頁
python基礎(chǔ)教程-函數(shù)_第2頁
python基礎(chǔ)教程-函數(shù)_第3頁
python基礎(chǔ)教程-函數(shù)_第4頁
python基礎(chǔ)教程-函數(shù)_第5頁
已閱讀5頁,還剩17頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、1函數(shù)Python基礎(chǔ)教程 函數(shù)2函數(shù)是什么主要內(nèi)容1函數(shù)定義與調(diào)用2函數(shù)參數(shù)與返回值3變量作用域4函數(shù)優(yōu)缺點(diǎn)53函數(shù)是什么sum = 0for i in range(1,sum += i print(Sum from11):1 to 10is, sum)求1 to 10, 20 to 37, 35 to 49的和sum = 0for i in range(20,sum += i print(Sum from38):20 to 37is, sum)sum = 0for i in range(35,sum += i print(Sum from50):35 to 49is, sum)4函數(shù)是什么

2、def sum(i1, i2):result = 0for i in range(i1, i2 + 1): result += ireturn resultdef main():print(Sumfrom 1 to 10 is,)sum(1, 10)sum(20, 37sum(35, 49main() # Call the main functionprint(Sum from 20 to 37 is,)print(Sum from 35 to 49 is,)5收件人信息:姓名,地址,電話 發(fā)件人信息:姓名,地址,電話 物品,郵費(fèi)快遞單函數(shù)是什么順豐客戶服務(wù)中心接件地址順豐快遞員6函數(shù)是什么p

3、函數(shù)p完成特定功能的一個(gè)語句組,這組語句可以作為 一個(gè)單位使用,并且給它取一個(gè)名字p通過函數(shù)名執(zhí)行pf(x) =px2 2x + 17函數(shù)頭def sum(i1, i2):the sum from i1 to i2is low value, i2 is high value”To calculate i1”關(guān)鍵字函數(shù)名參數(shù)函數(shù)定義函數(shù)體resultfor i in= 0range(i1,result += ireturn resulti2 + 1):#return sumvalue縮進(jìn)說 明 文 檔語句8函數(shù)定義result =def sum(i1, i2):0for i in range(i

4、1, i2 + 1):形式參數(shù)(形參)函數(shù)定義result += i return result函數(shù)調(diào)用sum(1, 10)實(shí)際參數(shù)(實(shí)參)9函數(shù)調(diào)用1 to 10 is, sum(1, 10)def main():p r i n t ( S u m p r i n t ( S u m print(Sumfrom from from20 to 37 is,35 to 49 is,sum(20, sum(35,37)49)main() # Call the main functiondef sum(i1, i2):result = 0for i in range(i1, i2 + 1):resu

5、lt += ireturn result10函數(shù)調(diào)用main()def sum(i1,result =for i ini2): 0range(i1,def main():sum(1, 10)sum(20, 37)sum(35, 49)i2 + 1):result += i return result11函數(shù)參數(shù)def sum(i1,result =for i ini2): 0range(i1,sum(1, 10)i2 + 1):result += i return result12def Biggersmaller(bigger, smaller): print bigger - smalle

6、rBiggersmaller(3, 1)函數(shù)參數(shù)Biggersmaller(1, 3)Biggersmaller(smaller = 1, bigger = 3)13def defaultParameters(arg1, arg2=2, arg3=3): print arg1=, arg1print arg2=, arg2 print arg3=, arg3函數(shù)參數(shù) 缺省參數(shù)defaultParameters(10)defaultParameters(10, 10)defaultParameters(10, 10, 10)14函數(shù)返回值def sum(i1,result =for i ini2

7、): 0range(i1,i2 + 1): 函數(shù)調(diào)用完成后,返回?cái)?shù)據(jù) Return語句終止當(dāng)前函數(shù)的執(zhí)行 Return后的語句將被忽略result += Ireturn result15函數(shù) 變量作用域globalVar = 1def f1():localVar = 2print printglobalVar localVarf1()print printglobalVar localVar16函數(shù) 變量作用域p局部變量p只能在程序的特定部分使用的變量p函數(shù)內(nèi)部p全局變量p為整個(gè)程序所使用的變量p所有函數(shù)均可以使用17函數(shù) 變量作用域x = 1def f1():x = 2 print xf1()print x18函數(shù) 變量作用域x = 1def increase(): global xx = x + 1 print xincrease() print x19函數(shù)實(shí)例計(jì)算n!有參數(shù):n返回值:n!def p(n): x = 1for i in range(1, n+1): x = x * ireturn xdef main():n = input(請輸入一個(gè)整數(shù)) print n, !的值為:, p(n)main()20函數(shù) 優(yōu)點(diǎn)代碼可重用提高開發(fā)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論