欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2015年11月12日 11:24:17   作者:Hongten  
這篇文章主要介紹了python開發(fā)之函數(shù)定義方法,以實(shí)例形式較為詳細(xì)的分析了Python中函數(shù)的定義與使用技巧,需要的朋友可以參考下

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

下面是我做的幾個(gè)用列:

#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)
#下面進(jìn)行舉例說明
def helloWorld():
  print('輸出:hello')
if __name__ == '__main__':
  helloWorld()
print('''\
  ################################################
  函數(shù)可以帶參數(shù)和返回值,參數(shù)將按從左到右的匹配,
  參數(shù)可設(shè)置默認(rèn)值,當(dāng)使用函數(shù)時(shí)沒給相應(yīng)的參數(shù)時(shí),
  會(huì)按照默認(rèn)值進(jìn)行賦值
  ################################################
  ''')
#定義一個(gè)方法: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()
#獲取一個(gè)新的數(shù)組
#@param oldList 原數(shù)組
#@param length 要添加的長(zhǎng)度
def getList(oldList,length):
  if length > 0:
    for i in range(0,length):
      oldList.append(i)
    return oldList
  else:
    return '你輸入的長(zhǎng)度小于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),當(dāng)n =' ,n)
  fib(n)
  print(getList(['begin'],-10))
  ask_ok('y')

運(yùn)行效果如下:

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è)置默認(rèn)值,當(dāng)使用函數(shù)時(shí)沒給相應(yīng)的參數(shù)時(shí),
  會(huì)按照默認(rèn)值進(jìn)行賦值

  ################################################
的 4 次方( 3 ** 4 ) = 81
函數(shù)fib(n),當(dāng)n = 2000
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 
你輸入的長(zhǎng)度小于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
>>>

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

相關(guān)文章

最新評(píng)論