Tuple with different data types
EasyTuple Programs · 5 of 70
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
Python
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 3Listing the type names shows every item keeps its own type.
Learn the concept
New to this topic? Read the lesson, then come back.
More tuple programs
← All 70 Tuple Programs