The article covers the use of the Python tuple function. The tuple function is used creates tuples. Read more about Python tuples in a separate article.
Syntax
The syntax for the Python tuple function is as follows.
tuple(iterable)
The function takes a single input argument, an iterable which may be a list, dictionary, sequence or iterator object and returns a tuple equivalent. In the event that an iterable is not included, an empty tuple will be returned.
If an appropriate iterable is not passed, a TypeError will be thrown.
Examples:
Below are several examples of the tuple function being used with different iterables.
>>> tuple()
()
>>> tuple("Hello")
('H', 'e', 'l', 'l', 'o')
>>> tuple([1,2,3,4,5])
(1, 2, 3, 4, 5)
>>> tuple({"a" : 1, "b" : 2, "c" : 3})
('a', 'b', 'c')
>>> tuple({3,7,"g","a",1})
(1, 3, 7, 'g', 'a')
>>> tuple(range(5))
(0, 1, 2, 3, 4)
Note that with dictionaries, from the key value pair, the values are ignored, and the keys are included into the tuple object.
This marks the end of the Python tuple Function section. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions be can be directed to the comments section below.