python—函數實例
練習:判斷輸入的是不是一個數字
#!/usr/bin/env python def isNum(): sth = raw_input("Please input something: ") try: if type(int(sth)) == type(1): print "%s is a number" % sth except Exception: print "%s is not a number" % sth isNum()
2.函數的參數
練習:判斷輸入的是不是一個數字
#!/usr/bin/env python import sys def isNum(s): for i in s: if i in "1234567890": pass else: print "%s is not a number" % s break else: print "%s is a number" % s isNum(sys.argv[1])
3.函數的默認參數
listdir()函數
練習:判斷輸入的是不是一個數字
#!/usr/bin/env python import os def isNum(s): for i in s: if i in "1234567890": pass else: break else: print s for i in (os.listdir("/proc")): isNum(i)
註:默認參數必須寫在後面
In [3]: def fun(x=1,y): ...: print x+y File "<ipython-input-3-3b7bae6400b0>", line 1 def fun(x=1,y): SyntaxError: non-default argument follows default argument In [4]: def fun(x,y=1): ...: print x+y ...: In [5]: fun(2)
4.1函數變量
練習:函數內部(局部)不能進行全局變量賦值等操作;如果申明成全局變量,才可以
#!/usr/bin/env python x = 1 def fun(): global x x+=1 print x fun() print x
結果:
2
2
練習2:把函數內部變量,申明成全局變量,外部也可以通過函數調用
#!/usr/bin/env python x = 1 def fun(): global x x += 1 global y y = 3 print x print y fun() print x print y
結果:
2
3
2
3
練習3:locas() :統計變量,返回字典
#!/usr/bin/env python x = 1 def fun(): x = 1 y = 1 print locals() fun() print locals()
結果:
{'y': 1, 'x': 1} {'__builtins__': <module '__builtin__' (built-in)>, '__file__': '18.py', '__package__': None, 'x': 1, 'fun': <function fun at 0x7f53bc8938c0>, '__name__': '__main__', '__doc__': None}
5.函數返回值
練習1:默認返回none
#!/usr/bin/env python def fun(): print "hello,world" print fun()
結果:
hello,world
None
練習2:自定義return返回值,return之後的語句將不再執行
#!/usr/bin/env python def fun(): print "hello,world" return "heihei" print "haha" print fun()
結果:
hello,world
heihei
練習3:判斷輸入是否為數字
函數裏很少使用print,使用return,更加簡化
#!/usr/bin/env python import os def isNum(s): for i in s: if i not in "1234567890": return False return True for i in (os.listdir("/proc")): if isNum(i): print i
練習4:isdigit()判斷輸入是否為數字
isdigit():判斷字符串是否為純數字(腳本更更簡化)
#!/usr/bin/env python import os def isNum(s): if s.isdigit(): return True return False for i in (os.listdir("/proc")): if isNum(i): print i
6.多類型傳值(元組或字典)和冗余參數
一個元組只表示一個參數;元組加一個*,則可以把元組中的元素作為參數,傳到腳本中;帶參數的元組只能放在後面,否則有語法錯誤
練習1:
In [2]: def fun(x,y,z): ...: print x + y +z ...: In [3]: a = [1,2] In [4]: fun(3,*a) 6
報錯:
In [5]: fun(*a,3)
File "<ipython-input-5-8a9ea4381ff5>", line 1
fun(*a,3)
SyntaxError: only named arguments may follow *expression
練習2:
字典傳參(形參名和實參名一致,位置無所謂)
In [8]: def fun(x,y,z): ...: print x + y +z ...: In [9]: a = {"x":1,"y":2,"z":3} In [10]: fun(**a) 6
或者:
In [11]: fun(x=1,y=2,z=3)
6
練習3:
In [1]: def fun(x,*argv,**kwargv): ...: print x ...: print argv ...: print kwargv ...: In [2]: fun(1) 1 () {}
練習4:
以等號或字典形式
In [6]: def fun(x,*argv,**kwargv): ...: print x ...: print argv ...: print kwargv ...: In [7]: t = [1,2] In [8]: fun(1,2,"a",*t,y=1,**{"b":1,"c":2}) 1 (2, 'a', 1, 2) {'y': 1, 'c': 2, 'b': 1}
7.函數的遞歸調用(函數調用本身)
條件:
1)必須有最後的默認結果,即if n == 0
2)遞歸參數必須向默認結果收斂,即factorial(n-1)
練習:階乘,n乘以f(n-1)
#!/usr/bin/env python def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) print factorial(5)
結果:
120
練習2:累加,n加f(n-1)
#!/usr/bin/env python def factorial(n): if n == 0: return 0 else: return n + factorial(n-1) print factorial(5)
結果:
15
python—函數實例