抽象

  • 计算斐波那契数
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))
执行结果
  
相关文章
相关标签/搜索