本节主要内容:
python内建函数、基本函数、转换函数
内置函数 - 1
- 类型转换函数
1. abs()
2. max() min()
3. len()
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]'