pythonベース-組み込み関数-役割ドメイン-閉パッケージ-再帰-python 3


組み込み関数-役割ドメイン-閉パッケージ-再帰
1.使用可能ないくつかの組み込み関数
      :
    print(dir(__builtins__))
    :
    len    
    min    
    max    
    sorted   ,    
    reversed   
    sum   
    :
    bin()        
    oct()         
    hex()          
    ord()            ASIIC  
    chr()    ASIIC          
  :
    1.enumerate()              
    2.filter()         
    3.map()           。    iterable         fuction  ,     map  
    4.zip()                

1.1       :
    >>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, start=0, /)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers
    When the iterable is empty, return the start value.
    This function is intended specifically for use with numeric values and may
    reject non-numeric types.

  >>> sum((1,23,4))
  28
  >>> sum([1,2,3])
  6
  >>> sum([1,2,3],10)
  16
  >>> sum([10,20,30],20)  # =iterable +start 
  80
  >>> sum([10,20,30],22)    # =iterable +start 
  82
  >>> sum({1:12,2:30})      #key   
  3
1.2    :
  >>> bin(1)
  '0b1'
  >>> bin(2)
  '0b10'
1.3    :
  >>> oct(8)
  '0o10'
  >>> oct(12)
  '0o14'
1.4     :
  >>> hex(10)
  '0xa'
  >>> hex(9)
  '0x9'
  >>> hex(15)
  '0xf'
1.5  ASIIC         
  >>> chr(65)
  'A'
  >>> chr(32)
  ' '
1.6       ASIIC 
  >>> ord('a')
  97
  >>> ord(' ')
  32
1.7 enumerate:
    >>> help(enumerate)
    Help on class enumerate in module builtins:

    class enumerate(object)
     |  enumerate(iterable[, start]) -> iterator for index, value of iterable
     |  
     |  Return an enumerate object.  iterable must be another object that supports
     |  iteration.  The enumerate object yields pairs containing a count (from
     |  start, which defaults to zero) and a value yielded by the iterable argument.
     |  enumerate is useful for obtaining an indexed list:
     |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
     |  
     |  Methods defined here:
     |  
     |  __getattribute__(self, name, /)
     |      Return getattr(self, name).
     |  
     |  __iter__(self, /)
     |      Implement iter(self).
     |  
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |  
     |  __next__(self, /)
     |      Implement next(self).
     |  
     |  __reduce__(...)
     |      Return state information for pickling.
  >>> enumerate([1,2,3,4])
        #       
  >>> list(enumerate([1,2,3,4]))                    #  
  [(0, 1), (1, 2), (2, 3), (3, 4)]              #             ,index  0,    
  >>> list(enumerate(['a','b','c','d']))
  [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]      #             ,index  0,    
  >>> list(enumerate(['a','b','c','d'],3))
  [(3, 'a'), (4, 'b'), (5, 'c'), (6, 'd')]
  >>> list(enumerate((1,23,4,5,6),3))
  [(3, 1), (4, 23), (5, 4), (6, 5), (7, 6)]
  >>> list(enumerate({1,2,3,4,5},3))        #       
  [(3, 1), (4, 2), (5, 3), (6, 4), (7, 5)]
  >>> list(enumerate({1:2,2:3,3:4},3))      #     
  [(3, 1), (4, 2), (5, 3)]
1.8 filter    
    >>> help(filter)
    Help on class filter in module builtins:
    class filter(object)
     |  filter(function or None, iterable) --> filter object
     |  Return an iterator yielding those items of iterable for which function(item)
     |  is true. If function is None, return the items that are true.
     |  Methods defined here:
     |  __getattribute__(self, name, /)
     |      Return getattr(self, name).
     |  __iter__(self, /)
     |      Implement iter(self). 
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |  __next__(self, /)
     |      Implement next(self).
     |  __reduce__(...)
     |      Return state information for pickling.
     >>> filter(lambda x:x>2,[1,2,3,4,5])   #lambda x:x>2     
       #     
    >>> list(filter(lambda x:x>2,[1,2,3,4,5]))
    [3, 4, 5]
    >>> list(filter(None,[1,2,3,4,5]))
    [1, 2, 3, 4, 5]
1.9 map  
  >>> help(map)
  Help on class map in module builtins:
  class map(object)
   |  map(func, *iterables) --> map object
   |  Make an iterator that computes the function using arguments from
   |  each of the iterables.  Stops when the shortest iterable is exhausted.
   |  Methods defined here:
   |  __getattribute__(self, name, /)
   |      Return getattr(self, name).
   |  __iter__(self, /)
   |      Implement iter(self).
   |  __new__(*args, **kwargs) from builtins.type
   |      Create and return a new object.  See help(type) for accurate signature. 
   |  __next__(self, /)
   |      Implement next(self).
   |  __reduce__(...)
   |      Return state information for pickling.
   >>> list(map(str,[1,2,3,4]))
    ['1', '2', '3', '4']
1.10 zip        
    >>> list(zip([1,2,3],[4,5,6]))
    [(1, 4), (2, 5), (3, 6)]
    >>> list(zip((1,2,3),(4,5,6)))
    [(1, 4), (2, 5), (3, 6)]
    >>> list(zip([1,2,3],(4,5,6)))
    [(1, 4), (2, 5), (3, 6)]
    >>> list(zip([1,2,3,4],[5,6,7,8,9],['a','b']))
    [(1, 5, 'a'), (2, 6, 'b')]

2.関数内変数の役割ドメイン
               :
        :         ,            
        :          ,          

    global:              ,      .(     )
    nonlocal:           ,    (   )  。(     )
  global  :
                 ,      
                  ,    global     

                    
           ,       ,     global,            

  nonlocal   :
         ,         ,    nonlocal。( :    )

  :
    global:    ,           
    nonlocal:     ,         ,  。(      ,  nonlocal)

2.1   、    
    x=1         #    ,        ,        
    def fun():
        y=2     #          
        print(x,y)
2.2         ,  global
    x=1
    def fun():
        global x   #   
        x += 1
        print(x)
2.3         ,  global
    def fun():
        global x
        x = 1
        print(x)
2.4     
    def test():
        a=1             #      
        print(a)
        def test2():
            b=2         #      
            print(a,b)
        test2()         #    ,   test()。test2()   
     >>> test()
      1
      1 2
  2.5           :
    def test():
      a=1       #    
      print(a)
      def test2():
          b=2       #    
          nonlocal a        #       
          a += 1            #    
          print(a,b)
    test2()
    >>> test()
    1
    2 2

3.埋め込み関数と閉パッケージ
    :
  def test():
      a=1
      print(a)
      def test2():
          b=2
          nonlocal a
          a += 1
          print(a,b)
      test2()       #          
   :
     def test():
        a=1
        print(a)
        def test2():
            b=2
            print(b)
        return test2    #          -  
    >>> test()
    1
    .test2 at 0x00000000005B7F28>
    :
    def test1():
        print('first')
    def fun(a):
        a()
        print('two')
    fun(test1)
    ================== RESTART: C:\Users\xinyu\Desktop\test.py ==================
    first
    two

4.再帰
        

'''
  : 5      ,        ?    4   2 。
         4    ,    3   2 。
            ,    2    。
         2  ,         。
              ,   10 。
                ?
'''
     :
    1.     
    2.      
def age(n):
    if n == 1:
        return 10
    else:
        return age(n-1)+2

  : 5!=5*4*3*2*1
#n != n*(n-1)*(n-2)*....1
def jicheng(n):
    if n == 1:
        return 1
    else:
        return jicheng(n-1)*n