1

Possible Duplicate:
How does Python compare string and int?

Can any one explain the below.how is the 'a' compared to 1 Internally is a and 1 ASCII val is compared or how is it i.e, there is some conversion happening with 'a' and then compared or how is this.Please explain

>>> 'a' > 1
True
>>> 'a' > 'b'
False
Community
  • 1
  • 1
Rajeev
  • 44,985
  • 76
  • 186
  • 285
  • 3
    seems like a duplication of: http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int – WeaselFox Feb 16 '12 at 10:04

2 Answers2

1

Different types are compared lexigraphically, and "int" is < "string".

In python 3.x, it changes this so different types aren't comparible.

Bool < Int:

In [15]: True > 5
Out[15]: False

List > Int:

In [14]: [1, 2] > 5
Out[14]: True

Tuple > List:

In [16]: (1, 2) > [1, 2]
Out[16]: True

And for your example: Str > Int:

In [17]: '1' > 5
Out[17]: True

And so on and so forth.

TyrantWave
  • 4,583
  • 2
  • 22
  • 25
0

from the manual :

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

so "str" is greater then "int"

WeaselFox
  • 7,220
  • 8
  • 44
  • 75