14-python_内建函数-基本和转换

内置函数 - 1
 - 常用函数   
   - abs()
   - max() min()
   - len()
   - divmod()
   - pow()
   - round()
   - callable()
   - isinstance()
   - cmp()
   - range()
   - xrange()
 - 类型转换函数
   - type()
   - int()
   - long()
   - float()
   - complex()
   - str()
   - list()
   - tuple()
   - hex()
   - oct()
   - chr()
   - ord()

1. abs()

    >>> help(abs)
    Help on built-in function abs in module __builtin__:
    abs(...)
        abs(number) -> number
        Return the absolute value of the argument.
    >>> abs(-10)
    10

2. max() min()

    >>> help(max)
    Help on built-in function max in module __builtin__:
    max(...)
        max(iterable[, key=func]) -> value
        max(a, b, c, ...[, key=func]) -> value
        With a single iterable argument, return its largest item.
        With two or more arguments, return the largest argument.
    >>> max(1,2,3)
    3
    >>> max( [1,2,3] )
    3
    >>> max( "123" )
    '3'

3. len()

    >>> help( len )
    Help on built-in function len in module __builtin__:
    len(...)
        len(object) -> integer
        Return the number of items of a sequence or mapping.
    >>> len( "123" )
    3

4. divmod()

    >>> help(divmod)
    Help on built-in function divmod in module __builtin__:

    divmod(...)
        divmod(x, y) -> (quotient, remainder)

        Return the tuple ((x-x%y)/y, x%y).  Invariant: div*y + mod == x.

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

5. pow()

    >>> help( pow )
    Help on built-in function pow in module __builtin__:

    pow(...)
        pow(x, y[, z]) -> number

        With two arguments, equivalent to x**y.  With three arguments,
        equivalent to (x**y) % z, but may be more efficient (e.g. for longs).

    >>> pow( 2, 3 )
    8
    >>> pow( 2, 3, 3 )
    2

6. round()

    >>> help( round )
    Help on built-in function round in module __builtin__:

    round(...)
        round(number[, ndigits]) -> floating point number

        Round a number to a given precision in decimal digits (default 0 digits).
        This always returns a floating point number.  Precision may be negative.

    >>> round(1.4)
    1.0
    >>> round(1.5)
    2.0

7. callable

    >>> help( callable )
    Help on built-in function callable in module __builtin__:

    callable(...)
        callable(object) -> bool

        Return whether the object is callable (i.e., some kind of function).
        Note that classes are callable, as are instances with a __call__() method.

    >>> callable( "a" )
    False
    >>> callable( min )
    True

8. isinstance()

    >>> help( isinstance )
    Help on built-in function isinstance in module __builtin__:

    isinstance(...)
        isinstance(object, class-or-type-or-tuple) -> bool

        Return whether an object is an instance of a class or of a subclass thereof.
        With a type as second argument, return whether that is the object's type.
        The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
        isinstance(x, A) or isinstance(x, B) or ... (etc.).

    >>> isinstance("a", str)
    True
    >>> isinstance(123, int)
    True

9. cmp()

    >>> help( cmp )
    Help on built-in function cmp in module __builtin__:

    cmp(...)
        cmp(x, y) -> integer

        Return negative if x<y, zero if x==y, positive if x>y.

    >>> cmp( 1, 2 )
    -1
    >>> cmp( 2, 1 )
    1
    >>> cmp( 2, 2 )
    0

10. range()

    >>> help( range )
    Help on built-in function range in module __builtin__:

    range(...)
        range(stop) -> list of integers
        range(start, stop[, step]) -> list of integers

        Return a list containing an arithmetic progression of integers.
        range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
        When step is given, it specifies the increment (or decrement).
        For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
        These are exactly the valid indices for a list of 4 elements.

    >>> range(3)
    [0, 1, 2]
    >>> range(1, 3)
    [1, 2]
    >>> range(1, 5, 2)
    [1, 3]

11. xrange()

    >>> help( xrange )
    Help on class xrange in module __builtin__:

    class xrange(object)
     |  xrange(stop) -> xrange object
     |  xrange(start, stop[, step]) -> xrange object
     |
     |  Like range(), but instead of returning a list, returns an object that
     |  generates the numbers in the range on demand.  For looping, this is
     |  slightly faster than range() and more memory efficient.
     |
    >>> xrange(3)
    xrange(3)
    >>> for x in xrange(3) :
    ...     print x
    ...
    0
    1
    2

12. int()
    >>> help( int )
    Help on class int in module __builtin__:

    class int(object)
     |  int(x=0) -> int or long
     |  int(x, base=10) -> int or long
     |
     |  Convert a number or string to an integer, or return 0 if no arguments
     |  are given.  If x is floating point, the conversion truncates towards zero.
     |  If x is outside the integer range, the function returns a long instead.
     |
     |  If x is not a number or if base is given, then x must be a string or
     |  Unicode object representing an integer literal in the given base.  The
     |  literal can be preceded by '+' or '-' and be surrounded by whitespace.
     |  The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
     |  interpret the base from the string as an integer literal.
     |  >>> int('0b100', base=0)
    >>> int("110")
    110
    >>> int("110", 10)
    110
    >>> int("110", 2)
    6
    >>> int("110", 8)
    72

13. str()

    >>> help( str )
    Help on class str in module __builtin__:

    class str(basestring)
     |  str(object='') -> string
     |
     |  Return a nice string representation of the object.
     |  If the argument is a string, the return value is the same object.
    >>> str(123)
    '123'
    >>> str([1,2])
    '[1, 2]'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值