Welcome to Day 6 of your 180-day AI and Machine Learning journey! Today, we’ll explore tuples, which are similar to lists but immutable. Tuples are useful for storing data that should not change, such as coordinates or configurations. By the end of this article, you’ll understand how to create, access, and use tuples effectively.
What Are Tuples?
A tuple is an ordered collection of items, similar to a list. However, tuples are:
Immutable: Once created, their content cannot be changed.
Ordered: Items have a defined order, and you can access them using their index.
Creating a Tuple
You can create a tuple by enclosing items in parentheses ()
, separated by commas.
Example:
python
Copy
fruits = ("apple", "banana", "cherry")
numbers = (1, 2, 3, 4, 5)
mixed_tuple = (1, "apple", 3.14, True)
If you create a tuple with a single item, you must include a comma:
single_item_tuple = ("apple",)
Accessing Tuple Items
You can access items in a tuple using their index, just like with lists.
Example:
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry (last item)
Tuple Operations
Since tuples are immutable, you cannot modify their content. However, you can perform operations like slicing and concatenation.
1. Slicing Tuples
You can extract a portion of a tuple using slicing.
Example:
numbers = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
print(numbers[2:5]) # Output: (2, 3, 4)
print(numbers[:5]) # Output: (0, 1, 2, 3, 4)
print(numbers[5:]) # Output: (5, 6, 7, 8, 9)
2. Concatenating Tuples
You can combine tuples using the +
operator.
Example:
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
combined_tuple = tuple1 + tuple2
print(combined_tuple) # Output: (1, 2, 3, 4, 5, 6)
3. Other Useful Methods
len()
: Returns the number of items in the tuple.index()
: Returns the index of the first occurrence of a specific item.count()
: Returns the number of times a specific item appears.
Example:
numbers = (1, 2, 3, 2, 4, 2)
print(len(numbers)) # Output: 6
print(numbers.index(3)) # Output: 2
print(numbers.count(2)) # Output: 3
When to Use Tuples
Use tuples when you want to ensure the data cannot be changed (e.g., constants, configurations).
Use tuples for heterogeneous data (e.g., storing a person’s name, age, and city).
Practice Exercise
Create a tuple of your favorite colors and print the third item.
Create a new tuple by combining two existing tuples.
Find the index of a specific item in the tuple.
Solution:
colors = ("red", "green", "blue")
print(colors[2]) # Output: blue
more_colors = ("yellow", "purple")
all_colors = colors + more_colors
print(all_colors) # Output: ('red', 'green', 'blue', 'yellow', 'purple')
print(all_colors.index("blue")) # Output: 2
Key Takeaways
Tuples are ordered, immutable collections of items.
Use indexing and slicing to access tuple items.
Use tuples for data that should not change.
What’s Next?
Tomorrow, we’ll explore sets, which are unordered collections of unique items. Sets are useful for tasks like removing duplicates and performing mathematical operations.
Congratulations on completing Day 6! Keep practicing, and see you tomorrow! 🚀