Using min/max for a set of variables of different types


#1

I'm currently on Functions 14. I, of course, finished the task easily, but I decided to experiment, and...
Well, min() and max() seem to work with different types.
You can see my code below. And it returns False. If I switch False to True - it returns True.
I have a simple question, two actually:
This doesn't work on PyCharm for some reason. I get this error: "TypeError: unorderable types: str() < int()". Why does it work here and why does it return a boolean every time?

# Set minimum to the min value of any set of numbers on line 3!

minimum = min('', 8, False)

print minimum

Thanks in advance.


#2

Well first, PyCharm I believe is on Python 3 while Codecademy is still on Python 2. Codecademy is updating to Python 3 but that would explain why it would not work without error in PyCharm. Secondly, obviously, when you ask it to print the minimum in it is going to return the smallest of the given arguments. Out of those given, the boolean False is the smallest. This would seem to indicate that a boolean is smaller than a string and number. This would explain while it always shows up as the minimum.


#3

Might I ask why a boolean is considered smaller than an empty string? Is that because of how many bits it occupies? Or is there any other reason to that?


#4

this is interessting:

Python 3.0 has simplified the rules for ordering comparisons:

The ordering comparison operators (<, <=, >=, >) raise a TypeError exception when the operands don’t have a meaningful natural ordering. Thus, expressions like: 1 < '', 0 > None or len <= len are no longer valid, and e.g. None < None raises TypeError instead of returning False.

so the reason you see false is not because it is the smallest, but because the comparison is not meaningful, causing to return false in python2, and a error in python3.

credit to this stackoverflow question


#5

Very interesting. Sorry @alucard_i I was wrong xD


#6

Wait, wait, wait. Then why does it return True if I do this?

print min('', 8, True)


#7

hm... then it seems it does compare. Interesting. Boolean are special values, because of how they implemented they appear to be the smallest


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.