0% found this document useful (0 votes)
15 views11 pages

Tuple Data Type (Python)

Python tuple data type

Uploaded by

notsureabout99
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)
15 views11 pages

Tuple Data Type (Python)

Python tuple data type

Uploaded by

notsureabout99
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/ 11

Question 1: Find the length of a tuple.

# Solution 1:

def tuple_length (tup):

return len(tup)

# Solution 2:

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

length = len(tup)

print (1length) # Output: 5

Explanation: The len) function returns the number of elements in the tuple.

Question 2:Concatenate two tuples.

# Solution 1:

def concatenate_tuples (tup1, tup2) :

return tup1 + tup2


# Solution 2:

tup1 = (1, 2, 3)

tup2 = (4, 5, 6)

result = tup1 + tup2

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

Explanation: The + operaior car be used to concatenateiwo tuples.

Question 3: Access an elernent in a tup!e.

# Solution 1:

def access_elesent (tup, index) :

return tup[index]

Realme Nazro 60x 5G


# Solution 2:

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

element = tup[2]

print(element) # Output:30

Explanation:Tuples are zero-indexed,so the third element has index 2.

Question 4: Count the occurrences of an element in a tuple.

# Solution 1:
def count element (tup, value):

return tup.count (value)

# Solut ion 2:

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

count = tup.count (2)

print (count ) # Output: 4

Explanation: The count) method returns the number of occurrences of


a given value in the tuple.

Question 5: Check if an element exists in a tuple.

# Solution 1:

def element_exists (tup,value):

return value in tup

# Solution 2:

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

exists = 30 in tup

print (exists) # Output: True

Realme Nazro 60x 5G


-S

Explanation:The in operator can be used


to check if an element
exists in the tuple.
Question 6:Convert a tuple
to astring.
# Solut ion 1:

def
tuple_to_string(tup):

return str(tup)

# Solution 2:

tup = (1, 2, 3)

string = str(tup)

print (string) # Output: "(1, 2, 3)"

Explanation: The str) function converts the tuple to its string


representation.
Question 7: Find the index of an element in a tuple.
# Solution 1:

def find_index (tup, value):

return tup.index (value)

# Solution 2:

tup = (10, 20, 30,


40, 50)

index = tup. index(30)

print (index) # Output: 2


Explanation: The index() method returns the index of the first
value in the tuple. occurrence of the given

Question 8: Convert a list to a tuple.

# Solution 1:

def list_to_ tuple (1st) :

Realme Nazro 60x 5G


return tuple(1lst)

# Solution 2:

lst = [1, 2, 3, 4, 5]

tup = tuple (1st)

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

Explanation: The tuple() constructor can be used to convert a list to a tuple.

Question 9: Iterate through a


tuple using a loop.

# Solut ion:

tup = (10, 20, 30, 40,


50)

for element in tup:

print (element)

Explanation: A for loop can be used to iterate through the elements of a tuple.

Question 10: Find the maximum


and minimum elements in a tuple.

# Solution:

def max_ min elements (tup):

return max(tup), min(tup)

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

max_element, min_element = max_min_ elements (tup)

,
print ("Max:", max_element )
# Output: MaX: 10
print ("Min:",
min_element) # Output: Min: 2

Explanation: The max() and min() functions the


in the tuple,respectively.
return maximum and minimum elements

Question 11: Convert a tuple of strings to a single string.

Realme Nazro 60x 5G


# Solution:

def
tuple_to_single_string(tup):

return".join(tup)

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

result = tuple to_single_string(tup)

print (result) # Output: "Hello"

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

Question 12: Remove an element from a tuple.

# Solution:

def remove element (tup, value):

return tuple(x for x in tup if x != value)

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

new_tup = remove_element (tup, 3)

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

Explanation: A newtuple is created using a generator expression that excludes the


specified value.

Question 13: Find the common elements between two tuples.

# Solution:

def common_elements (tup1, tup2):

return tuple (x for x in tup1 if x in tup2 )

tup1 = (1, 2, 3, 4)

tup2 = (3, 4, 5, 6)

common = common_elements (tup1, tup2)

Realme Nazro 60x 5G


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

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

Question 14: Sort a tuple of integers.

# Solution:

def sort_tuple (tup):

return tuple (sorted (tup) )

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

sorted_tup = sort_tuple(tup)

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

Explanation: The sorted() function used the elements of the tuple,


is to sort and then a
new tuple is created from the sorted list.

Question 15:Find the sum of all elements in atuple.

# Solution:

def sum_tuple (tup):

return sum(tup)

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

total = sum_tuple (tup )

print(total) # Output: 15

Explanation: The sum)function returns the sum of all elements in the tuple.

16:** Merge two tuples and remove


duplicates.

# Solution:

def merge_and_remove_duplicates (tup1,tup2):

Realme Nazro 60x 5G


return tuple (set(tup1) union(tup2)) .
tup1 = (1, 2, 3)

tup2 = (3, 4, 5)

result = merge_and remove duplicates (tup1, tup2)


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

Explanation:The set) function


is used to remove
the union() method duplicates, and then
combines the two sets into one
set.

Question 17: Find the first and last elements of a tuple.


# Solution:

def
first_last_elements(tup):

return tup[o], tup[-1]

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

first, last = first last_elements (tup)

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
using index -1. is accessed

Question 18: Convert a tuple of


integers to a tuple of strings.

# Solution:

def int_to_str_tuple (tup):

return tuple(str (x)for x in tup)

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

str_tup = int_to_str_tuple (tup)

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

Realme Nazro 60x 5G


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

representation.

Question 19: Count the number of even and odd numbersin a tuple.

# Solution:

def count even_odd(tup):

even cOunt = sum(1 for x in tup if x % 2 == Ø)

odd_count = len(tup) even COunt

return even_count, odd count

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

even, odd = count _even odd(tup)

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


print("Odd:", odd) # Output: 0dd: 5

Explanation: A generator expression is used to count even elements, and


calculated based on total count odd count is
and even count.

Question 20: Find the product


of all elements in a tuple.

# Solution:

def product_tuple (tup):

product = 1

for x in tup:

product *= x

return product

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

Realme Nazro 60x 5G


print (result) # Output: 128

Explanation: The product is calculated by through the elements


iterating and
continuously multiplying them.

Question 21:Split a tuple into equal parts.

# Solution:

def split_tuple (tup, n):

avg = len (tup) I/ n

split_result = [tup[i:i+avg] for i in range (8, len(tup), avg)]

return tuple (split result)

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

n= 2

result = split tuple(tup, n)

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.

Question 22: Find the freguency of each element in a tuple.

# Solution:

def element_frequency

freg_dict = ) (tup):

for element

return
freq_dict

freq_dict
in tup:

=
[element] freq_dict.get (element, ) + 1

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

frequency = element_frequency (tup)

Realme Nazro 60x 5G


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

used to store the frequency of each element in the tuple.


Explanation: A dictionary is

Question 23: Find the difference between two tuples.

# Solution:

def tuple difference (tup1, tup2) :

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


return

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

tup2 = (3, 4, 5, 6)

difference = tuple_difference (tup1, 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.

Question 24: Find the second largest element in a tuple.

# Solution:

def second_largest (tup):

sorted_tup = sorted (tup)

return sorted_tup [-2]

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

second_largest_element = second_largest (tup)

print (second_largest_element) # Output: 15

Explanation: The tuple is sorted in ascending order, and the second-to-lastelement is

the second largest.

Realme Nazro 60x 5G


Question 25:Check if a tuple is a subset of another tuple.

# Solution:

def is_subset (sub, main):

return all(x in main for x in sub)

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

sub_tuple = (2, 4, 6)

result = is_subset(sub_tuple, main_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.

Realme Nazro 60x 5G

You might also like