Tuple with different data types Easy Tuple 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 Reset Copy Try it ▸
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.
More tuple programs ← Program 4 List of (number, square) tuplesProgram 6 → Element by index← All 70 Tuple Programs