Create a Tuple
There are two ways to create a tuple in Python.
The first way is to use parenthesis
() as shown in the following example.
tuple_1 = (item_1, item_2, item_3)
We can pass as many items as required in parenthesis.
The second way to create a tuple is to use tuple() builtin function. Pass an iterable to tuple() function, and it returns a Tuple created from the items of the given iterable.
tuple_1 = tuple(iterable)
Since tuple is immutable, we cannot add to or delete items from a tuple. So, whatever the elements we initialize the tuple with, that’s it, no more modifications to the tuple.
Create Tuple using Parenthesis ()
In the following program, we create a tuple of integers
tuple_1 using parenthesis.
tuple_1 = (2, 4, 6) print(tuple_1)Try Online
(2, 4, 6)
Create Tuple using tuple() Function
In the following program, we create a tuple of strings
tuple_1 using tuple() builtin function. We take the list
x as iterable and pass it to the tuple() function.
x = ['a', 'b', 'c'] tuple_1 = tuple(x) print(tuple_1)Try Online
('a', 'b', 'c')
In this Python Tutorial, we learned how to create a tuple in Python with the help of examples.