89DEVs

Python tuple() function

overview of tuple()

The tuple() function is used to create tuples. A tuple is an immutable sequence and it can also be created by using round brackets.

use of tuple()

The tuple() function is used to create tuples out of iterables like lists. # use of tuple() function emptyTuple = tuple() listTuple = tuple([1, 2, 3, 4]) stringTuple = tuple('Python') dictTuple = tuple({1: ' first', 2: 'second'}) print(emptyTuple) print(listTuple) print(stringTuple) print(dictTuple) Four tuples are created by using the tuple() function: One empty tuple, one tuple from a list, one tuple from a string, and one tuple from a dictionary. Finally, all four tuples are printed. Please note that only the keys of the dictionary are used for the tuple, not the values. () (1, 2, 3, 4) ('P', 'y', 't', 'h', 'o', 'n') (1, 2)

syntax of tuple()

The syntax of the tuple() function is: tuple([iterable])

argument of tuple()

The tuple() functions accept one optional argument, which can be an iterable like a list or range or an iterator object. If no argument is passed, an empty tuple is created by the function.

return values of tuple()

The tuple() function returns the created tuple.

                
        

Summary


Click to jump to section