Home  >  Article  >  Backend Development  >  Chapter 5 python numbers

Chapter 5 python numbers

黄舟
黄舟Original
2016-12-22 17:08:571208browse

5.1 Number type
 Numbers provide scalar storage and direct access. It is an immutable type, which means that changing the value of the number will create a new object. Of course, this process is transparent to both programmers and users, and does not affect the way software is developed. Python supports multiple number types: integer, long, boolean, double, decimal float, and complex numbers.

How to update a numerical object
 Because you do not actually update the original value of the object. This is because numeric objects are immutable objects. Python's object model is somewhat different from the regular object model. What you think of as an update is actually creating a new numeric object and getting a reference to it.
In the process of learning programming, we have always been taught that a variable is like a box containing the value of the variable. In Python, a variable is more like a pointer pointing to a box that holds the variable's value. For immutable types, you cannot change the contents of the box, but you can point the pointer to a new box. Every time you assign another number to a variable, you actually create a new object and assign it to the variable. (This is true not just for numbers, but for all immutable types)
anInt += 1
aFloat = 2.718281828
How to delete a numerical object
 According to Python’s rules, you cannot really delete a numerical object, you just no longer use it. If you
actually want to delete a reference to a numeric object, use the del statement (see Section 3.5.6). After deleting the reference to an object, you can no longer use the reference (variable name) unless you assign a new value to it. If you try to use an object reference that has been deleted, a NameError exception will be raised.
del anInt
del aLong, aFloat, aComplex

5.3 Double-precision floating-point numbers

Floating-point numbers in Python are similar to the double type in C language. They are double-precision floating-point numbers that can be expressed in direct decimal or scientific notation. Each floating point number occupies 8 bytes (64 bits) and fully complies with the IEEE754 specification (52M/11E/1S), of which 52 bits are used to represent the base and 11 bits are used to represent the exponent (the representable range is approximately plus or minus 10 to the power of 308.25), and the remaining one bit represents the symbol. This looks pretty good, however, the actual accuracy depends on the machine architecture and the compiler used to create the Python interpreter. Floating-point values ​​usually have a decimal point and an optional suffix e (uppercase or lowercase, indicating scientific notation). You can use positive (+) or negative (-) between e and the exponent to indicate the sign of the exponent (the sign can be omitted if it is a positive number). Here are some examples of typical floating point values:

0.0   -777. 1.6   -5.555567119 96e3 * 1.0
4.3e25 9.384e-23   -2.172818   float(12)   1.000000001
3.14 16 4.2E-10 -90. 6.022e23 -1.609E -19

5.4 Complex numbers

The following are several concepts about complex numbers in the Python language:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Chapter 4 python objectsNext article:Chapter 4 python objects