Python Practice › Tuple Programs › Program 5
Tuple with different data types
Write a Python program to create a tuple containing values of different data types.
Output
(2.6, 1, 'Python', True, [5, 6, 7], (5, 1, 4), {'a': 123, 'b': 456})
['float', 'int', 'str', 'bool', 'list', 'tuple', 'dict']Approach
- A tuple can hold any objects, including lists, tuples and dicts.
Solution
mixed = (2.6, 1, "Python", True, [5, 6, 7], (5, 1, 4), {"a": 123, "b": 456})print(mixed)print([type(x).__name__ for x in mixed])Output
(2.6, 1, 'Python', True, [5, 6, 7], (5, 1, 4), {'a': 123, 'b': 456})
['float', 'int', 'str', 'bool', 'list', 'tuple', 'dict']Line by line
Line 3
Listing the type names shows every item keeps its own type.
Learn the concept
New to this topic? Read the lesson, then come back.