When we execute the program print('a' > 'b') it gives us the answer False.

When we execute the program print('a' > 'A') it gives us the answer True.

Please help me with a detailed explanation.

5

Best Answer


when comparing characters using the < or > it converts it to an integer.

according to ASCII Table

Meaning:

  • a is 97 decimal
  • b is 98 decimal
  • A is 65 decimal
  • B is 66 decimal

therefor:

print('a' > 'b') is false because print(97 > 98)

and then:

print('a' > 'A') is true because print(97 > 65)

Please check the ascii code of the characters.

enter image description here

You can also check it using python

>>> ord('a')97>>> ord('b')98>>> ord('A')65

Also, the reverse can be obtained as

>>> chr(97)'a'>>> chr(98)'b'>>> chr(65)'A'

Firstly, have a look at the ASCII table where you can find the numerical mapping of all the standard characters.

Did you see the values of 'a', 'b', and 'A'?

'a' == 97

'b' == 98

'A' == 65

That is why, ('a' > 'b') is false and ('a' > 'A') is true.

This is because on the ASCII (American Standard Code For Information Interchange) CHART the letter "a" equates to 97 (in decimal values) while the letter "b" equates to 98 (in the decimal values).

Therefore when you type in print('a' > 'b'), Python compares the aforementioned decimal values and replies "FALSE" because under the hood is just comparing literally 97 to 98. The same goes to print('a' > 'A'), it will be comparing 97 to 65; reason why you will get a "TRUE."

Computers can understand only numbers. So, every character has a numerical equivalent. Refer to more: https://www.asciitable.com/.Decimal equivalent ofA=65, B=66, a=97, b=98

Clearly,"A"<"B","B" <"C", "a"> "A"