Numeric Types in Python



Python has four built-in numeric data types they are integer, long integer, floating point numbers and imaginary numbers.
 
Let under them in details:

Integers 

Integers are whole numbers in the range of -2147483648 to 3147483647 (that is, they are signed, 32-bit numbers).
To check Maximum positive value use sys module has maxint value for example.
>>> import sys
>>> sys.maxint
2147483647

In addition to writing integers in the default decimal (base 10) notation, we can also write integer literals in hexadecimal (base 16) and octal (base 8) notation by preceding the number with a 0x or 0 respectively:

>>> 300 # 300 in decimal
300
>>> 0x12c # 300 in hex
300
>>> 0454 # 300 in octal
300

We need to understand that for decimal numbers, valid digitis are 0 through 9.  For hexadecimal, it’s 0 through 9 and A through F, and for octal its 0 through 7.  If we are not familiar with hexadecimal and octal numbering systems then we will go through in our next option Long integers.

Long integers

Long integers are similar to integers, except that the maximum and minimum values of long integers are restricted only by how much memory we have.  To differentiate between the two types of integers we append an “L” to the end of long integers:

>>> 200L # A long integer literal with a value of 200
200L
>>> 11223344 * 55667788 # Too big for normal integers...
Traceback (innermost last):
File “<interactive input>”, line 1, in ?
OverflowError: integer multiplication
>>> 11223344L * 55667788L # ...but works with long integers
624778734443072L

Note: The “L” on long integers can be uppercase or lowercase, but we make sure to use uppercase version always.

Floating point numbers

Floating point numbers let us express fractional numeric values such as 3.14159.  We can also include an optional exponent.  If we include neither an exponent nor a decimal point, Python interprets the number as an integer, so to express the floating point number two hundred we need to write as 200.0 and not just 200.  Let see sample below:

>>> 0.3
0.29999999999999999

The valid values for floating point numbers and the accuracy with which Python uses them is implementation-dependent, although it is at least 64-bit, double precision math and is often IEEE 754 compliant.

Imaginary numbers

Python has language-level support for imaginary numbers, making it trivial to use them in our programs.  We form an imaginary number by appending a “j” to decimal number.

3j
2.5e-3j

When we add a real and an imaginary number together, Python recognizes the result as a complex number and handles it accordingly:

>>> 2 + 5j
(2+5j)
>>> 2 * (2 + 5j)
(4+10j)

Post a Comment

0 Comments