python開發(fā)之函數(shù)定義實例分析
本文實例講述了python開發(fā)之函數(shù)定義方法。分享給大家供大家參考,具體如下:
下面是我做的幾個用列:
#python中的函數(shù)定義,使用和傳參
def_str = '''\
python中的函數(shù)以如下形式聲明:
def 函數(shù)名稱([參數(shù)1,參數(shù)2,參數(shù)3......]):
執(zhí)行語句
如:
def helloWorld():
print('hello')
if __name__ == '_main__':
helloWorld()
輸出:hello
'''
print(def_str)
#下面進行舉例說明
def helloWorld():
print('輸出:hello')
if __name__ == '__main__':
helloWorld()
print('''\
################################################
函數(shù)可以帶參數(shù)和返回值,參數(shù)將按從左到右的匹配,
參數(shù)可設(shè)置默認值,當使用函數(shù)時沒給相應(yīng)的參數(shù)時,
會按照默認值進行賦值
################################################
''')
#定義一個方法:x的y次方
def myMethod(x,y):
return x**y
def fib(n):
a , b = 0 , 1
while a < n:
print('a, end = ')
a , b = b , a + b
print()
#獲取一個新的數(shù)組
#@param oldList 原數(shù)組
#@param length 要添加的長度
def getList(oldList,length):
if length > 0:
for i in range(0,length):
oldList.append(i)
return oldList
else:
return '你輸入的長度小于0'
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise IOError('refusenik user')
print(complaint)
if __name__ == '__main__':
x = 3
y = 4
n = 2000
print(x , '的' , y , '次方(' ,x ,'**' , y ,') = ' , myMethod(x,y))
print('函數(shù)fib(n),當n =' ,n)
fib(n)
print(getList(['begin'],-10))
ask_ok('y')
運行效果如下:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
python中的函數(shù)以如下形式聲明:
def 函數(shù)名稱([參數(shù)1,參數(shù)2,參數(shù)3......]):
執(zhí)行語句
如:
def helloWorld():
print('hello')
if __name__ == '_main__':
helloWorld()
輸出:hello
輸出:hello
################################################
函數(shù)可以帶參數(shù)和返回值,參數(shù)將按從左到右的匹配,
參數(shù)可設(shè)置默認值,當使用函數(shù)時沒給相應(yīng)的參數(shù)時,
會按照默認值進行賦值
################################################
的 4 次方( 3 ** 4 ) = 81
函數(shù)fib(n),當n = 2000
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
你輸入的長度小于0
y輸出:hello
Yes or no, please!
y
Yes or no, please!
y
Yes or no, please!
y
Yes or no, please!
y
Traceback (most recent call last):
File "E:/Python33/python_workspace/test_function.py", line 80, in <module>
ask_ok('y')
File "E:/Python33/python_workspace/test_function.py", line 69, in ask_ok
raise IOError('refusenik user')
OSError: refusenik user
>>>
希望本文所述對大家Python程序設(shè)計有所幫助。
- 總結(jié)Python編程中函數(shù)的使用要點
- 淺析Python編寫函數(shù)裝飾器
- Python函數(shù)中的函數(shù)(閉包)用法實例
- 實例講解Python中函數(shù)的調(diào)用與定義
- 深入講解Python函數(shù)中參數(shù)的使用及默認參數(shù)的陷阱
- 使用Python內(nèi)置的模塊與函數(shù)進行不同進制的數(shù)的轉(zhuǎn)換
- python中enumerate函數(shù)遍歷元素用法分析
- 探究python中open函數(shù)的使用
- python嵌套函數(shù)使用外部函數(shù)變量的方法(Python2和Python3)
- Python函數(shù)中*args和**kwargs來傳遞變長參數(shù)的用法
- 深入解析Python中函數(shù)的參數(shù)與作用域
相關(guān)文章
Sublime?Text?配置?Python?環(huán)境的問題及解決方案
這篇文章主要介紹了Sublime?Text?配置?Python?環(huán)境的問題,文中介紹了python自定義的構(gòu)建系統(tǒng)的完整代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
Python數(shù)據(jù)結(jié)構(gòu)與算法中的棧詳解(1)
這篇文章主要為大家詳細介紹了Python中的棧,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
python 實現(xiàn)多線程下載m3u8格式視頻并使用fmmpeg合并
這篇文章主要介紹了python 實現(xiàn)多線程下載m3u8格式視頻,使用fmmpeg合并的實例代碼,需要的朋友可以參考下2019-11-11
python3+PyQt5使用數(shù)據(jù)庫表視圖
這篇文章主要為大家詳細介紹了python3+PyQt5使用數(shù)據(jù)庫表視圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Python+OpenCV圖像處理——實現(xiàn)輪廓發(fā)現(xiàn)
這篇文章主要介紹了Python+OpenCV實現(xiàn)輪廓發(fā)現(xiàn),幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-10-10
pycharm解決關(guān)閉flask后依舊可以訪問服務(wù)的問題
這篇文章主要介紹了pycharm解決關(guān)閉flask后依舊可以訪問服務(wù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

