Python skills-detailed explanation of the practical application of code efficiency

Source: Internet
Author: User

If you are not familiar with the Python skills-the practical application of code efficiency, you can read the following articles to learn more about the practical application of the Python skills, I hope you can learn what you want after reading the following articles.

Objects are equal. What is the difference between type (a) = type (B) and type (a) is type (B? Why the latter? What is the relationship between the isinstance () function and this function?

In Example 4.1, we provided a script to demonstrate how to use the isinstance () and type () functions in the runtime environment. Then we will discuss how to use type () and how to port this example to use isinstance (). Run typechk. py and we will get the following output:

 
 
  1. -69 is a number of type: int  
  2. 9999999999999999999999 is a number of type: long  
  3. 98.6 is a number of type: float  
  4. (-5.2+1.9j) is a number of type: complex  
  5. xxx is not a number at all!! 

For example, the type (typechk. py) function displayNumType () accepts a numeric parameter. In the Python technique, use the built-in function type () to confirm the numeric type or not a numeric type ).

 
 
  1. #!/usr/bin/env python  
  2. def displayNumType(num):  
  3. print num, 'is',  
  4. if isinstance(num, (int, long, float, complex)):  
  5. print 'a number of type:', type(num).__name__  
  6. else:  
  7. print 'not a number at all!!'  
  8. displayNumType(-69)  
  9. displayNumType(9999999999999999999999L)  
  10. displayNumType(98.6)  
  11. displayNumType(-5.2+1.9j)  
  12. displayNumType('xxx')  

The above is an introduction to the Python skills-code efficiency-related content. I hope you will gain some benefits.

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.