zoukankan      html  css  js  c++  java
  • Python 基础

    内建函数共68个

    1. 数学运算

    Function   中文释义 官方解释
    abs(x) 返回数字x的绝对值 Return the absolute value of a number.  The argument may be an integer or a floating point number.  If the argument is a complex number, its magnitude is returned.
    all(iterable)

    参数为可迭代对象

    返回为True如果所有元素都判断为true     

    Return True if all elements of the iterable are true (or if the iterable is empty).
    any(iterable)       

    参数为可迭代对象

    返回为True 如果任意元素判断为true

    Return True if any element of the iterable is true.  If the iterable is empty, return False
    ascii(object)                       和repr()类似,返回为字符串,非ascii码的转为以x,u或者U的ascii码
    >>> ascii([1,2,"开外挂"])
    "[1, 2, '\u5f00\u5916\u6302']"
    ascii(object)

    As repr(), return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr() using x, u or U escapes.  This generates a string similar to that returned by repr() in Python 2.

    bin(x)   将数字从十进制转为二进制  
    >>> bin(1)
    '0b1'
    >>> bin(10)
    '0b1010'
    >>> bin(255)
    '0b11111111'
    bin(x)

    Convert an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.

     bool([x])    布尔值判断 Return a Boolean value, i.e. one of True or Falsex is converted using the standard truth testing procedure.  If x is false or omitted, this returns False; otherwise it returns True.  The bool class is a subclass of int 
    bytes([source[, [encoding[, errors]]])  返回一个字符串的字节表示,不可修改
    a = bytes("abcde", encoding='utf-8')   # 将二进制改为一个字符串,不能修改;字节更是不能修改。任何修改都是创建一个新的字符串
    print(a.capitalize(),a)    # a的返回值仍旧为b'abcde';同时, 注意string是不能改动的,任何改动都会创建一个新的string
    bytes()

    Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x < 256. Bytes objects can also be created with literals, see String and Bytes literals

    bytearray([source[, [encoding[, errors]]])  返回一个数组的字节表示,可修改元素  
    b = bytearray("abcde", encoding='utf-8')  # 将二进制变为一个数组,可以修改
    print(b[0])   # 返回值为 97; 打印了a的ascii码
    b[1] = 100   # 100是ascii码
    print(b)    # 返回值:bytearray(b'adcde')
    bytearray()

    Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Bytearray Operations.

    callable(object) 判断是否可调用。函数和类都可调用 Return True if the object argument appears callable, False if not
    chr(i) 返回ascii码第i位的 字符串 Return the string representing a character whose Unicode code point is the integer i.For example, chr(97) returns the string 'a', while chr(8364) returns the string '€'. This is the inverse of ord().
    *classmethod(function) <后期详述>

    Return a class method for function.A class method receives the class as implicit first argument, just like an instance method receives the instance. 

    compile()

    把底层编码进行编译。

    几乎用不到,可以忘记。 

     
    # compile() # 相当于import fib的模块。可以实现动态导入,尤其远程传入代码。 
    code = """
    def fib(max):
        # Fibonacci 斐波拉契数列
        n, a, b = 0, 0, 1
        while n < max:
            yield b
            a, b = b, a + b
            n = n + 1
        return "done"
    """
    py_obj = compile(code,"err.log","exec")
    exec(py_obj) # 执行fib(max)
    compile
    *delattr(object,name)    <后期详述> This is a relative of setattr(). The arguments are an object and a string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For example, delattr(x, 'foobar') is equivalent to del x.foobar.
    *dir([object])  没有参数,返回当前文档位置;有参数,返回对象下的各种功能文件
    >>> import struct
    >>> dir()   # show the names in the module namespace
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    View Code

    Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object

    divmod(a,b) a除以b, 返回(商,余数) Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division.
    *enumerate(iterable, start=0) 循环打印,取有序序列下标作为计数  Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration.
    eval(expr, globals=none, locals=none) 将字符串str当成有效的表达式来求值并返回计算结果(value)。 The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object.
    exec(object[, globals[, locals]]) 来执行储存在字符串或文件中的Python语句。尤其想for 循环。 This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). [1] If it is a code object, it is simply executed. In all cases, the code that’s executed is expected to be valid as file input (see the section “File input” in the Reference Manual). Be aware that the return and yield statements may not be used outside of function definitions even within the context of code passed to the exec() function. The return value is None.
    filter(function, iterable) 筛选满足func (一般是lambda)   
    *map(function, iterable, ...)    
     class float([x])    
         
    *getattr <后期详述>  
         

    HASH(散列)原理的解释

    散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。然后如何在散列表中去搜索关键码值,就是算法决定的。

    Reference:

    官方 buildin function:  https://docs.python.org/3/library/functions.html?highlight=built#ascii  

    python内置函数详解(这个系列很详细): http://www.cnblogs.com/sesshoumaru/p/6140987.html 

  • 相关阅读:
    基于 OAI 部署私有的 4G EPS
    Ubuntu Snap 简述
    OAI SDR LTE 基站部署
    企业文化二三谈
    OpenStack 的 SR-IOV 虚拟机热迁移
    在 ThinkPad E470 上安装 Ubuntu 16.04 无线网卡驱动
    读写可编程 SIM/USIM 卡
    4G LTE/EPC UE 的附着与去附着
    4G EPS 的网络协议栈
    Java- 类型转换
  • 原文地址:https://www.cnblogs.com/lg100lg100/p/7306952.html
Copyright © 2011-2022 走看看