Python内置函数(built-in function)

这篇博客详细介绍了Python的内置函数,包括abs()、all()、any()、ascii()、bin()等,涵盖了数值处理、布尔判断、类型转换等多个方面。通过示例展示了如何使用这些函数,帮助理解它们的功能和用法。
摘要由CSDN通过智能技术生成

Python 内置函数

abs(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.

  返回一个数的绝对值。参数可以是整形或资格浮点数。如果参数是复数,则该复数的模(|z|=√a²+b²,)被返回。

             For example:     

        print abs(1)      # 1
        print abs(-1)     # 1
        print abs(1.0)    # 1.0
        print abs(-10)    # 1.0
        print abs(3+4j)   # 5    

all(iterable)

    Return True if all elements of the iterable are true (or if the iterableis empty). Equivalent to:

    如果可迭代对象的所有元素都为True或者该对象为空则返回真, 否则返回False。相当于:

        def all(iterable):
            for element in iterable:
                if not element:
                 return False
            return True

any(iterable)

    Return True if any element of the iterable are true, if the iterable is empty(all elements of the iterable are false), return False. Equivalent to:

    如果可迭代对象的任一元素为true则返回True, 如果可迭代对象的所有元素为false或者该对象为空则返回False。相当于:

        def any(iterable):
            for element in iterable:
                if element:
                 return True
            return False

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.

    类似于repr(),返回一个对象的可打印的字符串表示,在字符串中的转义非ASCI字符通过repr()函数使用\x, \u, \U转义字符返回,产生的字符串和Python2中的repr()函数

    类似。(换句话说就是将对象转化为一个ASCII字符表示的字符串。)

  ascii("哇哈哈abc")  # "'\\u54c7\\u54c8\\u54c8abc'"
  repr("哇哈哈abc")   # "'哇哈哈abc'"

bin(x)

    Convert an integer number to a binary string. 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.
    把一个整型数据转化为该整数的二进制字符串表示. 结果是一个有效的Python表达式.如果参数不是一个有效的整型对象,那么该对象必须有一个__index__()方法来返回一 
   个整型值。
  class Test:
      __init__(self, num):
        
          self.num = num

      __index__(self):

          return self.num

  bin(10)        # '0b1010'
  bin(A(10))     # '0b1010'

bool([x])

    Convert a value to a Boolean, using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is
    a subclass of int (see Numeric Types — int, float, complex). Class bool cannot be subclassed further. Its only instances are False and True (see Boolean Values).

    将一个值使用标准的真值测试过程转化为Boolean类型. 如果x为false或者x为缺省值, 返回False,否则返回True.另外bool是一个int类型的子类.bool不能被进一步的继承.
  它只有两个实例True和False(详见Boolean值).

  bool(1)   # True
  bool(0)   # False
  bool(-1)  # True
  bool()    # False

bytearray([source[, encoding[, errors]]])

    Return a new array of bytes. The bytearray type 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.

    The optional source parameter can be used to initialize the array in a few different ways:

        If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode().

        If it is an integer, the array will have that size and will be initialized with null bytes.

        If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to in

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值