Sideway from Sideway
Draft for Information Only


Numeric Types
 Types of Python Numeric
  Floating Point Number
  Complex Number
 Python Numeric Operations
  Arithmetic Operations
  Real Number Operations
 Typical Integer Operations
  Bitwise Operations on Integer Types
   Types of Bitwise Operators
  Methods on Integer Types
 Typical Float Operations
  Methods on Float Types
 Hashing of numeric types
 Source and Reference

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


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


ID: 201002402 Last Updated: 24/10/2020 Revision: 0


Home 5






Hobbies 8


Chinese 1097

English 337

Reference 67


Hardware 151


Application 202

Digitization 25

Latex 10

Manim 159

Numeric 19


Web 285

Unicode 494


CSS 59


OS 391

DeskTop 7

Python 37



Formulas 8

Algebra 29

Number Theory 206

Trigonometry 18

Geometry 18

Calculus 67

Complex Analysis 21


Tables 8


Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5


Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Electric 27

Biology 1

Geography 1

Copyright © 2000-2021 Sideway . All rights reserved Disclaimers last modified on 06 September 2019