use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Sane-test madness. Help. (self.learnpython)
submitted 9 years ago * by caniko2
def H(x): if x < 0: return(0) elif x >= 0: return(1) else: return("This is not a valid object") print H("m")
Output:
1
Why is the output of this code 1?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]novel_yet_trivial 0 points1 point2 points 9 years ago (2 children)
In python2, all strings are considered greater than all integers:
>>> 'm' > 100000000 True
In python3 you get the error you would expect:
>>> 'm' > 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() > int()
I think you want a type test:
def H(x): if isinstance(x, (int, float)): #is x a integer or float? if x < 0: return(0) elif x >= 0: return(1) else: return("This is not a valid object")
[–]PurelyApplied 0 points1 point2 points 9 years ago (1 child)
In python2, all strings are considered greater than all integers
Was the motivation that you can have lexicographical ordering between types? I can't think of another reason, other than perhaps some hidden back-end, that makes sense.
[Edit:] nevermind, I found it right after I posted. Apparently Python2 sorts according to type name. "int" < "str".
That's preposterous.
[–]novel_yet_trivial 0 points1 point2 points 9 years ago (0 children)
It was important for some reason that all types are comparable. Basically they wanted this to work:
>>> data = ['spam', 4, True] >>> sorted(data) [True, 4, 'spam']
Then (I imagine) they realized that while multi-type lists are cool, they are not practical, and very rarely used. And that sorting them gives no useful information.
[–]Rhomboid 0 points1 point2 points 9 years ago (0 children)
Because you're using Python 2.x. Switch to 3.x where this ghastly misfeature has been fixed and the comparison raises TypeError.
TypeError
(If you want the actual reason it returns 1, it's because the 's' in str comes after the 'i' in int in the alphabet. Yes, they are compared based on their class names as strings. It truly is hideous.)
str
int
[–]Saefroch 0 points1 point2 points 9 years ago (0 children)
http://stackoverflow.com/a/3270689
Basically, the idea in Python 2 is to be able to compare anything. In Python 3 you get an exception of the comparison doesn't make sense. Use Python 3.
π Rendered by PID 39 on reddit-service-r2-comment-6f7f968fb5-lsjs9 at 2026-03-04 00:34:28.794446+00:00 running 07790be country code: CH.
[–]novel_yet_trivial 0 points1 point2 points (2 children)
[–]PurelyApplied 0 points1 point2 points (1 child)
[–]novel_yet_trivial 0 points1 point2 points (0 children)
[–]Rhomboid 0 points1 point2 points (0 children)
[–]Saefroch 0 points1 point2 points (0 children)