Data Science Day 9 Tuples
Tuples in Python
Data Science Day 9 Tuples
Tuples in Python
Photo by Shubham Dhage on Unsplash
Tuples
In Python, tuples are collections of elements that are ordered and unchangeable. Their immutability — the inability to alter their contents after creation — distinguishes them from lists.
Important Features of Tuples:
· Ordered: Depending on where they are inserted, the elements in a tuple maintain a particular order.
· Immutable: A tuple’s elements cannot be changed, added, or removed once it has been created.
· Heterogeneous: Elements of various data types, such as integers, strings, and booleans, can be stored in tuples.
· Indexed: Like lists, elements can be accessed through zero-based integer indexing.
Creation
Tuples are typically created by enclosing comma-separated elements within parentheses ().
# Empty tuple
empty_tuple = ()
# Tuple with multiple elements of different types
mixed_tuple = (1, ‘hello’, True, 3.14)
# Tuple with a single element (requires a trailing comma)
single_element_tuple = (“item”,)
How to Access Elements: To access an element, use its index enclosed in square brackets.
my_tuple = (‘apple’, ‘banana’, ‘cherry’)
print(my_tuple[0]) # Output: apple
print(my_tuple[1]) # Output: banana
Tuple Operations:
While tuples are immutable, you can perform operations that return new tuples:
Concatenation: Joining two or more tuples using the + operator.
Repetition: Repeating a tuple multiple times using the * operator.
Slicing: Extracting a sub-tuple using slicing notation.
Immutability
Important facets of tuple immutability include:
No assignment of items: A tuple’s elements at a given index cannot be changed directly. A TypeError will be raised if you try to do this.
my_tuple = (1, 2, 3)
# my_tuple[0] = 5 # This will raise a TypeError
No elements can be added or removed: Because tuples’ size and content are fixed at creation, they lack methods like append(), insert(), and remove().
Making a “new” tuple: If you need to change the contents of an existing tuple, you have to make a new one with the necessary modifications, thereby replacing the old one.
original_tuple = (1, 2, 3)
new_tuple = original_tuple + (4,) # Creates a new tuple (1, 2, 3, 4)
It’s important to keep in mind that although a tuple is immutable in and of itself, it can be altered if it contains mutable elements, such as lists or dictionaries.
mutable_in_tuple = ([1, 2], ‘a’, 3)
mutable_in_tuple[0].append(3) # Modifies the list inside the tuple
print(mutable_in_tuple) # Output: ([1, 2, 3], ‘a’, 3)
In this scenario, the value of the mutable object that the tuple references can change, but the references to its elements stay the same. Even if the internal content of the list has changed, the tuple still contains the same list object.
Indexing
1. Positive Indexing:
Elements are accessed by their position, starting from 0 for the first element.
my_tuple = (“apple”, “banana”, “cherry”, “date”)
first_element = my_tuple[0] # “apple”
third_element = my_tuple[2] # “cherry”
2. Negative Indexing:
Elements are accessed from the end of the tuple, with -1 representing the last element.
my_tuple = (“apple”, “banana”, “cherry”, “date”)
last_element = my_tuple[-1] # “date”
second_to_last = my_tuple[-2] # “cherry”
index() Method:
This method returns the index of the first occurrence of a specified value within the tuple.
my_tuple = (1, 5, 2, 8, 5, 3)
index_of_5 = my_tuple.index(5) # 1
Type_of Indexing
Positive Indexing
Indexing starts from 0 for the first element.
my_tuple = ("apple", "banana", "cherry", "date")
print(my_tuple[0])
print(my_tuple[2])
'''Output -
apple
cherry'''
Negative Indexing
Indexing starts from -1 for the last element, moving backward.
my_tuple = ("apple", "banana", "cherry", "date")
print(my_tuple[-1])
print(my_tuple[-2])
'''Output -
date
cherry'''
Positive Indexing: The index number starts from 0, so the first element is at index 0, the second at index 1, and so on.
Negative Indexing: The index number starts from -1 for the last element, -2 for the second-last, etc.
Slicing
A range of elements can be extracted using slicing, specifying a start and end index (the end index is exclusive).
my_tuple = ("apple", "banana", "cherry", "date", "elderberry")
slice1 = my_tuple[1:4] # ("banana", "cherry", "date")
slice2 = my_tuple[:3] # ("apple", "banana", "cherry") (from beginning to index 2)
slice3 = my_tuple[2:] # ("cherry", "date", "elderberry") (from index 2 to end)
slice4 = my_tuple[:] # ("apple", "banana", "cherry", "date", "elderberry") (a copy of the entire tuple)
Slicing Type
Basic Slicing
Returns elements from index start to end — 1.
my_tuple = ("apple", "banana", "cherry", "date", "elderberry")
print(my_tuple[1:4])
'''Output -
('banana', 'cherry', 'date')'''
Omitting Start Index
Starts from the beginning of the tuple.
print(my_tuple[:3])
'''Output -
('apple', 'banana', 'cherry')'''
Omitting End Index
Slices from the given index to the end.
print(my_tuple[2:])
'''Output -
('cherry', 'date', 'elderberry')'''
Complete Slice
Copies the entire tuple.
print(my_tuple[:])
'''Output -
('apple', 'banana', 'cherry', 'date', 'elderberry')'''
Using Step Value
Skips elements according to the given step.
print(my_tuple[::2])
'''Ouput -
('apple', 'cherry', 'elderberry')'''
start → Index where the slice begins (default is 0)
end → Index where the slice stops (excluded)
step → Number that defines the jump between indices (default is 1)
When to Use Tuples:
Tuples work well for groups of objects that shouldn’t change, like:
· representing fixed data sets, such as database records or coordinates.
· arguments passed into the function that shouldn’t be changed.
· multiple values being returned by a function.
메타데이터
- post_id
- 91b73635fda0
- slug
- data-science-day-9-tuples-91b73635fda0
- url
- https://medium.com/@tharunbs352/data-science-day-9-tuples-91b73635fda0
- canonical_url
- https://medium.com/@tharunbs352/data-science-day-9-tuples-91b73635fda0
- author_url
- https://medium.com/@tharunbs352
- status
- ok
- fetched_at
- 2026-07-15 03:30:49