0

I wish to understand,

"apple" > 10 return True always.

I have mistakenly compared string with integer. instead of raising error it returns boolean.

I want to reason behind it..

When checking string with greater than Number it always return True.

eg 1: '' > 0 = True
eg 2: 'something' > 10 = True

etc, etc.

what it means actually?

I have tried, bytes of string, id etc. i am not sure what it means.

i can understand when if its string > string

here will get result based on sorting order something like below,

>>> 'a' >= 'a'
True
>>> 'apple' >= 'a'
True
>>> 'apple' > 'a'
True
>>> 'apple' > 'b'

Note: in Python 3 it will raises an error. what about python 2.x?

I know its sorting based. number has less precedence than string. but, is that precedence is based on memory consumption?

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • 1
    for python 3.6 in a jupyter notebook that raises `TypeError: '>' not supported between instances of 'str' and 'int'` – Florian H Jun 26 '19 at 13:28

1 Answers1

1

I found this definition:

For python2:

"If the comparison is between numeric and non-numeric, the numeric (int, float) is always less than non-numeric and if the comparison is between two non-numeric it's done by lexicographical ordering(str) or alphabetical order of their type-names(list, dict, tuple)."

For python3:

It will return TypeError.

Igor Servulo
  • 371
  • 1
  • 9
  • I know that.. but, my question was - it about memory size based precedence? – Mohideen bin Mohammed Jun 26 '19 at 13:31
  • As it was defined, it's done by lexicographical ordering. Its a mathematics generalization of the way the words are ordered. The precedence is set by the ordering of the letter or number in a sequence. Altough, python didn't specify how it its defined on the logic comparison for the precedence (https://docs.python.org/2/reference/expressions.html#value-comparisons). – Igor Servulo Jun 26 '19 at 13:50