Topic 3-1 Comparison Operators

Hi, I am new to programming and I am currently in topic 3-1 Comparison Operators in Python Mastery.
In the video it shows that

print(bool(“bag” > “apple”))
print(bool(“bag” == “BAG”))
print(ord(“b”))
print(ord(“B”))

Results are

True
False
98
66

The question I want to ask is below:

print(bool(“bag” == “bat”))

Results

False

Why isn’t the result “True”?
What I understand is that, since the letter “b” comes after the letter “a”, that’s why the results are “True”.
And the order of the letter “B” should be smaller than the letter “b”, which results in “False” as we are comparing in equality.

And now, since “bag” and “bat” starts with the same letter “b”, shouldn’t it be equal?
Shouldn’t be the results “True”?

I tried to make my question understandable, please let me know if there is any questions regarding my query.
Thanks in advance!

Python checks the Unicode value for the first two characters. If they are equal then it checks the following character until it finds a difference. This is called Lexicographical ordering (doc).

E.x. So in the “bag” == “bat” python looks at the first two letter in each word (“ba”) and finds that they are the same. Then it moves on to “g” == “t” and returns false because they have different Unicode values.

I hope this makes sense, If you have any further questions then feel free to reach out.

Credit:

1 Like

Thank you so much for the clarification.
Now it make sense!
I thought it only captures the first letter, which doesn’t make sense LOL.