Arithmetic in python2

Division and real division

>>> 1/2
0
>>> from __future__ import division
>>> 1/2
0.5
>>> 1//2
0

Comparison

>>> 1 > 2 > 3
False
>>> 3 > 2 > 1
True
>>> cmp(1,4)
-1

Numericla factory functions

>>> bool(0)
False
>>> bool('0')
True
>>> int('12332')
12332
>>> int('0xfff', base=16)
4095
>>> long('0xffffffff', base=16)
4294967295L
>>> float('1.23333')
1.23333
>>> float(123)
123.0
>>> complex(1,2)
(1+2j)

Coerce()

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

coerce(...)
    coerce(x, y) -> (x1, y1)
    
    Return a tuple consisting of the two numeric arguments converted to
    a common type, using the same rules as used by arithmetic operations.
    If coercion is not possible, raise TypeError.

 

你可能感兴趣的:(Arithmetic in python2)