
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How does tuple comparison work in Python?
Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on.
example
>>> a = (1, 2, 3) >>> b = (1, 2, 5) >>> a < b True
There is another type of comparison that takes into account similar and different elements. This can be performed using sets. Sets will take the tuples and take only unique values. Then you can perform a & operation that acts like intersection to get the common objects from the tuples.
example
>>> a = (1, 2, 3, 4, 5) >>> b = (9, 8, 7, 6, 5) >>> set(a) & set(b) {5}
example
You can also use set.intersection function to perform this operation.
>>> a = (1, 2, 3, 4, 5) >>> b = (9, 8, 7, 6, 5) >>> set(a).instersection(set(b)) set([5])
- Related Articles
- How does concatenation operator work on tuple in Python?
- How does the * operator work on a tuple in Python?
- How does tuple destructuring work in TypeScript?
- How does the repetition operator work on a tuple in Python?
- How does the del operator work on a tuple in Python?
- How does comparison operator work with date values in MySQL?
- How does MySQL QUOTE() function work with comparison values?
- How does the 'in' operator work on a tuple in Python?
- How does underscore "_" work in Python files?
- How does garbage collection work in Python?
- How does class inheritance work in Python?
- How does issubclass() function work in Python?
- How does isinstance() function work in Python?
- How does Overloading Operators work in Python?
- How does ++ and -- operators work in Python?

Advertisements