Python – Creates Tuple – tuple() Function with Examples

tuple() function is used to find or create a tuple in Python for the given input.

tuple() function is a part of Python Built-in Functions.

In Python programming, a tuple is similar to a list. The difference between them is that we cannot change the elements of a tuple once it is assigned whereas, in a list, elements can be changed.

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also.

Syntax of tuple() Function

The syntax of tuple() function in Python is:

tuple(iterable)

Parameters of tuple() Function in Python

x – Where x is any valid . This parameter is required.

 

Compatibility

tuple() function is available and compatible with both Python 2.x and 3.x.

 

Return Value of tuple() Function in Python

tuple() function returns create a tuple in Python for the given input.

 

Python tuple() Function Example 1

tup1 = ('Dog', 'Cat', 2222, 555555);
tup2 = (10, 20, 30, 40, 50 );
tup3 = "a", "b", "c", "d";
tup4 = ()

# list of tuples
tup5 = [(10, 20), (100, 200)]

Python tuple() Function Example 2

To write a tuple containing a single value you have to include a comma, even though there is only one value.

tup1 = (10, )