[Python Basics] The difference between Python 2 and Python 3--operations between different data types

Source: Internet
Author: User

In the update for Python 3, str and int are no longer supported for direct determination

' Ten '>>> a > 9Traceback (most recent call last):  File "<pyshell#18>", line 1, in <module >    a > 9TypeError: ' > ' not supported between instances of ' str ' and ' int '

In Python 2

' 1 '>>> a >true>>> a > 100000true

Description in the manual

... the choice whether one object is considered smaller or larger than another one was made arbitrarily but consistently

    • Any two objects can be compared
    • Objects of the same type (instances), if they are numeric (Int/float/long/complex), are compared by simple size, if they are non-numeric, and __cmp__ (including __gt__,__lt__, etc.) are defined in the class (type), compared by __cmp__. Otherwise, by address (ID) to compare
    • Different types of objects (instances), if one of the comparison objects is a numeric type (Int/float/long/complex, etc.), then the numeric object < Other non-numeric objects, if two are non-numeric objects, then the order of the type name is compared, such as {} < ABC "(By" dict "<" str "), and" abc "> [+]," ABC "< (.)
    • For a custom class (type) instance, if it inherits from the base type, it is compared by the rule of the base type (1-3). Otherwise, Old-style class < New-style class, New-style class is compared by type name order, and Old-style class is compared by address
    • The bool type is a subclass of int, and true=1, false=0, compare by 1-4, such as true >-1, True < 4.2, true < "abc", etc.

That is, the numeric object in Python 2 < Other non-numeric objects, the string > number expression always returns True

So it does look 3 more rigorous, avoid some of the problems that may arise

[Python Basics] The difference between Python 2 and Python 3--operations between different data types

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.