Python 3 Numeric Operations

List of numeric operations available in Python, with an example of each.

In Python, all numeric types (except complex) support the following operations. These are sorted by ascending priority. Also note that all numeric operations have a higher priority than comparison operations.

Operation Result Example
x + y Sum of x and y
Result


			700

			
x - y Difference of x and y
Result


			300

			
x * y Product of x and y
Result


			100000

			
x / y Quotient of x and y
Result


			2.5

			
x // y Floored quotient of x and y
Result


			2

			
x % y Remainder of x / y
Result


			100

			
-x x negated
Result


			-300

			
+x x unchanged
Result


			700

			
abs(x) Absolute value or magnitude of x
Result


			500

			
int(x) x converted to integer
Result


			500

			
float(x) x converted to float
Result


			500.0

			
complex(re, im) A complex number with real part re, imaginary part im. im defaults to zero.
Result


			(490+0j)

			
c.conjugate() Conjugate of the complex number c.
Result


(3-4j)

divmod(x, y) The pair (x // y, x % y)
Result


			(4, 10)

			
pow(x, y) x to the power y
Result


			250000

			
x ** y x to the power y
Result


			250000

			

Further Operations for Integers & Floats

Floats and integers also include the following operations.

Operation Result Example
math.trunc(x) x truncated to Integral
Result


			123

			
round(x[, n]) x rounded to n digits, rounding half to even. If n is omitted, it defaults to 0.
Result


			123

			
math.floor(x) The greatest Integral <= x.
Result


			123

			
math.ceil(x) The greatest Integral >= x.
Result


			124

			

The above are some of the most commonly used operations. The Python documentation contains a full list of math modules that can be used on floats and integers, and cmath modules that can be used on complex numbers.

Bitwise Operations on Integer Types

The following bitwise operations can be performed on integers. These are sorted in ascending priority.

Operation Result Example
x | y Bitwise or of x and y
Result


			508

			
x ^ y Bitwise exclusive of x and y
Result


			316

			
x & y Bitwise exclusive of x and y
Result


			192

			
x << n x shifted left by n bits
Result


			2000

			
x >> n x shifted right by n bits
Result


			125

			
~x The bits of x inverted
Result


			-501