Among the most basic Python objects are the numbers, which come in three types: integers (type: int), floating-point numbers (type: float) and complex numbers (type: complex).
Integers
Integers are whole numbers such as 1, 8, 72 and 3847298893721407. In Python 3, there is no limit to their magnitude (apart from the availability of your computer’s memory). Integer arithmetic is exact. For clarity, it is possible to separate any pair of digits by an underscore character, “_.” For example, 299_792_458 is interpreted as the same number as 299792458.
Floating-Point Numbers
Floating-point numbers are the representation of real numbers such as 1.2, -0:36 and 1:67263 . They do not, in general, have the exact value of the real number they represent, but are stored in binary to a certain precision (on most systems, to the equivalent of 15–16 decimal places). For example, the number 4/3 is stored as the binary equivalent of 1:33333333333333325931846502...., which is nearly (but not quite) the same as the infinitely repeating decimal representation of 4/3 = 1:3333..... Moreover, even numbers that do have an exact decimal representation may not have an exact binary representation: for example 1=10 is represented by the binary number equivalent to 0:10000000000000000555111512... Because of this finite
precision, floating-point arithmetic is not exact but, with care, it is “good enough” for most scientific applications.
Any single number containing a period (“.”) is considered by Python to specify a floating-point number. Scientific notation is supported using “e” or “E” to separate the significand (mantissa) from the exponent: for example, 1.67263e-7 represents the number 1:67263 X 10 to the power -7. As with integers, pairs of digits may be separated by an underscore. For example, 1.602_176_634e-34.
Complex Numbers
Complex numbers such as 4+3 j consist of a real and an imaginary part (denoted by j in Python), each of which is itself represented as a floating-point number (even if specified without a period). Complex number arithmetic is therefore not exact but subject to the same finite precision considerations as floats.
A complex number may be specified either by “adding” a real number to an imaginary one (denoted by the j suffix), as in 2.3 + 1.2j or by separating the real and imaginary parts in a call to complex, as in complex(2.3, 1.2).
Typing a number at the Python shell prompt simply echoes the number back to you:
>>> 5
5
>>> 5.
5.0
>>> 0.10
0.1
>>> 0.0001
0.0001
>>> 0.0000999
9.99e-05
Note that the Python interpreter displays numbers in a standard way. For example:
1. The internal representation of 0.1 discussed earlier is rounded to “0.1,” which is the shortest number with this representation.
2. Numbers smaller in magnitude than 0.0001 are displayed in scientific notation.
A number of one type can be created from a number of another type with the relevant constructor:
>>> float(5)
5.0
>>> int(5.2)
5
>>> int(5.9)
5
>>> complex(3.)
(3+0j)
>>> complex(0., 3.)
3j
1. Note that a positive floating-point number is rounded down in casting it into an integer; more generally, int rounds towards zero: int(-1.4) would yield -1.
2. Constructing a complex object from a float generates a complex number with the
imaginary part equal to zero.
3. To generate a pure imaginary number, you have to explicitly pass two numbers to
complex with the first, real part, equal to zero.
We will continue with our discussion about Numbers in the next post.
0 comments:
Post a Comment