zoukankan      html  css  js  c++  java
  • 11 练习题:数的陷阱 函数名的运用 f-string 迭代器

    day11作业
    1.请写出下列代码的执行结果:
    例一:
    def func1():
    print('in func1' )
    def func2():
    print('in func2' )
    ret = func1
    ret() # func1()
    ret1 = func2
    ret1() # func2()
    ret2 = ret
    ret3 = ret2
    ret2() # func1()
    ret3() # func1()
    result:
    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) # func2() print('in func3') func1()
    print(222)
    result:
    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) # in func2
    ret() # func1()
    ret2 = func3(func2) # in func3
    ret3 = ret2(func1) # in func2
    ret3() # func1()
    result:
    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()
    result:
    Alex的女朋友***和大家都是好朋友
    
    def func(arg):
    return arg.replace('苍老师', '***')
    def run():
    msg = "Alex的女朋友苍老师和大家都是好朋友"
    result = func(msg)
    print(result)
    data = run()
    print(data)
    result:
    Alex的女朋友***和大家都是好朋友
    None
    
    
    
    3.看代码写结果:
    DATA_LIST = []
    def func(arg):
    return DATA_LIST.insert(0, arg)
    data = func('绕不死你')
    print(data)
    print(DATA_LIST)
    result:
    ['绕不死你']
    []
    
    
    
    4.看代码写结果:
    def func():
    print('你好呀')
    return '好你妹呀'
    func_list = [func, func, func]
    for item in func_list:
    val = item()
    print(val)
    result:
    你好呀
    好你妹呀
    你好呀
    好你妹呀
    你好呀
    好你妹呀
    
    
    
    5.看代码写结果:
    def func():
    print('你好呀')
    return '好你妹呀'
    func_list = [func, func, func]
    for i in range(len(func_list)):
    val = func_listi
    print(val)
    result:
    你好呀
    好你妹呀
    你好呀
    好你妹呀
    你好呀
    好你妹呀
    
    
    
    6.看代码写结果:
    def func():
    return '烧饼'
    def bar():
    return '豆饼'
    def base(a1, a2):
    return a1() + a2()
    result = base(func, bar)
    print(result)
    result: 烧饼豆饼
    
    
    
    7.看代码写结果:
    for item in range(10):
    print(item)
    print(item)
    result: 0 1 2 3 4 5 6 7 8 9 9
    
    ]
    
    8.看代码写结果:
    def func():
    for item in range(10):
    pass
    print(item)
    func()
    result: 9
    
    
    
    9.看代码写结果:
    item = '老男孩'
    def func():
    item = 'alex'
    def inner():
    print(item)
    for item in range(10):
    pass
    inner()
    func()
    result: 9
    
    
    
    10.看代码写结果:
    l1 = []
    def func(args):
    l1.append(args)
    return l1
    print(func(1))
    print(func(2))
    print(func(3))
    result:
    [1]
    [1, 2]
    [1, 2, 3]
    
    
    
    11.看代码写结果:
    name = '太白'
    def func():
    global name
    name = '男神'
    print(name)
    func()
    print(name)
    result: 太白 男神
    
    
    
    12.看代码写结果:
    name = '太白'
    def func():
    print(name)
    func()
    result: 太白
    
    
    
    13.看代码写结果:
    name = '太白'
    def func():
    print(name)
    name = 'alex'
    func()
    result: local variable 'name' referenced before assignment
    
    
    
    14.看代码写结果:
    def func():
    count = 1
    def inner():
    nonlocal count
    count += 1
    print(count)
    print(count)
    inner()
    print(count)
    func()
    result: 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)
    result:
    list1=[10, 'a']
    list2=[123]
    list3=[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'))
    result:
    list1=[10]
    list2=[123]
    list3=[10, 'a']
    
    
    
    17.用你的理解解释一下什么是可迭代对象,什么是迭代器。
    可迭代对象:iterate 具有__iter__方法的对象,就是可迭代对象。
    迭代器:iterator 具有__iter__方法和__next__方法的对象。
    
    
    
    18.如何判断该对象是否是可迭代对象或者迭代器?
    object = ? # object是未知对象
    object_directory = dir(object)
    print('iter' in object_directory) # 若返回 True 则该对象是可迭代对象
    print(('next' and 'iter') in object_directory) # 若返回 True 则该对象是迭代器
    
    
    
    19.写代码:用while循环模拟for内部的循环机制(面试题)。
    iterator = iter(iterate_object)
    while 1:
    try:
    i = next(iterator)
    except StopIteration:
    break
    
    
    
    20.写函数,传入n个数,返回字典
    {‘max’:最大值,’min’:最小值}
    例如: min_max(2, 5, 7, 8, 4)
    返回: {‘max’:8,’min’:2}(此题用到max(), min()内置函数)
    def min_max(*args):
    return {'max': max(args), 'min': min(args)}
    
    
    
    21.写函数,传入一个参数n,返回n的阶乘
    例如: cal(7)
    计算7654321
    def cal(integer):
    re = 1
    for i in range(1, integer+1):
    re *= i
    return re
    
    
    
    22.写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组(选做题)
    例如:[(‘红心’,2), (‘草花’,2), …(‘黑桃’,‘A’)]
    def poker_list():
    list = []
    type1_list = ['红心', '草花', '黑桃', '红桃']
    type2_list = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'J', 'A']
    for type1 in type1_list:
    for type2 in type2_list:
    list.append((type1, type2))
    return list
    
    
    
    23.写代码完成99乘法表.(选做题,面试题)
    1 * 1 = 1
    2 * 1 = 2 2 * 2 = 4
    3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
    ......
    9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
    for int_1 in range(1, 10):
        for int_2 in range(1, int_1 + 1):
            print(f'{int_1} * {int_2} = {int_1 * int_2}', end=' ')
        print('')
    
  • 相关阅读:
    Educational Codeforces Round 83 --- F. AND Segments
    Educational Codeforces Round 83 --- G. Autocompletion
    SEERC 2019 A.Max or Min
    2019-2020 ICPC Southwestern European Regional Programming Contest(Gym 102501)
    Educational Codeforces Round 78 --- F. Cards
    今天我学习了一门全新的语言
    codeforces 1323D 题解(数学)
    Educational Codeforces Round 80 (Div. 2) 题解 1288A 1288B 1288C 1288D 1288E
    Educational Codeforces Round 81 (Div. 2) 题解 1295A 1295B 1295C 1295D 1295E 1295F
    Codeforces Round #617 (Div. 3) 题解 1296C 1296D 1296E 1296F
  • 原文地址:https://www.cnblogs.com/raygor/p/13273041.html
Copyright © 2011-2022 走看看