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?

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]
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. )

25 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'
3 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.

3 Likes

Well, you can ask Python!

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

17 Likes

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… :slight_smile:

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.

19 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.

1 Like

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.

1 Like

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

2 Likes

I do not understand it either

2 Likes

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.

[1,2] < [1,3] = True
1 < 1 = False
2 < 3 = True

[1,2,5] < [1,3,5] = True
1 < 1 = False
2 < 3 = True
5 < 5 = False

[1,4,5] < [1,3,4] = False
1 < 1 = False
4 < 3 = False
5 < 4 = False

When I first looked at it, I assumed it was using AND where it would only return True if all values were True. And that’s what I get for assuming.

3 Likes

then Why this is false ?

print( [2,2] < [1,3])

1 Like

@script7359737975 @smarti3 @rintina @jonathandesousa

Welcome to the forums!

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.

Hope this helps!

1 Like

Python compares the lists element by element, starting from the first element.

In the case of [1, 2] < [1, 3]:

  1. The first elements of both lists are compared: 1 and 1. Since they are equal, Python moves to the next element.
  2. The second elements are compared: 2 and 3. Here, 2 is less than 3.

Since the first unequal elements have been found and 2 < 3, the overall comparison returns True. Thus, [1, 2] < [1, 3] evaluates to True.

For the comparison [2, 2] < [1, 3]:

  1. The first elements are compared: 2 from the first list and 1 from the second list.
  2. Since 2 is greater than 1, Python determines that the first list is not less than the second list.

As a result, [2, 2] < [1, 3] evaluates to False.

Remember, the lists are being compared as a whole.

  • Is [1,2] less than [1,3]? Yes, overall it is less (just like ab would come before ac alphabetically).
  • Is [2,2] less than [1,3]? No, overall it is greater (just like bb would not come before ac alphabetically).