using > < for tuple , list,... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: using > < for tuple , list,... (/thread-41552.html) |
using > < for tuple , list,... - akbarza - Feb-05-2024 hi I saw on the net that in a test, two tuples were compared with the > sign. how can I know that the > or < or ... is applicable for a type such as tuples or lists or dicst,... thanks RE: using > < for tuple , list,... - rob101 - Feb-05-2024 The best way to learn, is to explore the concept. Here... t1 = ("A", "B", "C") t2 = ("A", "B", "D")... t2 > t1 because t2 has a "D" as one of its items.And here... t1 = ("A", "B", "C") t2 = ("B", "A", "C")... t2 > t1 because "B" is the first item in t2 and "B" > "A" .
RE: using > < for tuple , list,... - perfringo - Feb-05-2024 From documentation (Value Comparisons): Quote:Sequences compare lexicographically using comparison of corresponding elements. This means that order is important. This makes sense in strings (this is the way how words are ordered in any dictionary) but with tuples and lists one must be aware what practical implications of lexicographical comparison have on greater or less: >>> a = (2, 1) >>> b = (1, 4, 5, 10) >>> a < b False1 is smaller than 2 and so b is smaller than a. RE: using > < for tuple , list,... - deanhystad - Feb-05-2024 You can tell if an object can do comparison by looking to see if it has comparison dunder methods. Here I see that the tuple x can do __eq__, __ge__, __gt__, __le__, __lt__ and __ne__ which correspond to the comparison operators =, >=, >, <=, <, !=. You still have to read the documentation for the class, because this only tells you that comparison is supported, not how the comparison is done. https://docs.python.org/3/tutorial/datastructures.html Quote:5.8. Comparing Sequences and Other Types For lists an tuples, the items in the list or tuple must also be comparable. x = ('A', 'B', 'C') y = (1, 2, 3) print(x > y)
|