zoukankan      html  css  js  c++  java
  • python 内置函数

    1. all([-1, 0, 1]) #判断是否全是不等于0的数 

    2. any([-1,0,1])  # 有一个数不为0 返回真 any([]) 返回假

    3.ascii([1, 2, '开外挂']) #进行ascii 转换 

    4.bin(1) #十进制转换为二进制

    5.bool(1) #判断是否为真 

    6. a = bytes('abcde', encoding = 'utf-8')  #abcde 的代码是 utf-8编码的 转化为字节形式

    7.

    b = bytearray('abcde', encoding = 'utf-8')  #可以进行修改的字节形式

    print(b[1])

    b[1] = 100
    print(b)   

    8.def sayhi():pass

    callable(sayhi)  # 表示可以访问的函数 

    9. chr(98)  # 把 数字转换为ascii

    10.ord('d')  # 把ascii 转换为数字 

    11.code = 'for i in range(10):print(i)'

    exec(code)  # 把字母转化成可执行的 代码 

    12.dir 查看内部方法 

    13.divmod(5, 2) #相除返回余数

    14.calc = lambda n:print(n) 

    calc(5)

    res =filter(lambda n : n>5, range(10))  #过滤器

    res = [lambda n:n*2 for n in range(10)]

    for i in res:

         print(i)

    15 reduce 

    import functools 

    res = functools.reduce(lambda x,y : x+y, range(1, 10))

    print(res)

    16.frozenset([1, 2, 3, 4, 5]) #变成不可变列表 

    17.globals()  #整个程序的变量 

    18. hash('alex') #生成一个特有数学编号

    19 hex(255) #转化为16进制

    20.locals()

     def test():

            a = 333

            print(locals())   #找到局部程序的变量 

    test()

    21 oct(8) #转换成8进制

    22.pow(3, 5) # 多少次方

    23 repr # 转换成一个字符串

    c = 1 

    repr(c)

    24.reversed() 反转

    25.round(1.3333, 2) #四舍五入 保留两位自然数 

    26 slice #切片

    d = range(20)

    d[slice(2, 5)]

    27.sorted()

    a = {6:2, 8:0, 1:4, -5:6, 99:11, 4:22}

    print(sorted(a.items(), key = lambda x : x[1]) # 以value() 进行排序 

    28.zip 生成拉链 一一对应

    a = [1 ,2 , 3, 4, 5, 6]

    b = ['a', 'b', 'c', 'd']

    h = zip(a, b) 

    for i in h:

         print(i)

  • 相关阅读:
    Scrum 冲刺博客第五篇
    Scrum 冲刺博客第四篇
    Scrum 冲刺博客第三篇
    ajax send()
    form action中get post传递参数的问题
    struts2 iterator中if标签的使用
    表格内容自动换行
    从js向Action传中文参数出现乱码问题的解决方法
    java开发环境搭建
    Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/9048652.html
Copyright © 2011-2022 走看看