1

Why a tuple can be compared with a number? What is the semantic of it? Does > and == have different semantic?

>>> (1,2)>0
True
>>> (1,2)>11111111111111
True
>>> (1,2)<5
False
>>> (1,2)>1.578
True
>>> (5,5)==5
False
>>> (5,5)!=5
True

This is not the same as comparing against a tuple with one element (which is compared lexicographically):

>>> (5,5)>6
True
>>> (5,5)>(6,)
False

(Note: this is a different issue than comparing the tuple content against a value)

Community
  • 1
  • 1
Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46
  • @MartijnPieters The question in not a duplicate of [How does Python compare string and int?](http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int) altough the answer might be found there. – Lorenzo Belli Jan 06 '17 at 10:35
  • The answer is *exactly the same*. Arbitrary types are not compared on their value, only on their type name, with numbers before everything else. It doesn't matter if you have a tuple or a string here, numbers are still sorted first. – Martijn Pieters Jan 06 '17 at 10:35
  • Given the specific title 'How does Python compare string and int?' I couldn't find it. It's a too broad answer for a more specific title. Search in SO is mostly based on title and that could easily be missed. – Lorenzo Belli Jan 06 '17 at 10:38
  • I'm not sure what you are saying there. I didn't expect you to have found it directly, no. – Martijn Pieters Jan 06 '17 at 10:39
  • I wouldn't have found it directly. Either we change the title for that answer to a more generic one, or we create a new broader question whose answer is similar to the already present one. That would remove all these duplicates. – Lorenzo Belli Jan 06 '17 at 10:44
  • If you want to discuss that, feel free to bring it to the attention of the [Python chat room](https://chat.stackoverflow.com/rooms/6/python). – Martijn Pieters Jan 06 '17 at 11:40

0 Answers0