• 2019年1月20日 周末 习题2


    b= 'oldboy%d%%'%(12,)
    print(b)

    打印12% 需要后面添加2个%

    19,简述对象和类的关系

    值是某类型,这个值就是这个类的对象

    20,all与any区别

    all是都空或者都真  则为true

    any是有真就是真

    21, 为啥用rb,用字节节省空间

    22.将‘老男孩’编码为utf-8 编码的字节类型

    print("老男孩".encode('utf-8'))
    
    
    print(bytes('老男孩','utf-8'))

    25.内置函数globals()和locals()作用

    全局变量和局部变量

    26 zip函数实现功能

    l1=['alex',22,33,44,55]
    l2=['is',22,33,44,55]
    l3=['good',22,33,44,55]
    l4=['guy',22,33,44,55]
    s=list(zip(l1,l2,l3,l4))
    print('_'.join(list(zip(l1,l2,l3,l4))[0]))
    print(s)
    
    a1,a2,a3,a4= zip(*s)#zip(*)为解压
    print(list(a1))

    alex_is_good_guy
    [('alex', 'is', 'good', 'guy'), (22, 22, 22, 22), (33, 33, 33, 33), (44, 44, 44, 44), (55, 55, 55, 55)]
    ['alex', 22, 33, 44, 55]

    name='sxj'
    def outer(func):
        name='123'#这里的name只不过是和别人重名
        func()
    
    def show():
        print(name) #这里的show就是打印sxj,因为这里的name就是'sxj'
    
    outer(show)

    33。递归计算阶乘

    def f(n):
        if n==1:
            return 1
        return n*f(n-1)#计算阶乘
    
    print(f(5))

    reduce函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。

    from functools import reduce
    ret=reduce(lambda x,y:x*y,[x for x in range(1,6)])
    print (ret)
    # [x for x in range(1,6)] 代表生成列表[1,2,3,4,5]

    35 用with实现同时打开两个文件(1读1写,并将读取内容写到写入模式文件中)

    with open('test3','r') as x,open('test4','w') as y:
        y.write(x.read())
  • 相关阅读:
    android 选择图片 剪裁 拍照 兼容所有版本的代码
    bitmap_createScaledBitmap的方法
    ViewPager的滑动监听事件
    android效果背景虚化
    Python socket超时
    Python 半开放socket
    Python绑定方法,未绑定方法,类方法,实例方法,静态方法
    Python类属性,实例属性
    Python偏函数
    Python filter,map,lambda,reduce,列表解析
  • 原文地址:https://www.cnblogs.com/python1988/p/10294505.html
Copyright © 2020-2023  润新知