Tuples
Introduction to Tuples
Tuples are immutable sequences in Python, similar to lists but with one key difference: they cannot be modified after creation. Tuples are perfect for storing fixed collections of data that shouldn't change, such as coordinates, RGB color values, or database records.
Tuples are more memory-efficient than lists and can be used as dictionary keys (since they're immutable), making them useful in many scenarios.
Creating Tuples
Tuples are created using parentheses () with items separated by commas. You can also create a tuple with just a comma (parentheses are optional in some cases):
Accessing Tuple Items
You access tuple items the same way as lists, using indexing and slicing:
Tuple Methods
Tuples have only two methods since they're immutable: count() and index():
When to Use Tuples
✅ Use Tuples When:
• Data should not change (immutable)
• You need a dictionary key (tuples are hashable)
• Returning multiple values from a function
• Memory efficiency matters (tuples use less memory)
• Data integrity is important (prevents accidental modification)
💡 Use Lists When:
• You need to modify the collection
• You need list methods (append, remove, etc.)
• Order might change