I can't figure out why the following code gives a True, False, True result.
print("3" < "4")
print("3" < "10")
print("3" < "30")
I understand the 1st and 3rd code but why doesn't the print("3" < "10")
give a True
response.
thanks
I can't figure out why the following code gives a True, False, True result.
print("3" < "4")
print("3" < "10")
print("3" < "30")
I understand the 1st and 3rd code but why doesn't the print("3" < "10")
give a True
response.
thanks
From the python docs on comparison operations:
Strings are compared lexicographically using the numeric equivalents (the result of the built-in function ord()) of their characters.
The character comparison starts with the first character and steps through the strings being compared until a character difference is found.
Your 2nd statement returns False
since:
ord("3") > ord ("1")
The link suggested will give you elaborate answer. However since I am assuming that you are beginning programming, I am trying to take a very simple approach to explain.
For strings, as soon as you start writing:
"ab"<"bc"
"30"<"400"
Imagine this:
['a', 'b'] <['b', 'c']
['3', '0'] <['1', '0', '0']
Now the comparison is made on the first elements of both list and only moves to next element if they are equal:
The first one should be True. The second one should be False.
Follow the same logic and try:
'30'<'31'
It should evaluate to True.