標籤:python
1.函數的定義
練習:判斷輸入的是不是一個數字
#!/usr/bin/env pythondef isNum():sth = raw_input("Please input something: ")try:if type(int(sth)) == type(1):print "%s is a number" % sthexcept Exception:print "%s is not a number" % sthisNum()
2.函數的參數
練習:判斷輸入的是不是一個數字
#!/usr/bin/env pythonimport sysdef isNum(s):for i in s:if i in "1234567890":passelse:print "%s is not a number" % sbreakelse:print "%s is a number" % sisNum(sys.argv[1])
3.函數的預設參數
listdir()函數
練習:判斷輸入的是不是一個數字
#!/usr/bin/env pythonimport osdef isNum(s):for i in s:if i in "1234567890":passelse:breakelse:print sfor i in (os.listdir("/proc")):isNum(i)
註:預設參數必須寫在後面
In [3]: def fun(x=1,y):...: print x+yFile "<ipython-input-3-3b7bae6400b0>", line 1def fun(x=1,y):SyntaxError: non-default argument follows default argumentIn [4]: def fun(x,y=1):...: print x+y...:In [5]: fun(2)
4.1函數變數
練習:函數內部(局部)不能進行全域變數賦值等操作;如果申明成全域變數,才可以
#!/usr/bin/env pythonx = 1def fun():global xx+=1print xfun()print x
結果:
2
2
練習2:把函數內部變數,申明成全域變數,外部也可以通過函數調用
#!/usr/bin/env pythonx = 1def fun():global xx += 1global yy = 3print xprint yfun()print xprint y
結果:
2
3
2
3
練習3:locas() :統計變數,返回字典
#!/usr/bin/env pythonx = 1def fun():x = 1y = 1print 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 pythondef fun():print "hello,world"print fun()
結果:
hello,world
None
練習2:自訂return傳回值,return之後的語句將不再執行
#!/usr/bin/env pythondef fun():print "hello,world"return "heihei"print "haha"print fun()
結果:
hello,world
heihei
練習3:判斷輸入是否為數字
函數裡很少使用print,使用return,更加簡化
#!/usr/bin/env pythonimport osdef isNum(s):for i in s:if i not in "1234567890":return Falsereturn Truefor i in (os.listdir("/proc")):if isNum(i):print i
練習4:isdigit()判斷輸入是否為數字
isdigit():判斷字串是否為純數字(指令碼更更簡化)
#!/usr/bin/env pythonimport osdef isNum(s):if s.isdigit():return Truereturn Falsefor 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 pythondef factorial(n):if n == 0:return 1else:return n * factorial(n-1)print factorial(5)
結果:
120
練習2:累加,n加f(n-1)
#!/usr/bin/env pythondef factorial(n):if n == 0:return 0else:return n + factorial(n-1)print factorial(5)
結果:
15
python—函數執行個體