Python tuple() Function
Creates a tuple from a sequence, collection, or an iterable object. If the parameter is not specified, Python returns an empty tuple.
Syntax
Python
Copy Code
tuple(iterable = None)
Parameters
Parameter | Description |
---|---|
iterable) |
Optional. An iterable object used to create the tuple. If no object is supplied then an ampty tuple is returned. |
Notes
Unlike the set() function, a tuple can include duplicate values.
Example
Python
Copy Code
emptyTuple = tuple() print(emptyTuple) rangeTuple = tuple(range(10, 15)) print(rangeTuple) dictionary = {'apples': 5, 'oranges': 3} print(tuple(dictionary)) listWithDuplicate = ['Spring', 'Summer', 'Fall', 'Winter', 'Spring'] print(tuple(listWithDuplicate))
Output
() (10, 11, 12, 13, 14) ('apples', 'oranges') ('Spring', 'Summer', 'Fall', 'Winter', 'Spring')