python常用的内建函数、基本函数与转换函数

发布时间:2020-03-10编辑:脚本学堂
本文介绍下,python中常用的基本函数,以及基本函数与转换函数等,包括abs()、max()、min()、len()、round()等。有需要的朋友参考下。

本节主要内容:
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]'