1

While doing some coding challenges I ran across a very strange language quirk. The program I was writing was a lowest number finder. So basically you inputted a list of numbers and the program returned the lowest number. All went well for a while, like I said its a rather basic program, but when I started to input 2 or 3 digit numbers, the Boolean logic seemed to break down. Out of a list of 2 digit and 1 digit numbers like:
x = [10, 5, 10]

the program would return 10 as the lowest number. But in a regular 1 digit list the correct lowest value would be returned. So eventually I figured out that the bug was because I forgot to convert the string type to an integer type and once that was fixed the program ran flawlessly. But that still leads to an interesting question, why did Python believe that "10" was less than "5" but "9" was greater than "5"? To deliberately prove that Python believes that fact and it was not some outside factor in my program, I opened up a python interpreter and physically entered "10" < "5" and the interpreter returned True. Any ideas why this would be happening? Thank you in advance!

3

2 Answers 2

3

Python uses dictionary order aka Lexicographical Order when comparing strings, even if the strings only contain digits.

In a dictionary, "adam" < "eve" because "a" < "e". Similarly, "10" < "5" because "1" is less than "5".

Sign up to request clarification or add additional context in comments.

Comments

1
"10" < "5" 

because

"1" < "5"

the same as how with english

"at" < "i"

in alphabetical order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.