As a special general computer language, the Python programming language is used in a slightly different way than other common programming languages. So today, we can take a preliminary look at the application methods of the Python numeric type.
Description
The Python numeric type cannot be changed. That is to say, a new object is generated when the numeric value is changed.
Change
The following is an example of modifying or deleting a number type.
- >>> a=1
- >>> id(a)
- 3629968
- >>> a=2
- >>> id(a)
- 3629956
- >>> del a
- >>> id(a)
- Traceback (most recent call last):
- File "<interactive input>", line 1, in <module>
- NameError: name 'a' is not defined
- >>>
Python numeric type
Number types in Python: integer, long integer, Boolean, double-precision floating point, decimal floating point, and plural.
1) Integer
A) boolean type: there are only two integer values.
- >>> a=True
- >>> int(a)
- 1
- >>> a=False
- >>> int(a)
- 0
- >>>
- >>> bool(1)
- True
- >>> bool(True)
- True
- >>> bool('1')
- True
- >>> bool('0')
- True
- >>> bool(0)
- False
- >>> bool('')
- False
- >>> bool([])
- False
- >>> a=1
- >>> b=a<2
- >>> b
- True
- >>> True,FalseFalse=False,True
- >>> bool(True)
- False
- >>> bool(False)
- True
B) integer and long integer
After Python 2.2, you can think of integer and long integer as the same thing.
- >>> 10000**8
- 100000000000000000000000000000000L
- >>> 10**8
- 100000000
- >>>
2) double-precision floating point number
- Basic Application Syntax of Python constructor list
- Share basic operations for implementing INI File Operations in Python
- How to Implement tab file operations in Python
- Use Python recursion to process objects
- Analysis of simple Python File Operations
Similar to double in C.
3) plural
The plural number in the Python numeric type consists of a real number and a virtual number: real + imagj.
Negative attributes:
Num. real the real part of the plural
Num. imag the imaginary part of the plural number
Num. conjugate () returns the combination of the complex numbers.
- >>> a=1+3j
- >>> b=1+3j
- >>> id(a)
- 19911528
- >>> id(b)
- 19911600
- >>> a.real
- 1.0
- >>> a.imag
- 3.0
- >>> a.conjugate
- <built-in method conjugate of complex object at 0x012FD368>
4) decimal floating point number
- Decimal
- >>> from decimal import Decimal
- >>> Decimal("0.1")*Decimal("3.1415926")
- Decimal("0.31415926")
The above is our introduction to the Python numeric type.