01月29日【Python3 基础知识】

本文介绍了Python3中关于匿名函数、字典排序、生成器、装饰器等高级特性。通过实例讲解了lambda表达式的使用、如何对字典按值排序、生成器与迭代器的区别以及装饰器的作用。

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()

转载于:https://blog.51cto.com/13542406/2073084

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值