tuple_examples
tuple_examples
max_element = max(tup)
min_element = min(tup)
Explanation: The max() and min() functions return the maximum and minimum elements
in the tuple, respectively.
result = ''.join(tup)
Explanation: The join() method can be used to concatenate elements of the tuple into a
single string.
tup = (1, 2, 3, 4, 3, 5)
value_to_remove = 3
new_tup = tuple(x for x in tup if x != value_to_remove)
Explanation: A new tuple is created using a generator expression that excludes the
specified value.
tup1 = (1, 2, 3, 4)
tup2 = (3, 4, 5, 6)
Explanation: The generator expression creates a new tuple containing elements that are
common between the two input tuples.
tup = (5, 3, 8, 1, 2)
sorted_tup = tuple(sorted(tup))
Explanation: The sorted() function is used to sort the elements of the tuple, and then a
new tuple is created from the sorted list.
total = sum(tup)
print(total) # Output: 15
Explanation: The sum() function returns the sum of all elements in the tuple.
tup1 = (1, 2, 3)
tup2 = (3, 4, 5)
result = tuple(set(tup1).union(tup2))
Explanation: The first element is accessed using index 0, and the last element is
accessed using index -1.
tup = (1, 2, 3, 4, 5)
Explanation: A new tuple is created by converting each integer element to its string
representation.
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9)
Explanation: A generator expression is used to count even elements, and odd count is
calculated based on total count and even count.
tup = (1, 2, 3, 4, 5)
product = 1
for x in tup:
product *= x
result = tuple(split_result)
Explanation: The tuple is split into n equal parts using list comprehension and then
converted back to a tuple
tup = (1, 2, 2, 3, 2, 4, 2)
freq_dict = {}
for element in tup:
freq_dict[element] = freq_dict.get(element, 0) + 1
Explanation: A dictionary is used to store the frequency of each element in the tuple.
Explanation: A new tuple is created by including only those elements from the first tuple
that are not present in the second tuple.
sorted_tup = sorted(tup)
second_largest_element = sorted_tup[-2]
print(second_largest_element) # Output: 15
Explanation: The tuple is sorted in ascending order, and the second-to-last element is
the second largest.
main_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
sub_tuple = (2, 4, 6)
Explanation: The all() function is used to check if all elements in the subset tuple are
present in the main tuple.