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

    python的所有内置函数:

       

    Built-in Functions

       

    abs()

    dict()

    help()

    min()

    setattr()

    all()

    dir()

    hex()

    next()

    slice()

    any()

    divmod()

    id()

    object()

    sorted()

    ascii()

    enumerate()

    input()

    oct()

    staticmethod()
    

    bin()

    eval()

    int()

    open()

    str()

    bool()

    exec()

    isinstance()
    

    ord()

    sum()

    bytearray()

    filter()

    issubclass()
    

    pow()

    super()

    bytes()

    float()

    iter()

    print()

    tuple()

    callable()

    format()

    len()

    property()

    type()

    chr()

    frozenset()

    list()

    range()

    vars()

    classmethod()
    

    getattr()

    locals()

    repr()

    zip()

    compile()

    globals()

    map()

    reversed()

    __import__()
    

    complex()

    hasattr()

    max()

    round()

     

    delattr()

    hash()

    memoryview()
    

    set()

     

    abs(x):返回数字的绝对值;参数必须是整数或者浮点数,如果是复数则是返回复数的模(a+bj 的模 (a^2+b^2)的平方根 )

    1 >>> abs(2)
    2 2
    3 >>> abs(-4)
    4 4
    5 >>> abs(-4.567)
    6 4.567
    7 >>> abs(4-3j)
    8 5.0

    all(iterable): 如果可迭代对象的每一个元素都为True,则返回True,否则为False;但如果可迭代对象为空,则返回True。

    >>> all(range(3))
    False
    >>> all(range(1,3))
    True

    any(iterable):如果可迭代对象的元素有True,则返回True;如果可迭代对象为空,则为False。与all(iterable)相反。

    1 >>> any(range(3))
    2 True
    3 >>> any(range(0))
    4 False

    ascii(object):返回一个可打印的字符串对象,如果不在ascii码的字符则使用x、u、U显示。

    1 >>> ascii("df关键")
    2 "'df\u5173\u952e'"
    3 >>> ascii("asds")
    4 "'asds'"
    5 >>> ascii("/.<")
    6 "'/.<'"

    bin(x):把整数转成二进制字符串;

    1 >>> bin(10)
    2 '0b1010'

    callable(object):如果对象可以被调用,返回True;(注意,如果返回True,也可能调用不成功,但是如果返回False,一定无法调用。)这个函数此前在python3.0被移除,后来在python3.2回归了。

    dict():创建一个字典。

    dir(object):如果没有添加参数,将返回当前的局部变量,返回的是列表。如果传入参数,则返回该参数所有可用的方法。

    1 >>> dir("asd")
    2 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__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']
    View Code

    divmod(a,b):两个非复数的数字a,b进行整除和取模运算,返回整除的值(a//b)和取模的值(a%b)。

    1 >>> divmod(10,3)
    2 (3, 1)

    enumerate(iterable, start=0):enumerate方法产生一个可调用next方法的可迭代器,next方法后返回一个元组。

    1 >>> l = ["dog","cat","desk","people"]
    2 >>> f = enumerate(l,1)
    3 >>> f
    4 <enumerate object at 0x105ce43f0>
    5 >>> next(f) 
    6 (1, 'dog')
    7 >>> list(f)
    8 [(2, 'cat'), (3, 'desk'), (4, 'people')]

    reversed(seq):传入一个序列参数,返回一个序列的迭代器。

    1 >>> l  = [1,4,2,3,6,8]
    2 >>> l1 = reversed(l)
    3 >>> l1
    4 <list_reverseiterator object at 0x10cef6fd0>
    5 >>> list(l1)
    6 [8, 6, 3, 2, 4, 1]

    round(number[, ndigits ]):传入的数字,返回一个最近的整数。原则是:四舍六入五留偶。

     1 >>> round(4.4)
     2 4
     3 >>> round(4.6)
     4 5
     5 >>> round(4.5)
     6 4
     7 >>> round(4.5987,2)
     8 4.6
     9 >>> round(4.5987,3)
    10 4.599
    11 >>> round(4.5985,3)
    12 4.598

     sorted(iterable[, key][, reverse]):返回一个排序后的列表,默认是升序。

    1 >>> l
    2 [1, 4, 2, 3, 6, 8]
    3 >>> sorted(l)
    4 [1, 2, 3, 4, 6, 8]
    5 >>> l2 = "abdfrf"
    6 >>> sorted(l2)
    7 ['a', 'b', 'd', 'f', 'f', 'r']

    vars([object]):返回任何含有_dict_属性的类、实例对象等。

    >>> vars()
    {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'x': 1, 'e': None, 'l': [1, 4, 2, 3, 6, 8], 'l1': <list_reverseiterator object at 0x10cef6fd0>, 'l2': 'abdfrf'}

     zip(*iterables): 返回一个生成器,生成器会返回一个元组,放入迭代器的元素。

    1 >>> l
    2 [1, 4, 2, 3, 6, 8]
    3 >>> zip(l)
    4 <zip object at 0x10cf06048>
    5 >>> list(zip(l))
    6 [(1,), (4,), (2,), (3,), (6,), (8,)]
    1 >>> l2
    2 'abdfrf'
    3 >>> l
    4 [1, 4, 2, 3, 6, 8]
    5 >>> zip(l,l2)
    6 <zip object at 0x10cf06048>
    7 >>> list(zip(l,l2))
    8 [(1, 'a'), (4, 'b'), (2, 'd'), (3, 'f'), (6, 'r'), (8, 'f')]
  • 相关阅读:
    sql server不存在或访问被拒绝
    维护Sql Server中表的索引
    雷声大雨点小-参加江西省网站内容管理系统培训有感
    关于WINFORM中输入法的设置
    虚拟主机下asp.net 2.0的导航控件treeview,menu等出错。
    css背景图片不重复
    网上寻宝惊魂记
    一个不大注意的存储过程的小细节。
    css——之三行三列等高布局
    今天才发现ff不支持navigate。
  • 原文地址:https://www.cnblogs.com/greatkyle/p/6705863.html
Copyright © 2011-2022 走看看