Practice Top 30 Python Programming Questions on Tuples Data Structure
Q1. Write a program to create a tuple.
Input:
(10, 20, 30)
Expected Output:
Tuple: (10, 20, 30)
Q2. Write a program to access and print the first and last element of a tuple.
Input:
(1, 2, 3, 4, 5)
Expected Output:
First element: 1
Last element: 5
Q3. Write a program to find the length of a tuple.
Input:
(4, 5, 6, 7)
Expected Output:
Length of tuple: 4
Q4. Write a program to check if a specific element exists in a tuple.
Input:
Tuple: (1, 2, 3, 4, 5)
Element: 3
Expected Output:
3 exists in the tuple.
Q5. Write a program to concatenate two tuples.
Input:
Tuple 1: (1, 2, 3)
Tuple 2: (4, 5, 6)
Expected Output:
Concatenated tuple: (1, 2, 3, 4, 5, 6)
Q6. Write a program to repeat a tuple a given number of times.
Input:
Tuple: (1, 2)
Repeat times: 3
Expected Output:
Repeated tuple: (1, 2, 1, 2, 1, 2)
Q7. Write a program to slice a tuple.
Input:
Tuple: (10, 20, 30, 40, 50)
Slice: 2:4
Expected Output:
Sliced tuple: (30, 40)
Q8. Write a program to find the index of an element in a tuple.
Input:
Tuple: (10, 20, 30, 40)
Element: 30
Expected Output:
Index of 30: 2
Q9. Write a program to count the occurrences of an element in a tuple.
Input:
Tuple: (1, 2, 2, 3, 4, 5, 2)
Element: 2
Expected Output:
Count of 2: 3
Q10. Write a program to find the maximum and minimum element in a tuple.
Input:
Tuple: (4, 10, 7, 2, 9)
Expected Output:
Maximum: 10
Minimum: 2
Q11. Write a program to convert a tuple into a list.
Input:
Tuple: (1, 2, 3)
Expected Output:
List: [1, 2, 3]
Q12. Write a program to check if a tuple has exactly 3 elements.
Input:
Tuple: (1, 2, 3)
Expected Output:
Tuple has 3 elements.
Q13. Write a program to unpack a tuple into variables.
Input:
Tuple: (1, 2, 3)
Expected Output:
a: 1, b: 2, c: 3
Q14. Write a program to access an element from a nested tuple.
Input:
Tuple: ((1, 2), (3, 4), (5, 6))
Expected Output:
Second element in first nested tuple: 2
Q15. Write a program to sort the elements of a tuple in ascending order.
Input:
Tuple: (5, 2, 8, 1)
Expected Output:
Sorted tuple: (1, 2, 5, 8)
Q16. Write a program to convert a tuple to a string.
Input:
Tuple: ('a', 'b', 'c')
Expected Output:
String: 'abc'
Q17. Write a program to merge two tuples into a third tuple.
Input:
Tuple 1: (1, 2)
Tuple 2: (3, 4)
Expected Output:
Merged tuple: (1, 2, 3, 4)
Q18. Write a program to create a tuple with one element.
Input:
Element: 10
Expected Output:
Tuple with one element: (10,)
Q19. Write a program to remove an element from a tuple by converting it to a list first.
Input:
Tuple: (1, 2, 3, 4)
Element: 3
Expected Output:
Updated tuple: (1, 2, 4)
Q20. Write a program to check if a tuple is empty.
Input:
Tuple: ()
Expected Output:
Tuple is empty.
Q21. Write a program to multiply all the elements of a tuple.
Input:
Tuple: (1, 2, 3)
Expected Output:
Product of elements: 6
Q22. Write a program to find the longest tuple from a list of tuples.
Input:
Tuples: [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
Expected Output:
Longest tuple: (6, 7, 8, 9)
Q23. Write a program to create a tuple of even numbers from a given list of integers.
Input:
List: [1, 2, 3, 4, 5, 6]
Expected Output:
Even numbers tuple: (2, 4, 6)
Q24. Write a program to count how many tuples are in a list of tuples.
Input:
List of tuples: [(1, 2), (3, 4), (5, 6)]
Expected Output:
Number of tuples: 3
Q25. Write a program to find the index of an element in a nested tuple.
Input:
Tuple: ((1, 2), (3, 4), (5, 6))
Element: 5
Expected Output:
Index of (5, 6): 2
Q26. Write a program to compare two tuples.
Input:
Tuple 1: (1, 2, 3)
Tuple 2: (1, 2, 3)
Expected Output:
Tuples are equal.
Q27. Write a program to find common elements between two tuples.
Input:
Tuple 1: (1, 2, 3)
Tuple 2: (2, 3, 4)
Expected Output:
Common elements: (2, 3)
Q28. Write a program to swap two tuples.
Input:
Tuple 1: (1, 2)
Tuple 2: (3, 4)
Expected Output:
Swapped tuples: (3, 4) (1, 2)
Q29. Write a program to convert a list into a tuple.
Input:
List: [1, 2, 3]
Expected Output:
Tuple: (1, 2, 3)
Q30. Write a program to create a tuple with different data types.
Input:
Tuple: (1, 'apple', 3.5, True)
Expected Output:
Tuple with mixed data types: (1, 'apple', 3.5, True)
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!