Lecture
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.
Lessons in this chapter · Python Programming Review
- 1. Variables and Data Types
- 2. Expressions and Arithmetic
- 3. String Operations
- 4. Lists in Python
- 5. List Methods and Slicing
- 6. Tuples and Sets
- 7. Dictionaries - Keys and Values
- 8. Dictionary Methods and Nesting
- 9. Conditional Statements (if, elif, else)
- 10. Loops - for and while
- 11. Control Flow & Data Structures Recap
- 12. Multiple-choice quiz
- 13. Loop Controls (break, continue, pass)
- 14. Functions - Defining and Calling
- 15. Function Arguments and Return Values
- 16. Variable Scope and Nested Functions
- 17. Exception Handling with try/except
- 18. File Reading and Writing
- 19. CSV File Parsing
- 20. Using Built-in Modules (math, random, datetime)
- 21. Importing External Modules
- 22. Fill-in-the-blank quiz
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