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

    # list dict tuple str int float set
    # print input type id len

     以上这些都属于内置函数

    1、max()  

    print(max(range(1,28)))  #输出结果:27

    2、min()

    print(min(range(28)))  #输出结果:0

    3、sum()

    print(sum(range(1,101)))  #输出结果:5050

    4、dir()  # 输出msg具有的方法

    msg = 'hello'
    print(dir(msg))

    运行结果:

    D:work_softMiniconda3python.exe F:/python-besttest/day05/aaa.py
    ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
    
    Process finished with exit code 0

    5、sorted()排序  

    res = sorted([2,3,1,2,3],reverse=True)  #加上reverse就是降序排列了
    print(res)

    6、eval()

    如果转json的话,尽量不用eval(),容易报错。转json的话,还是尽量用json模块

    res = eval('[1,2,3,4,5]')#执行python代码,只能执行简单的python代码,1+1 a=1
    print(res)

    例子:

    # f = open('a.json',encoding='utf-8')
    
    # result = eval(f.read()) #用来动态执行python代码的,简单的
    # print(result)

    7、exec()的用法

    f = open('code',encoding='utf-8')
    code = f.read()
    exec(code)# 执行python代码的

    例子:

    s='''
    111112222333
    '''
    exec(s)#用来动态执行python代码的

    8、枚举

    stus = ['yyy','hhh','xxx','www']
    for i in range(len(stus)):
        print(i,stus[i])

    枚举:

    stus = ['yyy','hhh','xxx','www']
    for index,s in enumerate(stus):
        print(index,s)

    9、多个list压缩到一起

    stus = ['yyy','hhh','xxx','www']
    sex = ['','','','']
    age = [1,2]
    
    for name,se,ag in zip(stus,sex,age): #多个list压缩到一起
        print(name,se,ag)

     10、all()

    print(all([1, 2, 3, 4])) # 判断可迭代的对象里面的值是否都为真

    print(all([1,2,3,4,False]))#判断可迭代的对象里面的值是否都为真

    11、any()

     

    # print(any([0, 1, 2, 3, 4])) # 判断可迭代的对象里面的值是否有一个为真

    12、round()

    # print(round(11.11, 2)) # 取几位小数

    13、

    # print(filter(lambda x: x > 5, [12, 3, 12, 2, 1, 2, 35])) # 把后面的迭代对象根据前面的方法筛选

    14、

     

    # print(map(lambda x: x > 5, [1, 2, 3, 4, 5, 6]))

     

     15、

    # print(bin(10)) # 十进制转二进制

    16、

    # print(chr(10)) # 打印数字对应的ascii

    17、

    # print(ord('b')) # 打印字符串对应的ascii码

    18、

    # print(dir(1)) # 打印传入对象的可调用方法

    19、

    # print(globals()) # 返回程序内所有的变量,返回的是一个字典

    20、

     

    # print(locals()) # 返回局部变量

    21、

     

    # print(hex(111)) # 数字转成16进制

    22、

     

    # print(oct(111)) # 把数字转换成8进制

     

     23、filter #过滤 

    map # 

    不用它俩也没问题,用它俩就是会节省代码

     

    def oushu(number):
        if number%2==0:
            return True
    
    l = range(1,11)
    l2 = []
    for i in l:
        if oushu(i):
            l2.append(i)
    
    result = list(filter(oushu,l))  # 这一行代码和上面五行代码是一模一样的效果
    result2 = list(map(oushu,l))
    print(l2)
    print(result)
    print(result2)
    
    
    result3 = list(map(str,range(1,101)))
    
    # print(result3)
    #filter会自动循环你传给他的list,然后把list里面的
    #每一个元素传给指定的函数,如果这个函数结果返回的是
    #true,那么就保留这个元素
     
    #filter会自动循环你传给他的list,然后把list里面的
    #每一个元素传给指定的函数,把这个函数返回的结果保存下来
     
     map保存的是函数的返回值,把函数返回的结果保存下来,不会过滤
  • 相关阅读:
    EasyUI项目驱动学习
    给你一个能生成1到5随机数的函数,用它写一个函数生成1到7的随机数。 (即,使用函数rand5()来实现函数rand7())
    python手记(42)
    Succession
    第一节,学习cocos2d-x的前期准备
    DOM与JavaScript、jQuery之间的关系
    HTML5 精灵8方向移动+背景滚动+音效播放+鼠标事件响应
    Oracle多行记录合并自定义函数
    MSP430F5438 I2C学习笔记——AT24C02
    OPENCV 常用函数
  • 原文地址:https://www.cnblogs.com/Noul/p/9278718.html
Copyright © 2011-2022 走看看