Scripting >> Python >> Examples >> Operators >> Numeric operators

Python 2.7

Operation Result
x + y sum of x and y
x - y difference of x and y
x * y product of x and y
x / y quotient of x and y
x // y (floored) quotient of x and y
x % y remainder of x / y
-x x negated
+x x unchanged
abs(x) absolute value or magnitude of x
int(x) x converted to integer
long(x) x converted to long integer
float(x) x converted to floating point
complex(re,im) a complex number with real part re, imaginary part im. im defaults to zero.
c.conjugate() conjugate of the complex number c. (Identity on real numbers)
divmod(x, y) the pair (x // y, x % y)
pow(x, y) x to the power y
x ** y x to the power y

 

Bitwise operations on Integer Types

Operation Result Notes
x | y bitwise or of x and y  
x ^ y bitwise exclusive or of x and y  
x & y bitwise and of x and y  
x << n x shifted left by n bits (1)(2)
x >> n x shifted right by n bits (1)(3)
~x the bits of x inverted  

Notes:

  1. Negative shift counts are illegal and cause a ValueError to be raised.
  2. A left shift by n bits is equivalent to multiplication by pow(2, n). A long integer is returned if the result exceeds the range of plain integers.
  3. A right shift by n bits is equivalent to division by pow(2, n).