In the context of this exercise, can relational operators be applied to values other than numbers?
Answer
Yes, relational operators like >, <, >= and <= can be applied to values other than just numbers. They can also be applied to sequences of values like strings and lists. Strings are similar to a list of characters.
When comparing such values, the comparisons are done using ‘lexicographical’ (alphabetical) ordering. The first two items are compared, then the second two items, and so on. When any of the items are different, it will compare them based on their lexicographical ordering.
When comparing any two values, they must share the same type, or there will be a TypeError thrown.
Example
# This is False, "a" comes before "b" in lexicographical ordering.
"a" > "b"
# Lexicographical order follows alphabetical ordering.
# "a" < "b" < "c" …
# This is True because the first items match, and 1 < 3 for the second item.
[1, 2] < [1, 3]
print("a" > str(11))
print("a" > 11)
# Output:
True
Traceback (most recent call last):
File "C:\Users\path\to\test.py", line 3, in <module>
print("a" > 11)
TypeError: '>' not supported between instances of 'str' and 'int'
Thanks Patrick
now that was some answer!..
So order of values in strings is “directed” by Unicode number and each individual symbol number.
I bet there’s no reason why someone would ever compare like this in world that contain decimal numbers…
So if I understand right strings are compared by pairing symbols and comparing each symbol separately… by unicode order.
When sorting strings, the first letter of each string is compared, first, then the second letter, then the third, and so on.
aaa
baa
aba
aab
On the first pass, we get
aaa
aba
aab
baa
On the second pass we get,
aaa
aab
aba
baa
Under the hood all character data is sorted by ordinal, ascending (lowest first). If we look at an ASCII table we see that numbers come before uppercase letters which come before lowercase letters. Their Unicode equivalents are ordered the same whether Latin alphabet or otherwise.
Python sort() and sorted() have numeric recognition so can order numbers by their values, not their ordinal, but some languages such as JavaScript have no such built in skill so will sort 100 as coming before 20. To sort numbers numerically we need to supply a function that handles the ordering by relation.
should this say “and 2 < 3 for the second item.” ?
the first comparison is 1 and 1, and the second comparison is 2 and 3, unless i’m not understanding.
It is a grouping comparison, and the one on the left would be considered as occurring on the left in real space. Use your imagination if no logic exists, and reason it out from there.
It didn’t make much sense to me either until I tried playing around with a few variations and realized that it’s treating it like an OR situation. Each item on the left is being compared to the relative item on the right. If any of them come up as True, then the comparison is treated as True. For it to return a False result, all comparisons must be False.
I am afraid your interpretation @smarti3 is not correct in this case. The example given is not necessarily an exercise for you to evaluate the overall logic but to show you what happens when you compare string characters using comparison operators.
In general, for strings the comparison is made using Unicode order. In layman’s terms it’s alphabetical, ie. a being the smallest, followed by b, and so forth similar in how you would rank words in dictionary order.
In the last example, it’s showing what happens when you are comparing lists (to demonstrate the comparison operators are not limited to just numerical comparisons). When list is compared, Python checks the values in the first position and see which one is greater (in case of characters, which comes later in dictionary). If the first item is equal (as it is the case in the example provided) it moves to the second position and make comparison between them until a definitive order exists between the two lists.