Python : Tuple - Exercises and Solution


1. Write a Python program to create a tuple

Sample Output

Tuple = ()

View Solution

2. Write a Python program to create a tuple with different data types

Sample Output

('Tutor Joes', True, 'T', 3.21, 10)

View Solution

3. Write a Python program to create a tuple with numbers and print one item

Sample Output

10

Type = tuple

View Solution

4. Write a Python program to unpack a tuple in several variables

Sample Output

a = 4, 8, 3

(4, 8, 3)

View Solution

5. Write a Python program to add an item in a tuple

Sample Output

(10, 40, 50, 70, 90)

add items in list = 20

(10, 40, 50, 70, 90, 20)

View Solution

6. Write a Python program to convert a tuple to a string

Sample Output

('T', 'u', 't', 'o', 'r', ' ', 'J', 'o', 'e', 's')

Tutor Joes

View Solution

7. Write a Python program to get the 4th element and 4th element from last of a tuple

Sample Output

('w', 3, 'r', 'e', 's', 'o', 'u', 'r', 'c', 'e')

4th Elements From Tuple : e

4th Elements From Last Tuple : u

View Solution

8. Write a Python program to create the colon of a tuple

Sample Output

("Tutor", 'J', 23 , 56.67 , [23,12] , True)

Copy = ('Tutor', 'J', 23, 56.67, [23, 12], True)

View Solution

9. Write a Python program to find the repeated items of a tuple

Sample Output

(2, 34, 45, 6, 7, 2, 4, 5, 78, 34, 2)

item = 2

Repeated items of a tuple = 3

View Solution

10. Write a Python program to check whether an element exists within a tuple

Sample Output

('T', 'u', 't', 'o', 'r', ' ', 'J', 'o', 'e', 's',8)

8 in Tuple = True

'x' in Tuple = False

View Solution

11. Write a Python program to convert a list to a tuple

Sample Output

[12, 45, 87, 54, 89, 4]

(12, 45, 87, 54, 89, 4)

View Solution

12. Write a Python program to remove an item from a tuple

Sample Output

(23, 45, 56, 68, 10, 45, 7, 9)

Remove = 56

(23, 45, 68, 10, 45, 7, 9)

View Solution

13. Write a Python program to slice a tuple

Sample Output

(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

(40, 50, 60, 70, 80)

View Solution

14. Write a Python program to find the index of an item of a tuple

Sample Output

(23, 45, 67, 78, 89, 90, 34, 56)

Item = 78

Index Number : 3

View Solution

15. Write a Python program to find the length of a tuple

Sample Output

("Lion", "Cat", "Dog", "Panda", "Tiger", "Fox")

Length : 6

View Solution

16. Write a Python program to convert a tuple to a dictionary

Sample Output

( ("Name", "Ram"), ("Age", 23), ("City", "Salem"), ("Mark", 422) )

{ 'Name': 'Ram', 'Age': 23, 'City': 'Salem', 'Mark': 422 }

View Solution

17. Write a Python program to unzip a list of tuples into individual lists

Sample Output

[ (10,30), (60,90), (20,50) ]

[ (10, 60, 20), (30, 90, 50) ]

View Solution

18. Write a Python program to reverse a tuple

Sample Output

Before Reverse : ( 23, 45, 67, 78, 89, 90, 34, 56 )

After Reverse : ( 56, 34, 90, 89, 78, 67, 45, 23 )

View Solution

19. Write a Python program to convert a list of tuples into a dictionary

Sample Output

[ ("Name", "Ram"), ("Name", "Pooja"), ("Age", 21), ("Gender", "Male"), ("Age", 23), ("Gender", "Female") ]

{ 'Name' : ['Ram', 'Pooja'], 'Age' : [21, 23], 'Gender' : ['Male', 'Female'] }

View Solution

20. Write a Python program to print a tuple with string formatting

Sample Output

("watermelons", "strawberries", "mangoes", "bananas", "grapefruits", "oranges", "apples", "pears")

Fruits ('watermelons', 'strawberries', 'mangoes', 'bananas', 'grapefruits', 'oranges', 'apples', 'pears')

View Solution

21. Create a tuple with single item 23

Sample Output

Create a tuple with Item = 23

Type = tuple

View Solution

22. Unpack the tuple into 5 variables

Sample Output

Tuple = (11, 22, 333, 44, 55)

11
22
33
44
55

View Solution

23. Swap two tuples in Python

Sample Output

Before Swap A : 10

Before Swap B : 20

After Swap A : 20

After Swap B : 10

View Solution

24. Copy specific elements from one tuple to a new tuple

Sample Output

(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

(30, 40, 50, 60, 70, 80)

View Solution

25. Modify the tuple

Sample Output

( 10, 20, 30, 40, 50 )

( 10, 20, 33, 40, 50 )

View Solution

26. Sort a tuple of tuples by 2nd item

Sample Output

( ('a', 53), ('b', 37), ('c', 23), ('d', 1), ('e', 18) )

( ('d', 1), ('e', 18), ('c', 23), ('b', 37), ('a', 53) )

View Solution

27. Counts the number of occurrences of item 30 from a tuple

Sample Output

(30, 50, 10, 30, 70, 50, 30)

Number of Counts 30 : 3

View Solution

28. Write a Python program to compute element-wise sum of given tuples

Sample Output

A = (2, 5, 8)

B = (6, 5, 1)

C = (1, 4, 7)

D = (3, 7, 2)

Sum of Elements = (12, 21, 18)

View Solution

29. Write a Python program to sort a tuple by its float element

Sample Output

[ ('Ram', '89.20'), ('Siva', '76.45'), ('Pooja', '84.40'), ('Tara', '68.43'), ('Jeeva', '91.40') ]

[ ('Jeeva', '91.40'), ('Ram', '89.20'), ('Pooja', '84.40'), ('Siva', '76.45'), ('Tara', '68.43') ]

View Solution

30. Write a Python program to replace last value of tuples in a list

Sample Output

[(5, 2, 3), (4, 7, 6), (8, 9, 6)]

Replace value = 10

[(5, 2, 10),(4, 7, 10),(8, 9, 10)]

View Solution

31. Write a Python program to Extract tuples having K digit elements

Sample Output

[ (47, 23), (3, 78), (22, 53), (121, 45), (7,) ]

K = 2

[ (47, 23), (22, 53) ]

View Solution

32. Write a Python program to Extract Symmetric Tuples

Sample Output

[ (18, 23), (2, 9), (7, 6), (9, 2), (10, 2), (23, 18) ]

{ (2, 9), (18, 23) }

View Solution

33. Write a Python program to Sort Tuples by their Maximum element

Sample Output

[ (4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2) ]

[ (19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2) ]

View Solution

34. Write a Python program to Remove nested records from tuple

Sample Output

(10, 20, (30,), 40, (50, 60), 70)

Removal of Nested Tuple : (10, 20, 40, 70)

View Solution

35. Write a Python program to Elements Frequency in Mixed Nested Tuple

Sample Output

(10, 20, (20, 10), 30, (40, 50, 60), 60, 70)

Elements Frequency : {10: 2, 20: 2, 30: 1, 40: 1, 50: 1, 60: 2, 70: 1}

View Solution

36. Write a Python program to get unique elements in nested tuple

Sample Output

[ (1, 3, 5), (4, 5, 7), (1, 2, 6), (10, 9), (10,) ]

Unique Element in Nested Tuples : [1, 3, 5, 4, 7, 2, 6, 10, 9]

View Solution

37. Write a Python program to Concatenate tuples to nested tuples

Sample Output

Tuple 1 : ((18, 23, 2, 9),)

Tuple 2 : ((10, 3, 11),)

Tuples after Concatenating : ((18, 23, 2, 9), (10, 3, 11))

View Solution

38. Write a Python program to Sort by Frequency of second element in Tuple List

Sample Output

[ (2, 7), (3, 7), (2, 5), (8, 7), (6, 5), (9, 8) ]

Sorted List of tuples : [ (2, 7), (3, 7), (8, 7), (2, 5), (6, 5), (9, 8) ]

View Solution

39. Write a Python program to Sort lists in tuple

Sample Output

( [10, 50, 60], [80, 20, 30], [70, 100, 40], (90,) )

Tuple after sorting list : ( [10, 50, 60], [20, 30, 80], [40, 70, 100], [90] )

View Solution

40. Write a Python program to Order Tuples using external List

Sample Output

[ ('B', 68), ('D', 70), ('A', 67), ('C', 69) ]

Ordered Tuple List : [ ('A', 67), ('B', 68), ('C', 69), ('D', 70) ]

View Solution

41. Write a Python program to Filter Tuples by Kth element from List

Sample Output

[ ('B', 68), ('D', 70), ('A', 67), ('C', 69) ]

List of kth elements to filter = [67 , 70 , 71, 75]

Kth element index = 1

Filtered tuples : [('D', 70), ('A', 67)]

View Solution

42. Write a Python program to Closest Pair to Kth index element in Tuple

Sample Output

[ (23, 18), (9, 2), (2, 3), (9, 18), (23, 2) ]

Given Tuple = (20, 2)

Kth Element Index = 1

Nearest Tuple : (23, 18)

View Solution

43. Write a Python program to Tuple List intersection (Order irrespective)

Sample Output

List 1 : [ (3, 4), (5, 6), (9, 10), (4, 5) ]

List 2 : [ (5, 4), (3, 4), (6, 5), (9, 11) ]

Intersection : { (4, 5), (5, 6), (3, 4) }

View Solution

44. Write a Python program to Intersection in Tuple Records Data

Sample Output

list 1 : [ ('A', 65), ('D', 68), ('B', 66) ]

list 2 : [ ('D', 68), ('C', 67), ('A', 65) ]

Intersection of data records : [ ('A', 65), ('D', 68) ]

View Solution

45. Write a Python program to Unique Tuple Frequency (Order Irrespective)

Sample Output

[ (3, 4), (1, 2), (4, 3), (5, 6) ]

Unique tuples Frequency : [(1, 2), (3, 4), (5, 6)]

Unique tuples Frequency Count : 3

View Solution

46. Write a Python program to Skew Nested Tuple Summation

Sample Output

(1, (2, (3, (4, (5, None)))))

Summation of 1st positions : 15

View Solution

47. Write a Python program to Convert Binary tuple to Integer

Sample Output

(1, 0, 1, 0)

Decimal number is : 10

View Solution

48. Write a Python program to Tuple XOR operation

Sample Output

Tuple 1 : (10, 4, 6, 9)

Tuple 2 : (5, 2, 3, 3)

XOR tuple : (15, 6, 5, 10)

View Solution

49. Write a Python program to AND operation between Tuples

Sample Output

Tuple 1 : (10, 4, 6, 9)

Tuple 2 : (5, 2, 3, 3)

AND operation Between Tuple : (0, 0, 2, 1)

View Solution

50. Write a Python program to Elementwise AND in tuples

Sample Output

Tuple 1 : (10, 4, 6, 9)

Tuple 2 : (5, 2, 3, 3)

Elementwise AND Tuple : (0, 0, 2, 1)

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial