Python tuple() function

Python tuple() is used to create a tuple from iterable.It is one of the way to create tuple.


Syntax

iterable(optional): Iterable is optional parameter which can be iterable(list etc) or iterable object.
If iterable is not passed, tuple() will create empty tuple.


Return

It returns the tuple .


Example

Let’s see a very simple example:

t=tuple() print("Emply tuple:",t) t1=tuple([1,2,3,4]) print("tuple created from list:",t1) t2=tuple({1:'One',2:'Two'}) print("tuple created from dictionary:",t2) t3=tuple(range(10)) print("tuple created from range function:",t3) t4=tuple('code2master') print("tuple created from string:",t4)

Output:

Emply tuple: ()
tuple created from list: (1, 2, 3, 4)
tuple created from dictionary: (1, 2)
tuple created from range function: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
tuple created from string: (‘c’, ‘o’, ‘d’, ‘e’, ‘2’, ‘m’, ‘a’, ‘s’, ‘t’, ‘e’, ‘r’)

That’s all about Python tuple() function.

Leave a Reply

Your email address will not be published. Required fields are marked *