Tuples and Sets in Python
As you learn Python, you’ll come across several ways to store and organize data. Two important types are tuples and sets.
They might look similar at first, but they behave quite differently and serve different purposes.
Tuples
A tuple is an ordered collection of items that cannot be changed after it’s created.
Creating a Tuple
my_tuple = (1, 2, 3)
Tuples are useful when the data should stay constant, such as GPS coordinates or a student's birthdate.
Characteristics of Tuples
- Tuples are ordered, so the position of each item matters.
- They cannot be changed (immutable).
- You can still access items by index, for example
my_tuple[0].
Sets
A set is an unordered collection of unique elements.
Creating a Set
my_set = {1, 2, 3}
Sets are useful when you need to store unique values, such as a list of unique IDs or a collection of unique items.
Characteristics of Sets
- Sets remove repeated values automatically.
- You can add or remove items at any time.
- The order is not guaranteed because sets are unordered.
Quiz
0 / 1
What is a key characteristic of a set in Python compared to a tuple?
Sets are ordered.
Sets are immutable.
Sets automatically remove duplicate values.
Sets allow indexing like lists.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help