-2

I am not getting how does python evaluates this expression.What does it consider while evaluating this kind of expression?

The code snippet: print('a'>'b')

3 Answers3

1

If you compare the strings in python, it will consider the ascii values of the characters. Ascii value of 'a' is 97 and Ascii value of 'b' is 98. So basically you are asking if 97 > 98 which is false. That is the reason ('a' > 'b') evaluates to false.

Below are some examples

'a' > 'b' -> false
'ac' > 'ab' -> true (here first characters are equal. So it will compare 'c' and 'b')
'ac' > 'ba' -> false (here first characters are different. So it will just compare first characters)
kadina
  • 5,042
  • 4
  • 42
  • 83
0

Python uses Lexicographical ordering for strings. This means that it uses the Unicode point number to order characters.

Reference: https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types

You may find this useful: https://en.wikipedia.org/wiki/List_of_Unicode_characters

0

It's better to see the ASCII code table. I referred hexadecimal number below.

The 'a' corresponds to the number 61 and 'b' corresponds to the number 62 in the ASCII code table.

61 < 62.

Thus, "print('a'>'b')" prints false.

Ps. "print('a'<'b')" prints true. "print('#'<'$')" also prints true.

https://simple.wikipedia.org/wiki/ASCII