Python core programming-Chapter 4 Python objects

Source: Internet
Author: User

Three ways to view digital data:

[1] isinstance is more readable


#! /Usr/bin/ENV python <br/> ''' typechk. PY ''' <br/> def displaynumtype (Num): <br/> Print num, 'is ', <br/> If isinstance (Num, (INT, long, float, complex): <br/> Print 'a number of type: ', type (Num ). _ name __< br/> else: <br/> Print 'not a number at all! '<Br/> displaynumtype (-69) <br/> displaynumtype (99999999999999999999l) <br/> displaynumtype (99999999999999999999) <br/> displaynumtype (98.6) <br/> displaynumtype (-5.2 + 1.9j) <br/> displaynumtype ('aaa ') <br/>####### <br/> # output <br/>####### <br/>-69 is a number of type: int <br/> 99999999999999999999 is a number of type: Long <br/> 99999999999999999999 is a number of type: Long <br/> 98.6 is a number of Ty PE: Float <br/> (-5.2 + 1.9j) is a number of type: complex <br/> AAA is not a number at all! <Br/>

[2] Calling type twice

#! /Usr/bin/ENV <br/> python <br/> ''' typechk. PY ''' <br/> def displaynumtype (Num): <br/> Print num, 'is ', <br/> If type (Num) = type (0): <br/> Print 'an integer '<br/> Elif type (Num) = type (0l ): <br/> Print 'a long' <br/> Elif type (Num) = type (0.0 ): <br/> Print 'a float' <br/> Elif type (Num) = type (0 + 0j ): <br/> Print 'a complex number' <br/> else: <br/> Print 'not a number at all! '<Br/> displaynumtype (-69) <br/> displaynumtype (99999999999999999999l) <br/> displaynumtype (99999999999999999999) <br/> displaynumtype (98.6) <br/> displaynumtype (-5.2 + 1.9j) <br/> displaynumtype ('aaa ') <br/>####### <br/> # output <br/>####### <br/>-69 is an integer <br/> 99999999999999999999 is a long <br/> 99999999999999999999 is a long <br/> 98.6 is a float <br/> (-5.2 + 1.9j) is a complex number <br/> AAA is Not a number at all! <Br/>

[3] import types

#! /Usr/bin/ENV <br/> python <br/> Import types <br/> '''typechk. PY ''' <br/> def displaynumtype (Num): <br/> Print num, 'is ', <br/> If type (Num) = types. inttype: <br/> Print 'an integer '<br/> Elif type (Num) = types. longtype: <br/> Print 'a long' <br/> Elif type (Num) = types. floattype: <br/> Print 'a float' <br/> Elif type (Num) = types. complextype: <br/> Print 'a complex number' <br/> else: <br/> Print 'not Number at all! ', </P> <p> Print' but, it is ', <br/> If type (Num) = types. stringtype: <br/> Print 'a string' </P> <p> displaynumtype (-69) <br/> displaynumtype (99999999999999999999l) <br/> displaynumtype (99999999999999999999) <br/> displaynumtype (98.6) <br/> displaynumtype (-5.2 + 1.9j) <br/> displaynumtype ('aaa ') <br/>####### <br/> # output <br/>####### <br/>-69 is an integer <br/> 99999999999999999999 is a long <br/> 99999999999 999999999 is a long <br/> 98.6 is a float <br/> (-5.2 + 1.9j) is a complex number <br/> AAA is not a number at all! But, it is a string <br/>


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.