zoukankan      html  css  js  c++  java
  • 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())
  • 相关阅读:
    非常奇怪的VS2005无法调试的问题
    自已写的“动易PowerEasy2006暴库工具”
    用VBA去除Excel工作表保护密码
    mapgis同arcmap之间的数据转换, 投影变换,误差校正,坐标,基础资料,教程的相关信息的汇集
    HDU You Are the One (dp)
    HDU4291 A Short problem
    Mondriaan's Dream(poj2411)
    HDU 4277 USACO ORZ
    求欧拉回路的路径(usaco3.3Riding the Fences)
    poj2923 (状态压缩01背包)
  • 原文地址:https://www.cnblogs.com/python1988/p/10294505.html
Copyright © 2011-2022 走看看