My code gave a weird output for Conditionals & Control Flow in Learn Python 2

Well, I did something slightly silly, and in order to complete one of the boolean tasks, I wrote the following:
print “gorilla” > “elephant”
This gave an output of True!

When I write the following:
print “blue whale” > “godzilla”
The output came as False!

Now, my question here is that, on what basis did Python assign true or false to these seemingly random strings?

Writing string1 > string2 is makes python actually check len(string1) > len(string2) since these type of comparison operators only work with numbers, unlike =.

Or, the more unlikely scenario, is that python has gotten so smart it tries to interpret images of the animals as you enter them. I think this scenario is less likely, but I’d have to check the documentation for 3.8… :sweat_smile:

You can hunt down that info at the following (it’s the last bullet point approx 1 page down from link, keep reading from that point on strings and it explains some of the jargon)-
https://docs.python.org/3/reference/expressions.html#value-comparisons

3 Likes

Ah, but this surely cannot be the case, as len(“gorilla”)=7 and len(“elephant”)=8, which should make the first inequality false, and len(“blue whale”)=10 and len(“godzilla”)=8, which should make the second inequality true…

Ah yes, this makes sense now, so they are compared based on the Unicode code points of each letter one by one?

Ah yes, tip my hat to @tgrtim!!

I stand corrected.

1 Like