Tuple with different data types

EasyTuple Programs · 5 of 70

Write a Python program to create a tuple containing values of different data types.

Advertisement
Output
(2.6, 1, 'Python', True, [5, 6, 7], (5, 1, 4), {'a': 123, 'b': 456})
['float', 'int', 'str', 'bool', 'list', 'tuple', 'dict']

Approach

  1. 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 3

Listing the type names shows every item keeps its own type.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs