Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 939113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:44:17+00:00 2026-05-15T21:44:17+00:00

The following snippet is annotated with the output ( as seen on ideone.com ):

  • 0

The following snippet is annotated with the output (as seen on ideone.com):

print "100" < "2"      # True
print "5" > "9"        # False

print "100" < 2        # False
print 100 < "2"        # True

print 5 > "9"          # False
print "5" > 9          # True

print [] > float('inf') # True
print () > []          # True

Can someone explain why the output is as such?


Implementation details

  • Is this behavior mandated by the language spec, or is it up to implementors?
  • Are there differences between any of the major Python implementations?
  • Are there differences between versions of the Python language?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:44:18+00:00Added an answer on May 15, 2026 at 9:44 pm

    From the python 2 manual:

    CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

    When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).

    When you order a numeric and a non-numeric type, the numeric type comes first.

    >>> 5 < 'foo'
    True
    >>> 5 < (1, 2)
    True
    >>> 5 < {}
    True
    >>> 5 < [1, 2]
    True
    

    When you order two incompatible types where neither is numeric, they are ordered by the alphabetical order of their typenames:

    >>> [1, 2] > 'foo'   # 'list' < 'str' 
    False
    >>> (1, 2) > 'foo'   # 'tuple' > 'str'
    True
    
    >>> class Foo(object): pass
    >>> class Bar(object): pass
    >>> Bar() < Foo()
    True
    

    One exception is old-style classes that always come before new-style classes.

    >>> class Foo: pass           # old-style
    >>> class Bar(object): pass   # new-style
    >>> Bar() < Foo()
    False
    

    Is this behavior mandated by the language spec, or is it up to implementors?

    There is no language specification. The language reference says:

    Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.

    So it is an implementation detail.

    Are there differences between any of the major Python implementations?

    I can’t answer this one because I have only used the official CPython implementation, but there are other implementations of Python such as PyPy.

    Are there differences between versions of the Python language?

    In Python 3.x the behaviour has been changed so that attempting to order an integer and a string will raise an error:

    >>> '10' > 5
    Traceback (most recent call last):
      File "<pyshell#0>", line 1, in <module>
        '10' > 5
    TypeError: unorderable types: str() > int()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I do the following with c# attributes. Below is a snippet of
For the following snippet of code, int n; char buf[100]; int fd = open
I'm using the following snippet to create a select/deselect all checkboxes: http://www.wiseguysonly.com/2010/01/15/select-and-unselect-all-checkboxes-with-jquery/ This is
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
The following snippet of C# code: int i = 1; string result = String.Format({0},{1},{2},
The following snippet is supposed to take the value of PROJECT (defined in the
In the following snippet: public class a { public void otherMethod(){} public void doStuff(String
Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by
In the following snippet, using XmlReader, when I encounter an element. I would like
I have the following snippet in one of my html pages : <div class=inputboximage>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.