Tuple

A tuple is a collection which is ordered andĀ unchangeable. In Python tuples are written with round brackets.

Example

Create a Tuple:

thistuple = ("apple", "banana", "cherry")
print(thistuple)

 

Output

Access Tuple Items

You can access tuple items by referring to the index number:

Example

Return the item in position 1:

thistuple = ("apple", "banana", "cherry")
print(thistuple[1])

 

Output

Change Tuple Values

Once a tuple is created, you cannot change its values. Tuples areĀ unchangeable.

Example

You cannot change values in a tuple:

thistuple = ("apple", "banana", "cherry")
thistuple[1] = "blackcurrant"
# The values will remain the same:
print(thistuple)

 

Output

Leave a Reply

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