Built-in Numeric Objects and Operations in Python

Python's Numeric Objects

In Python, numbers are not really a single object type, but a category of similar types.

integers

Positive and negative whole numbers. Integers can have unlimited precision—they can grow to have as many digits as your memory space allows.

Python also allows us to write integers using hexadecimal, octal, and binary literals. The built-in calls hex(INT), oct(INT), and bin(INT) convert an integer to its representation string in these three bases. In memory, an integer’s value is the same, regardless of the base we use to specify it.

Example

1234, −24, 0, 99999999999999
0x9ff(hex, base 16), 0o177(octal, base 8), 0b101010(binary literals, base 2)

floating-point

Numbers with a fractional part. Floating-point numbers have a decimal point and/or an optional signed exponent introduced by an e or E and followed by an optional sign. Floating-point numbers are implemented as C “doubles” in standard CPython.

Example

1.23, 3.14e-10, 4E210, 4.0e+210

decimals

Decimals are fixed-precision floating-point values. Decimals generally incurs a performance penalty compared to the normal floating-point type.

>>> 0.1+0.1+0.1-0.3                        # floating-point
5.551115123125783e-17  
>>>
>>> from decimal import Decimal            # decimal
>>> Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.3')
Decimal('0.0')  
>>> 1999 + 1.33 # This has more digits in memory than displayed in 3.3
2000.33  
>>>
>>> decimal.getcontext().prec = 2
>>> pay = decimal.Decimal(str(1999 + 1.33))
>>> pay
Decimal('2000.33')  

complex

Typically used in engineering and science application.

Python complex literals are written as realpart+imaginary-part, where the imaginary-part is terminated with a j or J. Complex numbers may also be created with the complex(real, imag) built-in call.

3+4j, 3.0+4.0j, 3J

sets

Sets have both numeric and collection qualities, but are generally considered more the former than the latter.

set('spam'), {1, 2, 3, 4}

boolean

Boolean is numeric in nature because its two values, True and False, are just customized versions of the integers 1 and 0 that print themselves differently.

>>> True == 1 # Same value
True  
>>> True is 1 # But a different object: see the next chapter
False  

fraction

Operators

  • In Python3, devision( "/" ) returns floating-point numbers:
  >>> 4/2
  2.0
  • Floor devision, returns an integer for integer operands or a float if any operand is a float
  >>> 8//3
  2
  >>> 8.0//3
  2.0
  • Floor devision, truncates the result down to its floor, which means the closest whole number below the true result
  >>> import math
  >>> math.floor(2.5)         # Closest number below value
  2
  >>> math.floor(-2.5)
  -3

  • Chained comparison
  >>> 1 == 2 < 3                # Same as: 1 == 2 and 2 < 3
  False
  • Some times, the comparison does not work for floating-point numbers
  >>> 1.1 + 2.2 == 3.3             # Shouldn't this be True?...
  False
  >>> 1.1 + 2.2
  3.3000000000000003
  • Bitwise Operations

    bitwise operations are often not as important in a high-level language such as Python as they are in a low-level language such as C.

  >>> a = 1            # '1' decimal is 0001 in bits
  >>> a << 2            # Shift left 2 bits: 0100
  4
  >>> a | 2            # Bitwise OR (either bit=1): 0011
  3
  >>> a & 1            # Bitwise AND (both bits=1): 0001
  1

Related Modules

round

math - more advanced numeric functions

random - random number generation and random selections

Third-party Extensions