What is a tuple?
A tuple in Python is used to store heterogeneous data types and is enclosed by parenthesis (). The structure is almost similar to a list but the list is mutable, on the other hand, the tuple is immutable.
Mutable -we can change the elements in a data series.
Example of modifying a list:
my_list = [1, 2, 3]
my_list[1] = 100 # This is allowed
print(my_list) # Output: [1, 100, 3]
Immutable –we can not change the elements in a data series.
Example of attempting to modify a tuple:
my_tuple = (1, 2, 3)
# my_tuple[1] = 100 # This will raise an error because tuples are immutable
Here are some functions given below which we are using in tuple:
1. len()
Returns the number of elements in a tuple.
t = (1, 2, 3, 4)
print(len(t)) # Output: 4
2. max()
Returns the maximum value in a tuple.
t = (10, 20, 30)
print(max(t)) # Output: 30
3. min()
Returns the minimum value in a tuple.
t = (10, 20, 5)
print(min(t)) # Output: 5
4. sum()
Returns the sum of all elements in a tuple (if numeric).
t = (5, 10, 15)
print(sum(t)) # Output: 30
5. sorted()
Returns a sorted list of the tuple elements.
t = (30, 10, 20)
print(sorted(t)) # Output: [10, 20, 30]
6. tuple()
Converts an iterable (like a list) into a tuple.
lst = [1, 2, 3]
t = tuple(lst)
print(t) # Output: (1, 2, 3)
7. index()
Returns the index of the first occurrence of a specified value.
t = (10, 20, 30, 20)
print(t.index(20)) # Output: 1
8. count()
Counts the number of occurrences of a specified value in a tuple.
t = (10, 20, 20, 30)
print(t.count(20)) # Output: 2
9. in (Membership Testing)
Check if an element exists in a tuple.
t = (1, 2, 3)
print(2 in t) # Output: True
10. Tuple Unpacking
Extracts elements of a tuple into separate variables.
t = (1, 2, 3)
a, b, c = t
print(a, b, c) # Output: 1 2 3
11. Concatenation (+)
Combines two tuples into one.
t1 = (1, 2)
t2 = (3, 4)
print(t1 + t2) # Output: (1, 2, 3, 4)
12. Repetition (*)
Repeats the elements of a tuple.
t = (1, 2)
print(t * 3) # Output: (1, 2, 1, 2, 1, 2)
13. Slicing
Extracts a portion of the tuple using slicing.
t = (10, 20, 30, 40, 50)
print(t[1:4]) # Output: (20, 30, 40)
14. Negative Indexing
Accesses elements from the end of the tuple.
t = (10, 20, 30, 40)
print(t[-1]) # Output: 40
15. Nesting
Tuples can contain other tuples (or other data structures) as elements.
t = (1, 2, (3, 4), 5)
print(t[2]) # Output: (3, 4)
print(t[2][1]) # Output: 4
16. all()
Checks if all elements in the tuple are true (or if the tuple is empty).
t = (True, 1, “Hello”)
print(all(t)) # Output: True
t2 = (0, False, 10)
print(all(t2)) # Output: False
17. any()
Check if any element in the tuple is true.
t = (0, False, 10)
print(any(t)) # Output: True
18. Tuple Comparison
Tuples can be compared lexicographically (element by element).
t1 = (1, 2, 3)
t2 = (1, 2, 4)
print(t1 < t2) # Output: True
19. Immutable Property
Tuples are immutable, meaning their elements cannot be changed.
t = (1, 2, 3)
# t[0] = 10 # This will raise a TypeError
20. Membership Testing (not in)
Checks if an element is not in a tuple.
t = (1, 2, 3)
print(4 not in t) # Output: True
21. Reversing a Tuple
Reverses the order of elements in the tuple.
t = (1, 2, 3, 4)
print(t[::-1]) # Output: (4, 3, 2, 1)
22. Zipping Tuples
Combines multiple iterables into a tuple of tuples.
a = (1, 2, 3)
b = (“a”, “b”, “c”)
zipped = tuple(zip(a, b))
print(zipped) # Output: ((1, ‘a’), (2, ‘b’), (3, ‘c’))
23. Deleting a Tuple
Tuples can’t be deleted partially, but you can delete the entire tuple.
t = (1, 2, 3)
del t
# print(t) # This will raise a NameError as the tuple is deleted
24. Tuple to String Conversion
Joins elements of a tuple into a string (if all elements are strings).
t = (‘Hello’, ‘World’)
print(‘ ‘.join(t)) # Output: “Hello World”
25. Tuple of Single Element
Creating a tuple with one element requires a trailing comma.
t = (10,) # This is a tuple
print(type(t)) # Output: <class ‘tuple’>
not_tuple = (10) # This is an integer
print(type(not_tuple)) # Output: <class ‘int’>
26. Tuple as Dictionary Keys
Tuples (since they are immutable) can be used as keys in dictionaries.
d = {(1, 2): “point1”, (3, 4): “point2”}
print(d[(1, 2)]) # Output: “point1”
27. Using enumerate()
Generates an enumerate object for the tuple, yielding index-value pairs.
t = (‘a’, ‘b’, ‘c’)
for index, value in enumerate(t):
print(index, value)
# Output:
# 0 a
# 1 b
# 2 c
28. Finding the Product of Tuple Elements
Using functools.reduce to calculate the product of numeric tuple elements.
from functools import reduce
t = (1, 2, 3, 4)
product = reduce(lambda x, y: x * y, t)
print(product) # Output: 24