Python provides four built-in types of numbers: plain integers, long integers, floating point numbers and complex numbers.
Numbers all have several things in common. Principally, the standard arithmetic operators of ‘+’, ‘-’, ‘*’, ‘/’, ‘%’ and ‘**’ are all available for all of these numeric types. Additionally, numbers can be compared, using comparison operators that we’ll look at inComparisons. Also, numbers can be coerced from one type to another.
More sophisticated math is separated into the math module, which we will cover later. However, a few advanced math functions are an integral part of Python, includingabs()andpow().
5.2.1 Integers
Python represents integers as strings of decimal digits. A number does not include any punctuation, and cannot begin with a leading zero (0). Leading zeros are used for base 8 and base 16 numbers. We’ll look at this below. >>> 255+100 355 >>> 397-42 355 >>> 71*5 355 >>> 355/113 3
While most features of Python correspond with common expectations from mathematics and other program- ming languages, the division operator, ‘/’, poses certain problems. Specifically, the distinction between the algorithm and the data representation need to be made explicit. Division can mean either exact floating- point results or integer results. Mathematicians have evolved a number of ways of describing precisely what they mean when discussing division. We need similar expressive power in Python.We’ll look at more details of division operators inDivision Operators.
Binary, Octal and Hexadecimal. For historical reasons, Python supports programming in octal and hexadecimal. I like to think that the early days of computing were dominated by people with 8 or 16 fingers. A number with a leading ‘0’ (zero) is octal, base 8, and uses the digits 0 to 7. 0123is octal and equal to 83 decimal.
A number with a leading0xor 0Xis hexadecimal, base 16, and uses the digits 0 through 9, plus ‘a’, ‘A’, ‘b’, ‘B’, ‘c’, ‘C’, ‘d’, ‘D’, ‘e’, ‘E’, ‘f’, and ‘F’. 0x2BC8is hexadecimal and equal to 11208.
A number with a leading0bor 0Bis binary, base 2, and uses digits 0 and 1. Important: Leading Zeroes
When using Python 2.6, watch for leading zeros in numbers. If you simply transcribe programs from other languages, they may use leading zeros on decimal numbers.
Important: Python 3
In Python 3, the octal syntax will change. Octal constants will begin with ‘0o’ to match hexadecimal constants which begin with ‘0x’.
0o123will be octal and equal to 83 decimal.
5.2.2 Long Integers
One of the useful data types that Python offers are long integers. Unlike ordinary integers with a limited range, long integers have arbitrary length; they can have as many digits as necessary to represent an exact answer. However, these will operate more slowly than plain integers.
Long integers end in ‘L’ or ‘l’. Upper case ‘L’ is preferred, since the lower-case ‘l’ looks too much like the digit ‘1’. Python is graceful about converting to long integers when it is necessary.
Important: Python 3
Python 3 will not require the trailing ‘L’. It will silently deduce if you need an integer or a long integer. How many different combinations of 32 bits are there? The answer is there are232; ‘2**32’ in Python. The answer is too large for ordinary integers, and we get the result as a long integer.
>>> 2**32
4294967296L
>>> 2**64
18446744073709551616L
There are about 4 billion ways to arrange 32 bits. How many bits in 1K of memory? 1024×8 bits. How many combinations of bits are possible in 1K of memory? 21024×8.
print 2L**(1024*8)
I won’t attempt to reproduce the output from Python. It has 2,467 digits. There are a lot of different combinations of bits in only 1K of memory. The computer I’m using has 512×1024K bytes of memory; there are a lot of combinations of bits available in that memory.
Python will silently convert between ultra-fast integers and slow-but-large long integers. You can force a conversion using theint()orlong()factory functions.
5.2.3 Floating-Point Numbers
Python offers floating-point numbers, often implemented as “double-precision” numbers, typically using 64 bits. Floating-point numbers are written in two forms: a simple string of digits that includes a decimal point, and a more complex form that includes an explicit exponent.
.0625 0.0625 6.25E-2 625E-4
The last two examples are based on scientific notation, where numbers are written as a mantissa and an exponent. The ‘E’ (or code:e) , powers of 10 are used with the exponent, giving us numbers that look like this: 6.25×10−2 and625×10−4.
The last example isn’t properly normalized, since the mantissa isn’t between 0 and 10.
Generally, a number,n, is some mantissa,g, and an exponent ofc. For human consumption, we use a base of 10.
Internally, most computers use a base of 2, not 10.
n=g×10c
n=h×2d
This differece in the mantissa leads to slight errors in converting certain values, which are exact in base 10, to approximations in base 2.
For example, 1/5th doesn’t have a precise representation. This isn’t generally a problem because we have string formatting operations which can make this tiny representation error invisible to users.
>>> 1./5.
0.20000000000000001
>>> .2
5.2.4 Complex Numbers
Besides plain integers, long integers and floating point numbers, Python also provides for imaginary and complex numbers. These use the European convention of ending with ‘J’ or ‘j’. People who don’t use complex numbers should skip this section.
‘3.14J’ is an imaginary number = 3.14×√−1.
A complex number is created by adding a real and an imaginary number: ‘2 + 14j’. Note that Python always prints these in ()’s; for example(2+14j).
The usual rules of complex math work perfectly with these numbers.
>>> (2+3j)*(4+5j) (-7+22j)
Python even includes the complex conjugate operation on a complex number. This operation follows the complex number separated by a dot (‘.’). This notation is used because the conjugate is treated like a method function of a complex number object (we’ll return to this method and object terminology inClasses). For example:
>>> 3+2j.conjugate() (3-2j)