文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>python: built-in method cont2.

python: built-in method cont2.

时间:2008-09-29  来源:chunliny

20. 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. Class bool cannot be subclassed further. Its only instances are False and True.

New in version 2.2.1. Changed in version 2.3: If no argument is given, this function returns False.

>>> bool(1)
True
>>> bool([])
False
>>> bool("")
False
>>> bool(-1)
True
>>> bool("dhd")
True


21. cmp( x, y)
Compare the two objects x and y and return an integer according to the outcome. The return value is negative if x < y, zero if x == y and strictly positive if x > y.

>>> cmp(1,1)
0
>>> cmp(1,2)
-1
>>> cmp('ab','abc')
-1
>>> cmp('abc','abd')
-1


22. complex( [real[, imag]])
Create a complex number with the value real + imag*j or convert a string or number to a complex number. If the first parameter is a string, it will be interpreted as a complex number and the function must be called without a second parameter. The second parameter can never be a string. Each argument may be any numeric type (including complex). If imag is omitted, it defaults to zero and the function serves as a numeric conversion function like int(), long() and float(). If both arguments are omitted, returns 0j.

>>> complex(2,3)
(2+3j)
>>> complex()
0j
>>> complex('2-3j')
(2-3j)
>>> i = complex(1,3)
>>> print i
(1+3j)
>>> complex(i)
(1+3j)

  
23. abs( x)
Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned.

>>> i = complex(1,3)
>>> print i
(1+3j)
>>> abs(i)
3.1622776601683795
>>> abs(1.05)
1.05
>>> abs(-5)
5


24. divmod( a, b)
Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using long division. With mixed operand types, the rules for binary arithmetic operators apply. For plain and long integers, the result is the same as (a / b, a % b). For floating point numbers the result is (q, a % b), where qis usually math.floor(a / b) but may be 1 less than that. In any case q * b + a % b is very close to a, if a % b is non-zero it has the same sign as b, and 0 <= abs(a % b) < abs(b).

Changed in version 2.3: Using divmod() with complex numbers is deprecated.

>>> divmod(10,3)
(3, 1)


25. eval( expression[, globals[, locals]])
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. Changed in version 2.4: formerly locals was required to be a dictionary.The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local name space. If the globals dictionary is present and lacks '__builtins__', the current globals are copied into globals before expression is parsed. This means that expression normally has full access to the standard __builtin__ module and restricted environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where eval is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. 

>>> i = 5
>>> j = 7
>>> eval('i+j')
12
>>> eval('18-90')
-72


26. id( object)
Return the identity of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. (Implementation note: this is the address of the object.)

>>> print fun
<function fun at 0x00FFEEB0>
>>> id(fun)
16772784
>>> hex(16772784)
'0xffeeb0'


27. len(
s)
Return the length (the number of items) of an object. The argument may be a sequence (string, tuple or list) or a mapping (dictionary).

>>> len([])
0
>>> len([1,4,5,7,0])
5
>>> len({1:1,2:2,3:3})
3
>>> len('yangchunlin')
11


28.
max( s[, args...])
With a single argument s, return the largest item of a non-empty sequence (such as a string, tuple or list). With more than one argument, return the largest of the arguments.


min( s[, args...])
With a single argument s, return the smallest item of a non-empty sequence (such as a string, tuple or list). With more than one argument, return the smallest of the arguments.

>>> min((2,4,676,1))
1
>>> max([1,23,56])
56


29. pow( x, y[, z])
Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z). The arguments must have numeric types. With mixed operand types, the coercion rules for binary arithmetic operators apply. For int and long int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, 10**2 returns 100, but 10**-2 returns 0.01. (This last feature was added in Python 2.2. In Python 2.1 and before, if both arguments were of integer types and the second argument was negative, an exception was raised.) If the second argument is negative, the third argument must be omitted. If z is present, x and y must be of integer types, and y must be non-negative. (This restriction was added in Python 2.2. In Python 2.1 and before, floating 3-argument pow() returned platform-dependent results depending on floating-point rounding accidents.

    pow(10,-1,5)
TypeError: pow() 2nd argument cannot be negative when 3rd argument specified
>>> pow(2,5)
32


相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载