1. Technology

Numeric Types and Operations


Garry Gay/The Image Bank/Getty Images

There are four distinct numeric types in Python: plain integers, long integers, floating point numbers, and complex numbers. In addition, booleans are considered a subtype of plain integers. Plain integers (also called simply 'integers') are implemented using the long integer type in C. This gives them at least 32 bits of precision. Long integers have unlimited precision. Floating point numbers are implemented using the double data type in C. The precision of each of these types will vary by machine type.

Complex numbers have a real and imaginary part, which are each implemented using C's double integer type. To extract these parts from a complex number x, use x.real and x.imag.

Numbers are created by numeric literals or as the result of built-in functions and operators. Unadorned integer literals (including hex and octal numbers) yield plain integers unless the value they denote is too large to be represented as a plain integer. In this case they yield a long integer. Integer literals with an "L" or "l" suffix yield long integers ("L" is preferred because "1l" looks too much like eleven). Numeric literals containing a decimal point or an exponent sign yield floating point numbers. Appending "j" or "J" to a numeric literal yields a complex number with a zero real part. A complex numeric literal is the sum of a real and an imaginary part.

Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the `narrower' type is widened to that of the other; a plain integer is narrower than long integer is narrower than floating point is narrower than complex. Comparisons between numbers of mixed type use the same rule. The constructors int(), long(), float(), and complex() can be used to produce numbers of a specific type.

All numeric types (except complex) support the following operations, sorted by ascending priority:

  • x + y: sum of x and y/i]
  • x - y: difference of x and y
  • x * y: product of x and y
  • x / y: quotient of x and y; for long (plain) division, the result is an integer. It is always rounded toward minus infinity. The result is always a long integer if either x opr y is a long integer.
  • x // y: (floored) quotient of x and y (aka, integer division); the resultant value is a whole integer, but the result's type may not be that of int.
  • x % y: remainder of x / y (aka, complex floor division operator, modulo operator). Note that this operator is deprecated.
  • -x: x negated
  • +x: x unchanged
  • abs(x): absolute value or magnitude of x
  • int(x): x converted to integer; conversion from a floating point integer may result in truncating the value. Use floor() or ceil() from the math module for better defined conversions.
  • long(x): x converted to long integer; conversion from a floating point integer may result in a truncated value. Use floor() or ceil() from the math module for better defined conversions.
  • 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
  • divmod(x, y): the pair (x // y, x % y)
  • pow(x, y): x to the power y
  • x ** y: x to the power y
  1. About.com
  2. Technology
  3. Python
  4. Python Library
  5. Python Modules - Built-In Functions - Numeric Types and Operations

©2014 About.com. All rights reserved.