Pythonの14日目の努力--関数と反復器の練習

6324 ワード

01内容レビュー
  • 反復可能オブジェクト:
  • 反復の実在値
  • を更新することができる.
  • の内部には
    '__iter__'
    
    の方法が含まれている.
  • str list tuple dict set range
  • 利点:操作方法が多く、操作が柔軟で、直感的な
  • の欠点:メモリの消費量.

  • 反復器:
  • は反復のツール(データ構造)を更新することができる.
  • 内部に
    '__iter__'   '__next__'
    
    の方法を含む
  • .
  • ファイルハンドル
  • の利点:メモリの節約.不活性機構
  • 欠点:直感的ではなく、速度が相対的に遅く、操作方法が単一で、1本の道が黒くなる.

  • 出力をフォーマットします.
  • 関数名の適用.

  • 02練習
    # 1.            :
    #   :
    #
    # def func1():
    #     print('in func1' )
    #
    # def func2():
    #     print('in func2' )
    #
    # ret = func1
    # ret()
    # ret1 = func2
    # ret1()
    # ret2 = ret
    # ret3 = ret2
    # ret2()
    # ret3()
    
    #     :
    # in func1
    # in func2
    # in func1
    # in func1
    
    #    :
    #
    # def func1():
    #     print('in func1' )
    #
    # def func2():
    #     print('in func2' )
    #
    # def func3(x, y):
    #     x()
    #
    #     print('in func3' )
    #
    #     y()
    #
    # print(111)
    # func3(func2, func1)
    # print(222)
    #     :
    # 111
    # in func2
    # in func3
    # in func1
    # 222
    
    #    (   ):
    #
    # def func1():
    #     print('in func1' )
    #
    # def func2(x):
    #     print('in func2' )
    #     return x
    #
    # def func3(y):
    #     print('in func3' )
    #     return y
    #
    # ret = func2(func1)
    # ret()
    # ret2 = func3(func2)
    # ret3 = ret2(func1)
    # ret3()
    #
    #     :
    # in func2
    # in func1
    # in func3
    # in func2
    # in func1
    
    
    
    # 2.      :
    #
    # def func(arg):
    #     return arg.replace('   ', '***')
    #
    #
    # def run():
    #     msg = "Alex               "
    #     result = func(msg)
    #     print(result)
    #
    #
    # run()
    
    # Alex    ***        
    
    # def func(arg):
    #     return arg.replace('   ', '***')
    #
    #
    # def run():
    #     msg = "Alex               "
    #     result = func(msg)
    #     print(result)
    #
    # data = run()
    # print(data)
    
    # Alex    ***        
    # None
    
    
    
    # 3.      :
    #
    # DATA_LIST = []
    #
    #
    # def func(arg):
    #     return DATA_LIST.insert(0, arg)
    #
    #
    # data = func('    ')
    # print(data)
    # print(DATA_LIST)
    
    # None
    # ['    ']
    
    
    
    # 4.      :
    #
    # def func():
    #     print('   ')
    #     return '    '
    #
    #
    # func_list = [func, func, func]
    #
    # for item in func_list:
    #     val = item()
    #     print(val)
    
    #    
    #     
    #    
    #     
    #    
    #     
    
    
    
    # 5.      :
    #
    # def func():
    #     print('   ')
    #     return '    '
    #
    #
    # func_list = [func, func, func]
    #
    # for i in range(len(func_list)):
    #     val = func_list[i]()
    #     print(val)
    
    #    
    #     
    #    
    #     
    #    
    #     
    
    
    
    # 6.      :
    # def func():
    #     return '  '
    #
    #
    # def bar():
    #     return '  '
    #
    #
    # def base(a1, a2):
    #     return a1() + a2()
    #
    #
    # result = base(func, bar)
    # print(result)
    
    #     
    
    
    
    # 7.      :
    # for item in range(10):
    #     print(item)
    #
    # print(item)
    
    # 0
    # 1
    # 2
    # 3
    # 4
    # 5
    # 6
    # 7
    # 8
    # 9
    # 9
    
    
    
    # 8.      :
    # def func():
    #     for item in range(10):
    #         pass
    #     print(item)
    # func()
    
    # 9
    
    
    
    # 9.      :
    # item = '   '
    # def func():
    #     item = 'alex'
    #     def inner():
    #         print(item)
    #     for item in range(10):
    #         pass
    #     inner()
    # func()
    
    # 9
    
    
    
    # 10.      :
    # l1 = []
    # def func(args):
    #     l1.append(args)
    #     return l1
    # print(func(1))
    # print(func(2))
    # print(func(3))
    
    # [1]
    # [1,2]
    # [1,2,3]
    
    
    
    # 11.      :
    # name = '  '
    # def func():
    #     global name
    #     name = '  '
    # print(name)
    # func()
    # print(name)
    
    #   
    #   
    
    
    
    # 12.      :
    # name = '  '
    # def func():
    #     print(name)
    # func()
    
    #   
    
    
    
    # 13.      :
    # name = '  '
    # def func():
    #     print(name)
    #     name = 'alex'
    # func()
    
    #   
    
    
    
    # 14.      :
    # def func():
    #     count = 1
    #     def inner():
    #         nonlocal count
    #         count += 1
    #         print(count)
    #     print(count)
    #     inner()
    #     print(count)
    # func()
    
    # 1
    # 2
    # 2
    
    
    
    # 15.      :
    # def extendList(val,list=[]):
    #  list.append(val)
    #  return list
    #
    # list1 = extendList(10)
    # list2 = extendList(123,[])
    # list3 = extendList('a')
    #
    # print('list1=%s'%list1)
    # print('list2=%s'%list2)
    # print('list3=%s'%list3)
    
    # list1=[10,'a']
    # list1=[123]
    # list1=[10,'a']
    
    
    
    # 16.      :
    # def extendList(val,list=[]):
    #  list.append(val)
    #  return list
    # print('list1=%s'% extendList(10))
    # print('list2=%s'% extendList(123,[]))
    # print('list3=%s'% extendList('a'))
    
    # list1=[10]
    # list1=[123]
    # list1=[10,'a']
    
    
    
    # 17.   : while    for       
    # l1 = [1,2,3,4,5,6,7,8,9,10]
    #
    # obj = iter(l1)
    # while 1:
    #     try:
    #         print(next(obj))
    #     except StopIteration:
    #         break
    
    
    
    # 18.   ,  n  ,    {‘max’:   ,’min’:   }
    #   :min_max(2,5,7,8,4)   :{‘max’:8,’min’:2}(    max(),min()    )
    # dic = {}
    # def min_max(*args):
    #     dic['max'] = max(args)
    #     dic['min'] = min(args)
    #     print(dic)
    # min_max(2,5,7,8,4)
    
    
    
    # 19.   ,      n,  n   
    #   :cal(7)   7654321
    # def factorial(num):
    #     result = 1
    #     while 1:
    #         if num > 0:
    #             result = result * num
    #         else:
    #             break
    #         num = num - 1
    #     return result
    # print(factorial(3))
    
    # def factorial(num):
    #     result = 1
    #     for i in range(num,0,-1):
    #         result = result * i
    #     return result
    # print(factorial(3))
    
    
    
    # 20.   ,         ,   52 ,        (   )
    #   :[(‘  ’,2),(‘  ’,2), …(‘  ’,‘A’)]
    # l1 = []
    # def poker():
    #     tu1 = ()
    #     tu2 = ()
    #     tu3 = ()
    #     tu4 = ()
    #     s1 = 'JQKA'
    #     for i in range(2,11):
    #         tu1 = ('  ',i)
    #         tu2 = ('  ',i)
    #         tu3 = ('  ',i)
    #         tu4 = ('  ',i)
    #         l1.append(tu1)
    #         l1.append(tu2)
    #         l1.append(tu3)
    #         l1.append(tu4)
    #     for j in s1:
    #         tu1 = ('  ', j)
    #         tu2 = ('  ', j)
    #         tu3 = ('  ', j)
    #         tu4 = ('  ', j)
    #         l1.append(tu1)
    #         l1.append(tu2)
    #         l1.append(tu3)
    #         l1.append(tu4)
    # poker()
    # print(l1)
    
    # def poker():
    #     num = list(range(2, 11))
    #     num.extend('JQKA')
    #     color = ('  ', '  ', '  ', '  ')
    #     card = [(m, n) for m in color for n in num]
    #     return card
    # print(poker())