Analysis of the meaning and application characteristics of the Python numeric type

Source: Internet
Author: User

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.

 
 
  1. >>> a=1 
  2. >>> id(a)  
  3. 3629968  
  4. >>> a=2 
  5. >>> id(a)  
  6. 3629956  
  7. >>> del a  
  8. >>> id(a)  
  9. Traceback (most recent call last):  
  10. File "<interactive input>", line 1, in <module> 
  11. NameError: name 'a' is not defined  
  12. >>> 

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.

 
 
  1. >>> a=True 
  2. >>> int(a)  
  3. 1  
  4. >>> a=False 
  5. >>> int(a)  
  6. 0  
  7. >>>   
  8. >>> bool(1)  
  9. True  
  10. >>> bool(True)  
  11. True  
  12. >>> bool('1')  
  13. True  
  14. >>> bool('0')  
  15. True  
  16. >>> bool(0)  
  17. False  
  18. >>> bool('')  
  19. False  
  20. >>> bool([])  
  21. False  
  22. >>> a=1 
  23. >>> b=a<2 
  24. >>> b  
  25. True  
  26. >>> True,FalseFalse=False,True  
  27. >>> bool(True)  
  28. False  
  29. >>> bool(False)  
  30. True 

B) integer and long integer

After Python 2.2, you can think of integer and long integer as the same thing.

 
 
  1. >>> 10000**8  
  2. 100000000000000000000000000000000L  
  3. >>> 10**8  
  4. 100000000  
  5. >>> 

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.

 
 
  1. >>> a=1+3j  
  2. >>> b=1+3j  
  3. >>> id(a)  
  4. 19911528  
  5. >>> id(b)  
  6. 19911600  
  7. >>> a.real  
  8. 1.0  
  9. >>> a.imag  
  10. 3.0  
  11. >>> a.conjugate  
  12. <built-in method conjugate of complex object at 0x012FD368> 

4) decimal floating point number

 
 
  1. Decimal  
  2. >>> from decimal import Decimal  
  3. >>> Decimal("0.1")*Decimal("3.1415926")  
  4. Decimal("0.31415926") 

The above is our introduction to the Python numeric type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.