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

    可以使用dir(__builtins__)查看所有的内置函数

    ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError',
    'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError',
    'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError',
    'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning',
    'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError',
    'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError',
    'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning',
    'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
    'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
    'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError',
    '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all',
    'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright',
    'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset',
    'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len',
    'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',
    'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple',
    'type', 'vars', 'zip']

    1.type() :查看数据类型

    2.sum():求最大值

    3.min()、max():求最小值、最大值

    4.int()、float()、bool()、str()、list()、tuple()、dict()、set() :数据类型转化(整型、浮点型、布尔型、字符串、列表、元组、字典(key-value)、集合(去重))

    5.sort()、sorted():排序(sort()是修改原列表,没有返回值;sorted()是排序一个新的列表作为返回值,原列表不会发生变化)

    6.zip():用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象。

    如果各个可迭代对象的元素个数不一致,则返回的对象长度与最短的可迭代对象相同。

    利用 * 号操作符,与zip相反,进行解压。

    >>> a = [1,2,3]
    >>> b = [4,5,6]
    >>> c = [7,8,9,10,11,12]
    >>> z = zip(a,b,c)
    >>> z
    <zip object at 0x0000000002B86408> #返回的是一个对象
    >>> list(z)
    [(1, 4, 7), (2, 5, 8), (3, 6, 9)] #使用list()函数转换为列表
    >>> list(zip(a,c))
    [(1, 7), (2, 8), (3, 9)]
    
    >>> z = zip(a,b)
    >>>> list(zip(*z))    #解压也使用list进行转换
    [(1, 2, 3), (4, 5, 6)]

    7.range()函数可创建一个整数列表,一般用在 for 循环中

    >>>range(10)        # 从 0 开始到 10,取头不取尾
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> range(1, 11)     # 从 1 开始到 11
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> range(0, 30, 5)  # 步长为 5
    [0, 5, 10, 15, 20, 25]
    >>> range(0, -10, -1) # 负数
    [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
    >>> range(0)
    []
    >>> range(1, 0)
    []

    8.reversed()函数是返回一个翻转的迭代器

    # 字符串
    seqString = 'Runoob'
    print(list(reversed(seqString)))
    # 元组
    seqTuple = ('R', 'u', 'n', 'o', 'o', 'b')
    print(list(reversed(seqTuple)))
    # range
    seqRange = range(5, 9)
    print(list(reversed(seqRange)))
    # 列表
    seqList = [1, 2, 4, 3, 5]
    print(list(reversed(seqList)))

    9.open() 、close() 打开、关闭文件,一般连在一起使用

    10.print()打印结果

    11.help()函数用于查看函数或模块用途的详细说明。

    12.input():标准输入数据,返回为 string 类型

    13.len() 返回对象(字符、列表、元组等)长度或项目个数。

    14.enumerate() 枚举 用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

    seq = ['one', 'two', 'three']
    for k  in enumerate(seq): #结果是元组
        print(k)
    '''
    (0, 'one')
    (1, 'two')
    (2, 'three')
    '''
    seq = ['one', 'two', 'three']
    for k,v  in enumerate(seq): #元组的解包,
        print(k,v)
    '''
    0 one
    1 two
    2 three
    '''

    15.eval()函数用来执行一个字符串表达式,并返回表达式的值。

    a = 7
    print(eval('a * 8'))
    print(eval('3+8'))
    '''
    56
    11
    '''

    16.format() 格式化字符串的函数 str.format()

  • 相关阅读:
    你欠我的幸福,怎么弥补
    爱,请你走开
    一生为你
    爱你到底
    粒子滤波简介(转载)
    关于小波变换和Gabor变换的一些知识!
    基于Opencv的MeanShift跟踪算法实现
    opencv学习网页
    基于OpenCV库的Gabor滤波器的实现
    Mean Shift算法(CamShift)
  • 原文地址:https://www.cnblogs.com/ananmy/p/12856536.html
Copyright © 2011-2022 走看看