📚EPISODE 3: UNDERSTANDING TUPLES IN PYTHON – DARCHUMSTECH SERIES
on
Get link
Facebook
X
Pinterest
Email
Other Apps
📗 Episode 3: Understanding Tuples in Python – DarchumsTech Series
Welcome to Episode 3 of the Data Structures in Python series. Today we’re diving into Tuples, a simple but powerful structure used for storing collections of data.
📦 What is a Tuple?
A Tuple is an ordered and immutable collection of items. Once a tuple is created, you cannot change its contents. Tuples use parentheses () instead of square brackets.
🛠Creating and Accessing Tuples
You can define a tuple using parentheses. Items are accessed by index, just like lists.
person = ("John", 30, "USA")
print(person[0]) # John
print(person[-1]) # USA
🎯 Why Use Tuples?
Immutable: Protects data from accidental changes
Faster: Performance advantage over lists
Used as keys: Can be dictionary keys (lists cannot)
Memory efficient: Smaller memory footprint
🔄 Tuple Unpacking
You can assign each element in a tuple to a variable in a single line.
name, age, country = person
print(name) # John
print(age) # 30
print(country) # USA
⚠️ Tuple Immutability
Trying to modify a tuple will raise an error:
person[0] = "Jane" # ❌ This will cause a TypeError
🎮 Try It Yourself
📌 Summary
Tuples are ordered and immutable.
Ideal for fixed data collections like coordinates, RGB values, or config data.
Support indexing, slicing, and unpacking.
🚀 Coming Up Next...
In Episode 4, we’ll explore Sets in Python — perfect for uniqueness and fast membership testing!
Comments
Post a Comment