国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python開發(fā)之函數(shù)定義實例分析

2020-01-04 17:56:57
字體:
供稿:網(wǎng)友

這篇文章主要介紹了python開發(fā)之函數(shù)定義方法,以實例形式較為詳細的分析了Python中函數(shù)的定義與使用技巧,需要的朋友可以參考下

本文實例講述了python開發(fā)之函數(shù)定義方法。分享給大家供大家參考,具體如下:

下面是我做的幾個用列:

 

 
  1. #python中的函數(shù)定義,使用和傳參 
  2. def_str = '''/ 
  3. python中的函數(shù)以如下形式聲明: 
  4. def 函數(shù)名稱([參數(shù)1,參數(shù)2,參數(shù)3......]): 
  5. 執(zhí)行語句 
  6. 如: 
  7. def helloWorld(): 
  8. print('hello'
  9. if __name__ == '_main__'
  10. helloWorld() 
  11. 輸出:hello 
  12. ''
  13. print(def_str) 
  14. #下面進行舉例說明 
  15. def helloWorld(): 
  16. print('輸出:hello'
  17. if __name__ == '__main__'
  18. helloWorld() 
  19. print('''/ 
  20. ################################################ 
  21. 函數(shù)可以帶參數(shù)和返回值,參數(shù)將按從左到右的匹配, 
  22. 參數(shù)可設(shè)置默認值,當(dāng)使用函數(shù)時沒給相應(yīng)的參數(shù)時, 
  23. 會按照默認值進行賦值 
  24. ################################################ 
  25. ''') 
  26. #定義一個方法:x的y次方 
  27. def myMethod(x,y): 
  28. return x**y 
  29. def fib(n): 
  30. a , b = 0 , 1 
  31. while a < n: 
  32. print('a, end = '
  33. a , b = b , a + b 
  34. print() 
  35. #獲取一個新的數(shù)組 
  36. #@param oldList 原數(shù)組 
  37. #@param length 要添加的長度 
  38. def getList(oldList,length): 
  39. if length > 0: 
  40. for i in range(0,length): 
  41. oldList.append(i) 
  42. return oldList 
  43. else
  44. return '你輸入的長度小于0' 
  45. def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): 
  46. while True: 
  47. ok = input(prompt) 
  48. if ok in ('y''ye''yes'): 
  49. return True 
  50. if ok in ('n''no''nop''nope'): 
  51. return False 
  52. retries = retries - 1 
  53. if retries < 0: 
  54. raise IOError('refusenik user'
  55. print(complaint) 
  56. if __name__ == '__main__'
  57. x = 3 
  58. y = 4 
  59. n = 2000 
  60. print(x , '的' , y , '次方(' ,x ,'**' , y ,') = ' , myMethod(x,y)) 
  61. print('函數(shù)fib(n),當(dāng)n =' ,n) 
  62. fib(n) 
  63. print(getList(['begin'],-10)) 
  64. ask_ok('y'

運行效果如下:

 

 
  1. Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 
  2. Type "copyright""credits" or "license()" for more information. 
  3. >>> ================================ RESTART ================================ 
  4. >>>  
  5. python中的函數(shù)以如下形式聲明: 
  6.  
  7. def 函數(shù)名稱([參數(shù)1,參數(shù)2,參數(shù)3......]): 
  8. 執(zhí)行語句 
  9.  
  10. 如: 
  11.  
  12. def helloWorld(): 
  13. print('hello'
  14.  
  15. if __name__ == '_main__'
  16. helloWorld() 
  17.  
  18. 輸出:hello 
  19.  
  20. 輸出:hello 
  21. ################################################ 
  22.  
  23. 函數(shù)可以帶參數(shù)和返回值,參數(shù)將按從左到右的匹配, 
  24. 參數(shù)可設(shè)置默認值,當(dāng)使用函數(shù)時沒給相應(yīng)的參數(shù)時, 
  25. 會按照默認值進行賦值 
  26.  
  27. ################################################ 
  28. 的 4 次方( 3 ** 4 ) = 81 
  29. 函數(shù)fib(n),當(dāng)n = 2000 
  30. 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 
  31. 你輸入的長度小于0 
  32. y輸出:hello 
  33. Yes or no, please! 
  34. Yes or no, please! 
  35. Yes or no, please! 
  36. Yes or no, please! 
  37. Traceback (most recent call last): 
  38. File "E:/Python33/python_workspace/test_function.py", line 80, in <module> 
  39. ask_ok('y'
  40. File "E:/Python33/python_workspace/test_function.py", line 69, in ask_ok 
  41. raise IOError('refusenik user'
  42. OSError: refusenik user 
  43. >>> 

希望本文所述對大家Python程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 临城县| 博湖县| 淮北市| 维西| 隆林| 巨野县| 博罗县| 伊春市| 科技| 霞浦县| 扶沟县| 嵊泗县| 长春市| 东兰县| 罗江县| 巴青县| 比如县| 年辖:市辖区| 金山区| 阿拉善左旗| 湖口县| 平山县| 富蕴县| 汶上县| 满洲里市| 凤山市| 周宁县| 长垣县| 澳门| 建瓯市| 江山市| 犍为县| 塘沽区| 九台市| 克什克腾旗| 平遥县| 兴义市| 禹州市| 金秀| 二连浩特市| 修水县|