zoukankan      html  css  js  c++  java
  • Python内置方法

      顾名思义,内置方法就是你打开解释器就可以直接用的方法,比如len()方法,abs()方法等。

      由于比较多,我就不一一说明了,以实例的方式展示给大家:

      内置方法第一部分:

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    # write by congcong
    print(abs(5-9)) # 输出 :4, abs()表示绝对值
    
    a = ['qw','hy','cc']
    print(dict(a)) #输出:{'q': 'w', 'h': 'y', 'c': 'c'} ,dict()转字典
    
    print(help(all)) #help为帮助
    
    a = [1,2,0]
    b = []
    print(all(a),all(b))# 输出:False True ,当all()括号内元素都不为0或元素为空时为真,其余均为假
    
    c = [0]
    print(any(a),any(b),any(c)) # 输出:True False False,当括号内元素有不为0的存在时,为真
    print(help(any))
    
    print(dir()) # 输出已定义的变量
    
    print(hex(12)) # 输出:0xc,转成16进制
    print(oct(12)) # 输出:0o14,转成8进制
    print(bin(12)) # 输出:0b1100,转成二进制
    
    s = 'asd希望'
    print(ascii(s)) # 输出:'asdu5e0cu671b',转成ascii码表示
    
    d = {1:'q',2:'w',3:'e'}
    for index,i in enumerate(d): # enumerate枚举,可列举索引
        print(index,d[i])
    
    print(divmod(9,4)) # 输出:(2,1),divmod()表示求括号两元素的商和余数
    
    print(id(d)) # 输出:1885634510712 ,求变量的内存地址
    
    lis = [1,3,5,6,2,51,31]
    print(sorted(lis)) # 输出:[1, 2, 3, 5, 6, 31, 51] ,按ascii码表排列
    
    f = '3-2*1'
    print(eval(f)) #输出:1,eval()方法是将字符串按照规定方法转成代码,只能处理单行
    print(exec(f)) #输出:None,无法处理单行
    code = '''
    def foo():
        print('it runs!')
        return 666
    foo()
    '''
    #res = eval(code) #会出错,无法处理多行
    res = exec(code) #字符串转代码,可执行多行,不报错,但无法取出,有返回值(None)
    print(res) #
    
    print(ord('a')) #输出:97,输出字符在ascii表中的位置
    print(chr(97)) #输出:a,输出数字对应的字符位置
    
    # bytearray 字节数组
    s = 'abcd宝贝'
    #s[0] = 65  不能直接修改
    print(s[0],id(s)) #字符串可切片,但不可直接修改,s的内存地址是 2274693312656
    s =s.encode('utf-8')
    s = bytearray(s)
    print(s,id(s)) #输出:bytearray(b'abcdxe5xaex9dxe8xb4x9d') 2739958153320
    s[0] = 65
    print(s) #输出:bytearray(b'Abcdxe5xaex9dxe8xb4x9d') ,修改成功
    print(id(s)) #输出:2739958153320 ,字符串位置没有改变
    
    # map
    print(list(map(lambda x:x*x,[1,2,3,4]))) #输出:[1,4,9,16]
    
    #filter 过滤
    print(list(filter(lambda x:x>3,[1,2,3,4,5]))) #输出:[4,5]
    
    #reduce 累加
    import functools
    print(functools.reduce(lambda x,y:x+y,[1,2,3,45,6])) #输出:57
    print(functools.reduce(lambda x,y:x+y,[1,2,3,45,6],1)) #输出:58
    
    # pow()括号内传两个参数时x,y时,则x**y;传三个参数时x,y,z时,则x**y%z
    print(pow(2,4),pow(2,4,3)) #输出:16  1
    View Code

      其中比较难理解的我单独再说明下:

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    
    #eval()和exec()方法
    
    f = '3-2*1'
    print(eval(f)) #输出:1,eval()方法是将字符串按照规定方法转成代码,只能处理单行
    print(exec(f)) #输出:None,无法处理单行
    code = '''
    def foo():
        print('it runs!')
        return 666
    foo()
    '''
    #res = eval(code) #会出错,无法处理多行
    res = exec(code) #字符串转代码,可执行多行,不报错,但无法取出,有返回值(None)
    print(res)
    
    # bytearray 字节数组,就是先将字符串转成二进制字符串 s = 'abcd宝贝' #s[0] = 65 不能直接修改 print(s[0],id(s)) #字符串可切片,但不可直接修改,s的内存地址是 2274693312656 s =s.encode('utf-8') # 编码成二进制字符串
    print(s,id(s))  # b'abcdxe5xaex9dxe8xb4x9d' 1988565108560
    s = bytearray(s) # 二进制字符串转成字节数组 
    print(s,id(s)) #输出:bytearray(b'abcdxe5xaex9dxe8xb4x9d') 2739958153320 

    s[0] = 65

    print(s) #输出:bytearray(b'Abcdxe5xaex9dxe8xb4x9d') ,修改成功

    print(id(s)) #输出:2739958153320 ,字符串位置没有改变

      内置方法第二部分:

    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-
    # write by congcong
    
    #print  sep: str = ' ', end: str = '
    '
    print("life is hard,but we can't give up!",end='[]')
            # 输出:life is hard,but we can't give up![],end默认是‘
    ’
    print('life is hard','but we must work hard!',sep='->')
            #输出:life is hard->but we must work hard!,sep默认是‘空格’
    msg = '别让自己活得像个鬼'
    f = open('print_to_file','w',encoding='utf-8')
    print(msg,'我是江小白',sep='||',end='',file=f)
    print(msg,'我不是江小白',sep='||',end='',file=f)
    
    #collable 判断对象是否可调用
    a = [1,2,3,4,5]
    print(callable(f),callable(a)) #False False
    print(callable(msg)) #False
    
    # vars()打印已定义的变量和值
    print(vars())
    # dir() 打印已定义的变量
    print(dir())
    
    print(locals()) #打印函数的局部变量
    
    print(globals()) #打印函数的全局变量
    
    #repr 显示形式变为字符串
    
    #zip
    a = [1,2,3,4]
    b = ['a','b','c']
    print(list(zip(a,b))) #输出:[(1, 'a'), (2, 'b'), (3, 'c')]
    
    #reversed() 默认翻转
    
    # complex()转为复数
    print(complex(2,5)) # 输出:(2+5j)
    
    # round()
    print(round(1,32324)) #输出:1
    print(round(1.32432,3)) #输出:1.324
    
    #hash 字符串转数字
    print(hash('zxc')) #-943184633069281759
    
    
    #set列表转集合,去重
    a = [1,2,3,2,4,4,5]
    print(set(a)) # 输出:{1, 2, 3, 4, 5}
    View Code

      补充

    
    
    #!/usr/bin/env python3
    #-*- coding:utf-8 -*-

    #
    slice 切片 a = range(20) pattern = slice(3,8,2) for i in a[pattern]: #等于a[3:8:2] print(i) # print sep: str = ' ', end: str = ' ' print("life is hard,but we can't give up!",end='[]') # 输出:life is hard,but we can't give up![],end默认是‘ ’ print('life is hard','but we must work hard!',sep='->') #输出:life is hard->but we must work hard!,sep默认是‘空格’ msg = '别让自己活得像个鬼' f = open('print_to_file','w',encoding='utf-8') print(msg,'我是江小白',sep='||',end='',file=f) print(msg,'我不是江小白',sep='||',end='',file=f)

      

  • 相关阅读:
    TinyOS功率编程指南
    深度学习入门资料
    通信常识
    CTF入门
    前端开发工具之服务器选择
    Spring
    NoSQL -- MongoDB
    NoSQL -- Redis
    mysql alter table修改表结构添加多个字段的几个写法
    gongle 访问助手安装
  • 原文地址:https://www.cnblogs.com/schut/p/8419026.html
Copyright © 2011-2022 走看看