 Can relational operators be applied to values other than numbers?

Question

In the context of this exercise, can relational operators be applied to values other than numbers?

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]
23 Likes

I think the second time should compare 2 and 3.
(This is True because the first items match, and 1 <3 for the second item. )

22 Likes

You have mentioned lexicographical ordering.
What if I compare str() value of a number to letter…

Is it possible?

Well I did check this in editor and it says that “a” > str(11).

did edit string value for number

1 Like

Show us what you tried!

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'
2 Likes

I did something like this

def comparator(x,y):
if x > y:
return x
if y > x:
return y
print(comparator("a",str(11)))
print(comparator(str(11),"a"))

and I do get “a” in return

don’t know if this is right thing to do that’s why I’m asking
My question was if letters comes before numbers of course as a string values.

2 Likes

The built-in function ord(ch) returns the Unicode code point for a character. It’s inverse is chr(n)

print(ord("a"))
print(chr(97))

# Output:
97
a

Now, the string representation of 11 consists of two consecutive characters, “1” and “1”

print(ord("1"))
print(chr(49) + chr(49))

# Output:
49
11

So (as comparisons compare only individual elements (here, characters) up to the point where there is a difference), 97 > 49 returns True

13 Likes

Thanks Patrick
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.

17 Likes

Yes that makes much more sense. If 1=1 then 2<3 should be next. Is this right?

3 Likes

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.

I still don’t understand how the third example works.

How is it true if 1<1 is false (because 1 is not less than 1, it is equal to it). And how does python check? I am still really confused.

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.

hey, sorry- I still didnt get it , can someone explain the steps on how this statement is checked and why its True ?
thank you