This content originally appeared on DEV Community and was authored by Aditi Sharma
🔹 What are Tuples?
• Tuples are ordered and immutable collections.
• They can store multiple items like lists, but unlike lists, they cannot be modified (no append, remove, etc.).
• Defined using parentheses ().
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # apple
🔹 Key Properties of Tuples
✅ Ordered → items have a fixed sequence
✅ Immutable → can’t change once created
✅ Allow duplicates → can store repeated values
âś… Can store mixed data types
🔹 Why Use Tuples?
• Safer than lists if data shouldn’t change
• Faster than lists (performance benefit)
• Can be used as dictionary keys (since immutable)
🔑 Tuple Operations & Examples
Accessing Elements
numbers = (10, 20, 30, 40)
print(numbers[1]) # 20
Tuple Packing & Unpacking
person = ("Alice", 25, "Engineer")
name, age, profession = person
print(name) # Alice
Concatenation & Repetition
t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2) # (1, 2, 3, 4)
print(t1 * 2) # (1, 2, 1, 2)
Nesting Tuples
nested = (1, (2, 3), (4, 5))
print(nested[1][1]) # 3
🎯 Reflection
Tuples may look similar to lists, but their immutability makes them useful in situations where data must remain constant.
⚡ Next, I’ll be diving into Sets and Set operations in Python!
This content originally appeared on DEV Community and was authored by Aditi Sharma
Aditi Sharma | Sciencx (2025-09-04T10:36:06+00:00) 🚀 Day 7 of My Python Learning Journey – Tuples in Python. Retrieved from https://www.scien.cx/2025/09/04/%f0%9f%9a%80-day-7-of-my-python-learning-journey-tuples-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.