w3resource

Python Data Types: Tuple - Exercises, Practice, Solution

Python Tuple [ 33 exercises with solution]

A tuple is a container which holds a series of comma-separated values (items or elements) between parentheses such as an (x, y) co-ordinate. Tuples are like lists, except they are immutable (i.e. you cannot change its content once created) and can hold mix data types.

You may read our Python tuple tutorial before solving the following exercises.

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Python program to create a tuple. Go to the editor

Click me to see the sample solution

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

Click me to see the sample solution

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

Click me to see the sample solution

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

Click me to see the sample solution

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

Click me to see the sample solution

6. Write a Python program to convert a tuple to a string. Go to the editor

Click me to see the sample solution

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

Click me to see the sample solution

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

Click me to see the sample solution

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

Click me to see the sample solution

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

Click me to see the sample solution

11. Write a Python program to convert a list to a tuple. Go to the editor

Click me to see the sample solution

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

Click me to see the sample solution

13. Write a Python program to slice a tuple. Go to the editor

Click me to see the sample solution

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

Click me to see the sample solution

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

Click me to see the sample solution

16. Write a Python program to convert a tuple to a dictionary. Go to the editor

Click me to see the sample solution

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

Click me to see the sample solution

18. Write a Python program to reverse a tuple. Go to the editor

Click me to see the sample solution

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

Click me to see the sample solution

20. Write a Python program to print a tuple with string formatting. Go to the editor
Sample tuple : (100, 200, 300)
Output : This is a tuple (100, 200, 300)

Click me to see the sample solution

21. Write a Python program to replace last value of tuples in a list. Go to the editor
Sample list: [(10, 20, 40), (40, 50, 60), (70, 80, 90)]
Expected Output: [(10, 20, 100), (40, 50, 100), (70, 80, 100)]

Click me to see the sample solution

22. Write a Python program to remove an empty tuple(s) from a list of tuples. Go to the editor
Sample data: [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')]
Expected output: [('',), ('a', 'b'), ('a', 'b', 'c'), 'd']

Click me to see the sample solution

23. Write a Python program to sort a tuple by its float element. Go to the editor
Sample data: [('item1', '12.20'), ('item2', '15.10'), ('item3', '24.5')]
Expected Output: [('item3', '24.5'), ('item2', '15.10'), ('item1', '12.20')]
Click me to see the sample solution

24. Write a Python program to count the elements in a list until an element is a tuple. Go to the editor
Click me to see the sample solution

25. Write a Python program convert a given string list to a tuple. Go to the editor
Original string: python 3.0
<class 'str'>
Convert the said string to a tuple:
('p', 'y', 't', 'h', 'o', 'n', '3', '.', '0')
<class 'tuple'>

Click me to see the sample solution

26. Write a Python program calculate the product, multiplying all the numbers of a given tuple. Go to the editor
Original Tuple:
(4, 3, 2, 2, -1, 18)
Product - multiplying all the numbers of the said tuple: -864
Original Tuple:
(2, 4, 8, 8, 3, 2, 9)
Product - multiplying all the numbers of the said tuple: 27648

Click me to see the sample solution

27. Write a Python program to calculate the average value of the numbers in a given tuple of tuples. Go to the editor
Original Tuple:
((10, 10, 10, 12), (30, 45, 56, 45), (81, 80, 39, 32), (1, 2, 3, 4))
Average value of the numbers of the said tuple of tuples:
[30.5, 34.25, 27.0, 23.25]
Original Tuple:
((1, 1, -5), (30, -15, 56), (81, -60, -39), (-10, 2, 3))
Average value of the numbers of the said tuple of tuples:
[25.5, -18.0, 3.75]

Click me to see the sample solution

28. Write a Python program to convert a tuple of string values to a tuple of integer values. Go to the editor
Original tuple values:
(('333', '33'), ('1416', '55'))
New tuple values:
((333, 33), (1416, 55))

Click me to see the sample solution

29. Write a Python program to convert a given tuple of positive integers into an integer. Go to the editor
Original tuple:
(1, 2, 3)
Convert the said tuple of positive integers into an integer:
123
Original tuple:
(10, 20, 40, 5, 70)
Convert the said tuple of positive integers into an integer:
102040570

Click me to see the sample solution

30. Write a Python program to check if a specified element presents in a tuple of tuples. Go to the editor
Original list:
(('Red', 'White', 'Blue'), ('Green', 'Pink', 'Purple'), ('Orange', 'Yellow', 'Lime'))
Check if White presenet in said tuple of tuples!
True
Check if White presenet in said tuple of tuples!
True
Check if Olive presenet in said tuple of tuples!
False

Click me to see the sample solution

31. Write a Python program to compute element-wise sum of given tuples. Go to the editor
Original lists:
(1, 2, 3, 4)
(3, 5, 2, 1)
(2, 2, 3, 1)
Element-wise sum of the said tuples:
(6, 9, 8, 6)

Click me to see the sample solution

32. Write a Python program to compute the sum of all the elements of each tuple stored inside a list of tuples. Go to the editor
Original list of tuples:
[(1, 2), (2, 3), (3, 4)]
Sum of all the elements of each tuple stored inside the said list of tuples:
[3, 5, 7]
Original list of tuples:
[(1, 2, 6), (2, 3, -6), (3, 4), (2, 2, 2, 2)]
Sum of all the elements of each tuple stored inside the said list of tuples:
[9, -1, 7, 8]

Click me to see the sample solution

33. Write a Python program to convert a given list of tuples to a list of lists. Go to the editor
Original list of tuples: [(1, 2), (2, 3), (3, 4)]
Convert the said list of tuples to a list of lists: [[1, 2], [2, 3], [3, 4]]
Original list of tuples: [(1, 2), (2, 3, 5), (3, 4), (2, 3, 4, 2)]
Convert the said list of tuples to a list of lists: [[1, 2], [2, 3, 5], [3, 4], [2, 3, 4, 2]]

Click me to see the sample solution

Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Understanding slice notation:

It's pretty simple really:

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relation to slice() object

The slicing operator [] is actually being used in the above code with a slice() object using the : notation (which is only valid within []), i.e.:

a[start:stop:step]

is equivalent to:

a[slice(start, stop, step)]

Slice objects also behave slightly differently depending on the number of arguments, similarly to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the : -based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

Ref: https://bit.ly/2MHaTp7