首页 > 编程语言 > 详细

01月29日【Python3 基础知识】

时间:2018-02-26 14:51:00      阅读:221      评论:0      收藏:0      [点我收藏+]
01月29日【Python3 基础知识】

5.4 参数匿名函数字典排序
5.5 生成式和生成器
5.6 装饰器的作用

5.4 参数匿名函数字典排序

# *元组;**字典
def add(*args):
    total = 0
    for i in args:
        total += i
    print("total = {0}".format(total))
def sortDictValue(dict1):
    print(sorted(dict1.items(), key =lambda d:d[1], reverse=False))
if __name__ == ‘__main__‘:
    add(1, 2, 3, 4, 5)
    s1 = lambda x, y: x + y
#    def s1(x, y):
#        return x + y
    print(s1(10, 20))
    aaa = dict(a = 100, b = 10, c = 50, d = 321)
    l = list()
    sortDictValue(aaa)

5.5 生成式和生成器

了解return和yield的区别

a = [x * x for x in range(1, 30) if x % 2 == 0]
print(type(a))
b = (x * x for x in range(1, 30) if x % 2 == 0)
print(type(b))
for i in b:
    pass
#     print(i)
def test(l):
    for i in l:
        yield i
        print("i = {0}".format(i))
m = test([1, 2, 3])
print(type(m))
for i in m:
    print(i)

5.6 装饰器的作用

def hello():
    print("Hello world")
def test():
    print("######start######")
    hello()
    print("######end######")
test()

01月29日【Python3 基础知识】

原文:http://blog.51cto.com/13542406/2073084

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!