Variables can be of the datatype tuple. A tuple is collection that cannot be modified.
It differs from a list, in that lists can be changed or modified after creation.
A tuple is defined using parenthesis ( and ) instead of [ and ] that a list uses.
If you want to change the data during program execution, use a list instead of a tuple.
Python tuple
A tuple is an immutable sequence type. A tuple is a sequence, but its values cannot be altered.
The values of a tuple cannot change. This is very important, because this is the only difference between a list.
To create a tuple, all you need to do is define a set of comma-separated values between the parenthesis.
An empty tuple would be defined as:
tup1 = () |
be careful with parenthesis,
()is a tuple and[]is a list and{}a dictionary
A tuple with one item ( a comma is needed in the end ):
x = (1,) |
x = (1,2,3,4) |
You can create all kinds of tuples:
t1 = ('physics', 'chemistry', 'geography', 'history') |
Note: Even if your tuple has only one element, the comma is needed between the parenthesis.

Accessing tuples
To access individual elements, we use square brackets. To print the first element (Python starts counting from zero):print(x[0])
print(x[1])
You can access tuples both in the interpreter and during code execution.
In the interpreter python, you can type commands.
Lets create a tuple
t1 = ('coffee','tea','orange juice','lemonade') |
Then output some elements
t1[0] |

And why not try to change a value
t1[1] = 'water' |
Remember, the contents of a tuple cannot be changed after creation.

To print the last element, you can count from the back using the minus sign.print(x[-1])
Tuple Unpacking
You can unpack a tuple. A package tuple object has more than one value like:
t = (1,2,3) |
To unpack the tuple you can do this:
x,y,z = t |
Then each variable (x,y,z) will contain one of the tuples values:
x |

When unpacking, the number of variables must match the number of values in the tuple:
t = (1,2,3) |
Tuple assignment
If you define a tuple, Python allows the parenthesis to be left out. In Python these are the same:
t = (1,2,3) |
So you can do unpacking like this:
t = 1,2,3 |
So it works both if parenthesis are written or left out. For clarity, you could add them.
When in doubt, you can always call type().
type(t) |
Python tuple append
Once defined, the contents of a tuple cannot be changed.
t = (1,2,3) |
That is by design. If you want to append values, you should use a list.
Note: Tuples cannot be changed. But what you can do, is create a new tuple and overwrite it.
However, you can add a tuple to a tuple:
t = t + (4,) |
A new element has been appended at the end of the tuple, by appending two tuples.
Python tuple insert
Start with a tuple, like this:
t = (1,2,4,5) |
To insert a new tuple, create three tuples and the tuple to insert will go in the middle.
So you can use tuple slicing [start:end] and combine.
The example below shows how a new tuple is created with a new element in the middle:
t = t[0:2] + (3,) + t[2:4] |
Python tuple modify
The contents of a tuple cannot be changed. If you try it will throw an TypeError.
t[1] = 2 |
You can get around this by creating a new tuple.
t = (1,2,3,4,5) |
Python tuple delete
An element cannot be deleted. You can only create a new tuple without that element.
If you have this tuple:
t = (1,2,3,4,5) |
You can create a new one using slicing:
# Create new tuple |
If you want to change the contents of a tuple (append, delete, modify,…) you should use a list instead.
Python tuple to list
To convert a tuple to a list, call the list() function.
# create tuple |
# convert tuple to list |
If you output mylist, you’ll see its a list:
mylist |
You can see the difference between lists and tuples by their parenthesis.
- Tuples use
() - lists uses
[].
Python list to tuple
A list can be converted to a tuple by calling the tuple() function:
t = tuple(mylist) |
Variable t is then a tuple:
t |
If you want to try this yourself, the exercises on PyChallenge turn the same idea into a few short problems with instant feedback.

what's the difference between Lists and tuples? Is the tuples similar with enumeration of C language?
Tuples cannot be modified once created. The only way to change a tuple is to create a new one and replace the existing one.
Hi,
I don't understand why the last numeric character in the multiple tuple does not have a comma after it?
If it is a stand alone single character, it has a comma; and yet if it is the last numeric character in a string of numerics, then each numeric has a comma after it - with the exception of the last numeric, why?
A comma on the end is required with a single element, but not if you have multiple elements. A tuple is defined as: values separated by commas.
Why a comma for one element? Python has otherwise no way of knowing its a tuple, consider:
Python wouldn't know if it's a tuple, in this case it's just a number. That's why a tuple always needs at least one comma. If we have more than one number, it already has commas.