pythopn関数


       ?

         ,     「  」  ,              
           ,         。     Python         ,  print()。
           ,           。
  :                   (   )    ,        ,          
  :
                    1.    
                    2.     
                    3.    

       

2.1   :
Python        def    ,      :
def    (
        ):
   
>>> def hello():
>>>     print('hello')
>>> hello()  #   

2.2         :

              ,        、         。
           ;
          。                         #          
         。

2.3      

  :    ,      ,     。                ,           
  (    ,          )

  :    ,            ,     ,  ,   ,  ,       

  :      ,       ,.                  ,       ,    
  ,      ,      ,        

>>> def add(a,b):          #           a   b    
>>>     print(a+b)
>>> add(5,6)                 #           5   6    

2.4   

>>> import time
>>> def logger(n):
>>>     time_format='%Y-%m-%d %x'
>>>     time_current=time.strftime(time_format)
>>>     with open('    ', 'a') as f:
>>>         f.write('%s end action%s
' %(time_current, n)) >>> def action1(n): >>> print ('starting action1...') >>> logger(n) >>> def action2(n): >>> print ('starting action2...') >>> logger(n) >>> def action3(n): >>> print ('starting action3...') >>> logger(n) >>> action1(1) >>> action2(2) >>> action3(3) : 。 。 >>> def f(name,age): >>> print('I am %s,I am %d'%(name,age)) >>> f('name',age) # : , 。 , Python 。 >>> def f(name,age): >>> print('I am %s,I am %d'%(name,age)) >>> # f(16,'name') # >>> f(age=16,name='name') ( ): , , 。 age, age : >>> def print_info(name,age,sex='male'): >>> print('Name:%s'%name) >>> print('age:%s'%age) >>> print('Sex:%s'%sex) >>> return >>> print_info('alex',18) >>> print_info(' ',40,'female') 。 , 2 , 。 >>> def add(*tuples): >>> sum=0 >>> for v in tuples: >>> sum+=v >>> return sum >>> print(add(1,4,6,9)) >>> print(add(1,4,6,9,5)) >>> def add(*t): # *t *tuples >>> print(type(t)) # t >>> print(t) # (1, 4, 6, 9, 5) >>> add(1,4,6,9,5) (*) 。 (**) >>> def print_info(**kwargs): >>> print(kwargs) >>> for i in kwargs: >>> print('%s:%s'%(i,kwargs[i])) # >>> return >>> print_info(name='n',age=18,sex='female',hobby='girl') : {'name': 'n', 'age': 18, 'sex': 'female', 'hobby': 'girl'} name:n age:18 sex:female hobby:girl >>> def print_info(**k): # **k **kwargs >>> print(type(k)) # # k >>> print(k) # {'name': 'n', 'age': 18, 'sex': 'female', 'hobby': 'girl'} >>> print_info(name='n',age=18,sex='female',hobby='girl') *args **kwargs *args **kwargs def function(name,age=22,*args,**kwargs) ( ): : >>> def add(x,y,f): >>> return f(x) + f(y) >>> res = add(3,-6,abs) #abs >>> print(res) >>> def foo(): >>> x=3 >>> def bar(): >>> return x >>> return bar , return : return , , return return, None return , 。 5.1 python 4 : L:local, , ; E:enclosing, , , ; G:globa, , ; B:built-in, , int, bytearray 。 : > > >python , LEGB。 >>> x = int(2.9) # int built-in >>> g_count = 0 # global >>> def outer(): >>> o_count = 1 # enclosing >>> def inner(): >>> i_count = 2 # local >>> print(o_count) >>> # print(i_count) >>> inner() >>> outer() ,local enclosing ,enclosing local。 5.2 Python , (module), (class) (def、lambda) , ( if、try、for ) , : >>> if 2>1: >>> x = 1 >>> print(x) # 1 ,if ,x , 。 >>> def test(): >>> x = 2 >>> print(x) # NameError: name 'x2' is not defined def、class、lambda 。 5.3 >>> x=6 >>> def f2(): >>> print(x) >>> x=5 >>> f2() # print(x) , , x=5( ), x , : # local variable 'x' referenced before assignment. x=5 ? : x=5,x=6 # :name 'x' is not defined # >>> x=6 >>> def f2(): >>> x+=1 #local variable 'x' referenced before assignment. >>> f2() 5.4 global , global nonlocal , (global ) , global , : >>> count = 10 >>> def outer(): >>> global count >>> print(count) #10 >>> count = 100 >>> print(count) #100 >>> outer() 5.5 nonlocal global , , (enclosing , ) , nonlocal >>> def outer(): >>> count = 10 >>> def inner(): >>> nonlocal count >>> count = 20 >>> print(count) #20 >>> inner() >>> print(count) #20 >>> outer() 5.6 (1) :LEGB, > > >python ; (2) 、 、 ; (3) , , , ; (4) , global , nonlocal 。nonlocal python3 , , 。 : , 。 , 。 1( ) >>> def factorial(n): >>> result=n >>> for i in range(1,n): >>> result*=i >>> return result >>> print(factorial(4)) #********** ********* >>> def factorial_new(n): >>> if n==1: >>> return 1 >>> return n*factorial_new(n-1) >>> print(factorial_new(3)) 2( ) >>> def fibo(n): >>> before=0 >>> after=1 >>> for i in range(n-1): >>> ret=before+after >>> before=after >>> after=ret >>> return ret >>> print(fibo(3)) #************** ********************* >>> def fibo_new(n):#n , [0] >>> if n <= 1: >>> return n >>> return(fibo_new(n-1) + fibo_new(n-2)) >>> print(fibo_new(3)) #1 >>> print(fibo_new(30000)) #maximum recursion depth exceeded in comparison : , 。 , , 。 : 1. 2. , 3. , ( , (stack) , , , , 。 , , , 。)