python開發(fā)之函數(shù)定義實例分析
更新時間:2015年11月12日 11:24:17 作者:Hongten
這篇文章主要介紹了python開發(fā)之函數(shù)定義方法,以實例形式較為詳細的分析了Python中函數(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ù)時,
會按照默認值進行賦值
################################################
''')
#定義一個方法: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ù)時,
會按照默認值進行賦值
################################################
的 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程序設計有所幫助。
您可能感興趣的文章:
- 總結(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ù)與作用域
相關文章
Python中常用文本數(shù)據(jù)格式轉(zhuǎn)換
這篇文章主要為大家整理了一下Python中一些常用文本數(shù)據(jù)格式轉(zhuǎn)換,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的小伙伴可以參考一下2025-02-02
Python PyAutoGUI模塊控制鼠標和鍵盤實現(xiàn)自動化任務詳解
這篇文章主要介紹了Python PyAutoGUI模塊控制鼠標和鍵盤實現(xiàn)自動化任務,結(jié)合實例形式詳細分析了pyautogui模塊的安裝、導入以及針對鼠標與鍵盤的各種常見響應操作實現(xiàn)技巧,需要的朋友可以參考下2018-09-09
前女友發(fā)來加密的"520快樂.pdf",我用python破解開之后,卻發(fā)現(xiàn)
520收到前女友發(fā)來的加密PDF文件,說打開之后有驚喜,難道是要復合?我用python破解開之后,卻發(fā)現(xiàn)...python干貨+劇情滿滿收藏收藏2021-08-08
Python+Selenium實現(xiàn)在Geoserver批量發(fā)布Mongo矢量數(shù)據(jù)
這篇文章主要為大家詳細介紹了如何利用Python+Selenium實現(xiàn)在 Geoserver批量發(fā)布來自Mongo中的矢量數(shù)據(jù),文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-07-07

