码迷,mamicode.com
首页 > 其他好文 > 详细

函数小练习02

时间:2019-08-23 19:19:41      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:依次   多个   global   对象   nonlocal   pre   聚合   报错   如何   

# 写函数,接收n个数字,求这些参数数字的和。(动态传参)
# def func(*args):
#     return sum(args)
# print(func(1,2,3,3))
# 读代码,回答:代码中, 打印出来的值a, b, c分别是什么?为什么?
# a = 10
# b = 20
# def test5(a, b):
#     print(a, b)#20 10
# c = test5(b, a)
# print(c)#none
# 读代码,回答:代码中, 打印出来的值a, b, c分别是什么?为什么?
# a = 10
# b = 20
# def test5(a, b):
#     a = 3
#     b = 5
#     print(a, b)#3,5
# c = test5(b, a)
# print(c)#none

# 传入函数中多个列表和字典, 如何将每个列表的每个元素依次添加到函数的动态参数args里面?如何将每个字典的所有键值对依次添加到kwargs里面?
# 打散聚合
# 下面代码成立么?如果不成立为什么报错?怎么解决?
# a = 2
# def wrapper():
#     print(a)
# wrapper()
#
# a = 2
# def wrapper():
#     global a
#     a += 1
#     print(a)
# wrapper()
#
# def wrapper():
#     a = 1
#     def inner():
#         print(a)
#     inner()
# wrapper()
#
# def wrapper():
#     a = 1
#     def inner():
#         nonlocal a
#         a += 1
#         print(a)
#     inner()
# wrapper()

# 写函数, 接收两个列表, 将列表长度比较小的列表返回.
# def func(a,b):
#     return b if len(a)>len(b) else a
# print(func([1,2,3],[1,2]))
# 写函数, 接收一个参数(此参数类型必须是可迭代对象), 将可迭代对象的每个元素以’_’相连接, 形成新的字符串, 并返回.# 传入的可迭代对象为[1, '老男孩', '宝元']# 返回的结果为’1 _老男孩_宝元’

# 有如下函数:
# def wrapper():
#     def inner():
#         print(666)
#     return inner()
# print(wrapper())

# a = 10
# def func():
#     global a
#     a += 1
#     print(a)

函数小练习02

标签:依次   多个   global   对象   nonlocal   pre   聚合   报错   如何   

原文地址:https://www.cnblogs.com/saoqiang/p/11401875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!