1 fibs = [0 , 1] 2 num = int(input('please input your name: ')) 3 for i in range(num-2): 4 fibs.append(fibs[-2]+fibs[-1]) 5 print(fibs)
判斷某個對象是否可調用,可以使用內置函數callablepython
1 def hello(name): 2 return 'hello, ' + name + '!' 3 print(hello('world')) 4 print(hello('Gumby'))
1 def fibs(num): 2 result = [0,1] 3 for i in range(num-2): 4 result.append(result[-2]+result[-1]) 5 return result 6 print(fibs(10)) 7 print(fibs(15))
1 def square(x): 2 'this number is x.' 3 return x * x 4 print(square.__doc__) 5 print(square(5))
執行結果app
這裏使用return只是爲告終束函數 函數
1 def test(): 2 print('ha ha ha') 3 return 4 print('he he he') 5 print(test())
執行結果this
將同一個列表賦給兩個變量是,這兩個變量將同時指向這個列表。要避免原列表被修改,必須建立列表的副本spa
關鍵字參數和默認值3d
位置參數code
1 def hello(greeting,name): 2 print ('{}, {}!'.format(greeting, name)) 3 hello('hello', 'world')
關鍵字參數orm
1 def hello(greeting,name): 2 print ('{}, {}!'.format(greeting, name)) 3 hello(greeting='hello', name='world')
默認參數 對象
指定默認值後,調用函數是可不提供它
1 def hello_1(greeting='hello', name='world'): 2 print('{}, {}!'.format(greeting, name)) 3 hello_1()
執行結果
指定一個參數
1 def hello_1(greeting='hello', name='world'): 2 print('{}, {}!'.format(greeting, name)) 3 hello_1(name='xiaoming')
執行結果
收集參數blog
參數前面的*號將提供的全部值都放在一個元組中,也就是將這些值收集起來
1 def print_params(*params): 2 print(params) 3 print_params('123','abc','abc')
執行結果
收集關鍵字參數
這樣獲得的是一個字典而不是元組
1 def abc(**params): 2 print(params) 3 abc(a='haha', b='hehe', c='yoyo', d='jojo', f='bibi')
執行結果
1 def print_params(x, y, z=3, *abc, **bcd): 2 print(x,y,z) 3 print(abc) 4 print(bcd) 5 print_params(1,2,3,4,5,6,7,foo=1,bar=2)
執行結果
分配參數
1 params=(1, 2) 2 def add(x, y): 3 return x + y 4 a=add(*params) 5 print(a)
執行結果
使用運算符**,可將字典中的值分配給關鍵字參數
1 params = {'name':'xiaoming','age':'10'} 2 def hello(name, age): 3 print('{}, {}!'.format(name, age)) 4 hello(**params)
執行結果
1 def story(**kwds): 2 return 'once upon a time, there was a ' '{job} called {name}.'.format_map(kwds) 3 print(story(job='king',name='xiaoming'))
執行結果
1 def story(**kwds): 2 return 'once upon a time, there was a ' '{job} called {name}.'.format_map(kwds) 3 params={'job': 'language', 'name':'python'} 4 print(story(**params))
執行結果
1 def power(x,y,*others): 2 if others: 3 print('Received redundant parameters:', others) 4 return pow(x,y) 5 a=power(2,3) 6 print(a)
執行結果
1 def power(x,y,*others): 2 if others: 3 print('Received redundant parameters:', others) 4 return pow(x,y) 5 a=power(y=3,x=4) 6 print(a)
執行結果
1 def power(x,y,*others): 2 if others: 3 print('Received redundant parameters:', others) 4 return pow(x,y) 5 a=power(3,3,'hello,world') 6 print(a)
執行結果
1 def interval(start,stop=None,step=1): 2 'Imitates range() for step > 0' 3 if stop is None: 4 start,stop=0,start 5 result=[] 6 i=start 7 while i < stop: 8 result.append(i) 9 i +=step 10 return result 11 a=interval(10) 12 print(a)
執行結果
1 def interval(start,stop=None,step=1): 2 'Imitates range() for step > 0' 3 if stop is None: 4 start,stop=0,start 5 result=[] 6 i=start 7 while i < stop: 8 result.append(i) 9 i +=step 10 return result 11 a=interval(3,12,4) 12 print(a)
執行結果
函數foo修改(從新關聯)了變量x,但當你最終查看時,它根本沒變。這是由於調用foo是建立了一個新的命名空間,供foo中的代碼塊使用
使用函數globals來訪問全局變量
階乘和冪
1 def factorial(n): 2 result = n 3 for i in range(1, n): 4 result *= i 5 return result 6 print(factorial(5))
執行結果
1 def power(x,n): 2 result = 1 3 for i in range(n): 4 result *= x 5 return result 6 print(power(2,3))
執行結果