0% found this document useful (0 votes)
10 views3 pages

tuple_examples

Tuples example
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

tuple_examples

Tuples example
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

• Find the maximum and minimum elements in a tuple.

tup = (5, 8, 2, 10, 3)

max_element = max(tup)
min_element = min(tup)

print("Max:", max_element) # Output: Max: 10


print("Min:", min_element) # Output: Min: 2

Explanation: The max() and min() functions return the maximum and minimum elements
in the tuple, respectively.

• Convert a tuple of strings to a single string.

tup = ('H', 'e', 'l', 'l', 'o')

result = ''.join(tup)

print(result) # Output: "Hello"

Explanation: The join() method can be used to concatenate elements of the tuple into a
single string.

• Remove an element from a tuple.

tup = (1, 2, 3, 4, 3, 5)

value_to_remove = 3
new_tup = tuple(x for x in tup if x != value_to_remove)

print(new_tup) # Output: (1, 2, 4, 5)

Explanation: A new tuple is created using a generator expression that excludes the
specified value.

• Find the common elements between two tuples.

tup1 = (1, 2, 3, 4)
tup2 = (3, 4, 5, 6)

common = tuple(x for x in tup1 if x in tup2)

print(common) # Output: (3, 4)

Explanation: The generator expression creates a new tuple containing elements that are
common between the two input tuples.

• Sort a tuple of integers.

tup = (5, 3, 8, 1, 2)

sorted_tup = tuple(sorted(tup))

print(sorted_tup) # Output: (1, 2, 3, 5, 8)

Explanation: The sorted() function is used to sort the elements of the tuple, and then a
new tuple is created from the sorted list.

• Find the sum of all elements in a tuple.


tup = (1, 2, 3, 4, 5)

total = sum(tup)

print(total) # Output: 15
Explanation: The sum() function returns the sum of all elements in the tuple.

• Merge two tuples and remove duplicates.

tup1 = (1, 2, 3)
tup2 = (3, 4, 5)

result = tuple(set(tup1).union(tup2))

print(result) # Output: (1, 2, 3, 4, 5)

Explanation: The set() function is used to remove duplicates, and then


the union() method combines the two sets into one set.

• Find the first and last elements of a tuple.


tup = (10, 20, 30, 40, 50)

first, last = tup[0], tup[-1]

print("First:", first) # Output: First: 10


print("Last:", last) # Output: Last: 50

Explanation: The first element is accessed using index 0, and the last element is
accessed using index -1.

• Convert a tuple of integers to a tuple of strings.

tup = (1, 2, 3, 4, 5)

str_tup = tuple(str(x) for x in tup)

print(str_tup) # Output: ('1', '2', '3', '4', '5')

Explanation: A new tuple is created by converting each integer element to its string
representation.

• Count the number of even and odd numbers in a tuple.

tup = (1, 2, 3, 4, 5, 6, 7, 8, 9)

even_count = sum(1 for x in tup if x % 2 == 0)


odd_count = len(tup) - even_count

print("Even:", even_count) # Output: Even: 4


print("Odd:", odd_count) # Output: Odd: 5

Explanation: A generator expression is used to count even elements, and odd count is
calculated based on total count and even count.

• Find the product of all elements in a tuple.

tup = (1, 2, 3, 4, 5)

product = 1
for x in tup:
product *= x

print(product) # Output: 120

Explanation: The product is calculated by iterating through the elements and


continuously multiplying them.

• Split a tuple into equal parts.


tup = (1, 2, 3, 4, 5, 6)
n = 2
avg = len(tup) // n
split_result = [tup[i:i+avg] for i in range(0, len(tup), avg)]

result = tuple(split_result)

print(result) # Output: ((1, 2, 3), (4, 5, 6))

Explanation: The tuple is split into n equal parts using list comprehension and then
converted back to a tuple

• Find the frequency of each element in 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

print(freq_dict) # Output: {1: 1, 2: 4, 3: 1, 4: 1}

Explanation: A dictionary is used to store the frequency of each element in the tuple.

• Find the difference between two tuples.


tup1 = (1, 2, 3, 4, 5)
tup2 = (3, 4, 5, 6)

difference = tuple(x for x in tup1 if x not in tup2)

print(difference) # Output: (1, 2)

Explanation: A new tuple is created by including only those elements from the first tuple
that are not present in the second tuple.

• Find the second largest element in a tuple.

tup = (10, 5, 8, 20, 15)

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.

• Check if a tuple is a subset of another tuple.

main_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
sub_tuple = (2, 4, 6)

result = all(x in main_tuple for x in sub_tuple)

print(result) # Output: True

Explanation: The all() function is used to check if all elements in the subset tuple are
present in the main tuple.

You might also like