zoukankan      html  css  js  c++  java
  • day12-2 内置方法

    1.abs(x) 取绝对值

    2.all(Iterable)  如果可迭代对象都为真,为空的时候也为真,那么返回True

    3.any(Iterable)  可迭代对象任意一个为真,,返回True,为空的时候返回False

    4.ascii(object)  把一个数据对象变为可打印的字符串形式bool

    1 a = ([1,2,3,'sadas'])
    2 
    3 print(a,type(a),[a])
    4 [1, 2, 3, 'sadas'] <class 'list'> [[1, 2, 3, 'sadas']]

    5.bin() 把一个整数变为二进制

    1 bin(52)
    2 Out[4]: '0b110100'

    6.bool() 判断真假,

    bool(5)
    Out[9]: True
    
    bool([])
    Out[10]: False

    7.bytes() 把数据转换为字节类型,不能修改

    a = bytes('ads',encoding = 'utf-8')
    
    a
    Out[17]: b'ads'
    
    print(a.capitlize(),a)
    Traceback (most recent call last):
    
      File "<ipython-input-18-1cef42b3d8b6>", line 1, in <module>
        print(a.capitlize(),a)
    
    AttributeError: 'bytes' object has no attribute 'capitlize'

    8.bytearray  。可以修改

    a = bytearray('ads',encoding = 'utf-8')
    chr(97)
    Out[27]: 'a'
    
    
    
    
    
    a[0]
    Out[21]: 97
    a[1] = 10
    
    a[2]
    Out[24]: 115
    
    a[1]
    Out[25]: 10
    
    a
    Out[26]: bytearray(b'a
    s')

    9.callable  判断是否可调用 。后面可以加()的可以调用

    10.chr。 输入数字,返回对应位置的ascii码

    chr(97)
    Out[27]: 'a'

    11.输入字符,转换为ascii。

    1 print(ord('s'))
    2 #115

    12.compile 可以把一大段字符串转换为可执行的代码

    简单的数据类型,加减乘除,直接接使用exec(‘str’)同样可以执行

    复杂的则要compile转换

    compile(code,'','exec')
    Out[37]: <code object <module> at 0x000000000A3330C0, file "", line 1>
    
    c = compile(code,'','exec')  #中间的‘’可以写一个文件名,编译的时候如果出错,会把信息打在这个文件了
    
    exec(c)
    #0
    #1
    #2
    #3
    #4
    #5
    #6
    #7
    #8
    #9  

    13.dir 可以查对象可以使用的方法

    14.divmod,得出商和余数

    divmod(5,2)
    Out[40]: (2, 1)

    15.enumerate 在前面加上编号

    (0, 's')
    (1, 'A')
    (2, 'c')
    (3, 'd')
  • 相关阅读:
    HDU 2888 Check Corners (模板题)【二维RMQ】
    POJ 3264 Balanced Lineup(模板题)【RMQ】
    poj 3368 Frequent values(经典)【RMQ】
    SPOJ RPLN (模板题)(ST算法)【RMQ】
    UVA 796 Critical Links(模板题)(无向图求桥)
    UVA 315 Network (模板题)(无向图求割点)
    POJ 2029 Get Many Persimmon Trees (模板题)【二维树状数组】
    poj 3067 Japan 【树状数组】
    POJ 2481 Cows 【树状数组】
    POJ 1195 Mobile phones【二维树状数组】
  • 原文地址:https://www.cnblogs.com/yfjly/p/9790958.html
Copyright © 2011-2022 走看看