Numeric Types
The Python numeric types are integers, floating point numbers, and complex numbers. Besides, booleans are implemented as a subtype of integers.
Types of Python Numeric
Integer
Integers
int have unlimited precision. The constructor
int() can be used to create a integer. Integer literals can also be used to create integers. For example,
- Decimal integer
decinteger:e.g. 1234567890
- Binary integer
bininteger:e.g. 0๐10,0๐ต10
- Octal integer
octinteger:e.g. 0๐1234567, 0๐1234567
- Hexadecimal integer
hexinteger: e.g. 0๐ฅ1234567890๐ด๐ต๐ถ๐ท๐ธ๐น, 0๐1234567890๐ด๐ต๐ถ๐ท๐ธ๐น
Floating Point Number
Floating point numbers are usually implemented using double in C. Information about the precision and internal representation of Python floating point numbers can be obtained in
sys.float_info. The standard library also includes the additional numeric types
fractions.Fraction for rationals, and
decimal.Decimal for floating-point numbers with user-definable precision. The constructor
float() can also be used to create a integer. Floating point literals can also be used to create floating point number. For example,
- Decimal point floating point number:
pointfloat e.g. 123456.7890
- Exponent floating point number:
exponentfloat e.g. 7๐10,7.2๐ธ10
Complex Number
Complex numbers have a real and imaginary part. The real part of a complex number can be extracted by
๐ง.real and the imaginary part of the complex number can be extracted by
๐ง.imag. The constructor
complex() can be used to create a integer. The real part of a complex number can be created by the floating point numeral or integer numeral. While the imaginary part of the complex number can be created by the imaginary numeral which is similar to the real part but a specifier
๐ or
๐ฝ is appended to the numeric literal. A complex number with a zero real part can be created by a imaginary literal only. For example,
1.3+0.8๐, 5.6+8.4๐ฝ
Python Numeric Operations
Python numeric operations can be divided into basic arithmetic operations and real operations.
Arithmetic Operations
The basic types of Python supported operators for basic arithmetic operations are Unary operators, binary operators and numeric functions.
OperationDescriptionRemarks
-๐ฅ๐ฅ negated
+๐ฅ๐ฅ unchanged
๐ฅ+๐ฆsum of ๐ฅ and ๐ฆ
๐ฅ-๐ฆdifference of ๐ฅ and ๐ฆ
๐ฅ*๐ฆproduct of ๐ฅ and ๐ฆ
๐ฅ/๐ฆquotient of ๐ฅ and ๐ฆ
๐ฅ//๐ฆfloored quotient of ๐ฅ and ๐ฆ
๐ฅ%๐ฆremainder of ๐ฅ/๐ฆ
abs(๐ฅ)absolute value or magnitude of ๐ฅ
int(๐ฅ)๐ฅ converted to integer
float(๐ฅ)๐ฅ converted to floating point
complex(๐๐,๐๐)a complex number with real part ๐๐, imaginary part ๐๐ with ๐๐ defaults to zero
๐.conjugate()conjugate of the complex number ๐
divmod(๐ฅ,๐ฆ)the pair (๐ฅ//๐ฆ,๐ฅ%๐ฆ)
pow(๐ฅ,๐ฆ)๐ฅ to the power ๐ฆ
๐ฅ**๐ฆ๐ฅ to the power ๐ฆ
Also referred to as integer division. The resultant value is a whole integer, though the resultโs type is not necessarily int. The result is always rounded towards minus infinity: 1//2 is 0, (-1)//2 is -1, 1//(-2) is -1, and (-1)//(-2) is 0.
Not for complex numbers. Instead convert to floats using abs() if appropriate.
Conversion from floating point to integer may round or truncate as in C; see functions math.floor() and math.ceil() for well-defined conversions.
float also accepts the strings โnanโ and โinfโ with an optional prefix โ+โ or โ-โ for Not a Number (NaN) and positive or negative infinity.
Python defines pow(0, 0) and 0 ** 0 to be 1, as is common for programming languages.
The numeric literals accepted include the digits 0 to 9 or any Unicode equivalent (code points with the Nd property).
Real Number Operations
The standard types of Python supported operators for real number operations are numeric functions.
OperationDescriptionRemarks
math.trunc(๐ฅ)๐ฅ truncated to Integral
round(๐ฅ[,๐])๐ฅ rounded to ๐ digits, rounding half to even. Default to 0, if ๐ is omitted
math.floor(๐ฅ)the greatest Integral ๐ฅ <=
math.ceil(๐ฅ)the least Integral ๐ฅ >=
Typical Integer Operations
Bitwise Operations on Integer Types
Bitwise operations are for integers only. The basic types of Python supported operators are Unary operators, binary operators and numeric functions.
Types of Bitwise Operators
The types of bitwise operators are:
OperationDescriptionRemarks
~๐ฅthe bits of ๐ฅ
๐ฅ|๐ฆbitwise or of ๐ฅ and ๐ฆ
๐ฅ^๐ฆbitwise exclusive or of ๐ฅ and ๐ฆ
๐ฅ&๐ฆbitwise and of ๐ฅ and ๐ฆ
๐ฅ<<๐๐ฅ shifted left by ๐ bits
๐ฅ>>๐๐ฅ shifted right by ๐ bits
The result of bitwise operations is calculated as though carried out in twoโs complement with an infinite number of sign bits.
The priorities of the binary bitwise operations are all lower than the numeric operations and higher than the comparisons; the unary operation ~ has the same priority as the other unary numeric operations (+ and -).
Negative shift counts are illegal and cause a ValueError to be raised.
A left shift by n bits is equivalent to multiplication by pow(2, n) without overflow check.
A right shift by n bits is equivalent to division by pow(2, n) without overflow check.
Performing these calculations with at least one extra sign extension bit in a finite twoโs complement representation (a working bit-width of 1 + max(x.bit_length(), y.bit_length()) or more) is sufficient to get the same result as if there were an infinite number of sign bits.
Methods on Integer Types
The
int type implements the
numbers.Integral abstract base class.
Other Methods on integer are:
OperationDescriptionRemarks
int.bit_length()
int.to_bytes(length, byteorder, *, signed=False)
classmethod int.from_bytes(bytes, byteorder, *, signed=False)
int.as_integer_ratio()
Typical Float Operations
The
float type implements the
numbers.Real abstract base class.
Methods on Float Types
Other Methods on float are:
OperationDescriptionRemarks
float.as_integer_ratio()
float.is_integer()
float.hex()
classmethod float.fromhex(s)
Hashing of numeric types
For numbers x and y, possibly of different types, itโs a requirement that hash(x) == hash(y) whenever x == y (see the __hash__() method documentation for more details). For ease of implementation and efficiency across a variety of numeric types (including int, float, decimal.Decimal and fractions.Fraction) Pythonโs hash for numeric types is based on a single mathematical function thatโs defined for any rational number, and hence applies to all instances of int and fractions.Fraction, and all finite instances of float and decimal.Decimal. Essentially, this function is given by reduction modulo P for a fixed prime P. The value of P is made available to Python as the modulus attribute of sys.hash_info.
CPython implementation detail: Currently, the prime used is P = 2**31 - 1 on machines with 32-bit C longs and P = 2**61 - 1 on machines with 64-bit C longs.
Here are the rules in detail:
If x = m / n is a nonnegative rational number and n is not divisible by P, define hash(x) as m * invmod(n, P) % P, where invmod(n, P) gives the inverse of n modulo P.
If x = m / n is a nonnegative rational number and n is divisible by P (but m is not) then n has no inverse modulo P and the rule above doesnโt apply; in this case define hash(x) to be the constant value sys.hash_info.inf.
If x = m / n is a negative rational number define hash(x) as -hash(-x). If the resulting hash is -1, replace it with -2.
The particular values sys.hash_info.inf, -sys.hash_info.inf and sys.hash_info.nan are used as hash values for positive infinity, negative infinity, or nans (respectively). (All hashable nans have the same hash value.)
For a complex number z, the hash values of the real and imaginary parts are combined by computing hash(z.real) + sys.hash_info.imag * hash(z.imag), reduced modulo 2**sys.hash_info.width so that it lies in range(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width - 1)). Again, if the result is -1, itโs replaced with -2.
Source and Reference