Python - Tuple

Python

Share
python logo

In this tutorial we will learn about tuple in Python.

We briefly talked about tuple in the Python - Data Type tutorial.

What is a tuple?

Tuples are immutable ordered sequence of items similar to a List.

Once a tuple is created it can't be changed.

To create a tuple in Python we use ( ) parenthesis and separate the items using , comma.

Items of a tuple are indexed and the first item gets the index 0.

In the following example we are creating a tuple of integer values.

myTuple = (1, 2, 3)

The tuple() constructor

We use the tuple() constructor to create a tuple in Python.

# tuple
myTuple = tuple((1, 3.14, 'yusufshakeel', 1+2j, True))

print("Type of myTuple:", type(myTuple))
print(myTuple)

Output

Type of myTuple: <class 'tuple'>
(1, 3.14, 'yusufshakeel', (1+2j), True)

Accessing tuple items

Items of a tuple are indexed and the first item of the list gets index 0, the second item of the list gets index 1 and so on.

So, to access the items of a tuple we use the following syntax tuple[i].

Where, tuple represents a tuple variable and i represents the index of an item in the tuple.

In the following example we are printing item at index 1 in the given tuple.

# tuple
myTuple = (1, 3.14, 'yusufshakeel', 1+2j, True)

# output
print(myTuple[1])        # 3.14

In the following Python program we are printing all the items of a tuple using for loop

# tuple
myTuple = (1, 3.14, 'yusufshakeel', 1+2j, True)

# output
for i in myTuple:
  print(i)

We will get the following output.

1
3.14
yusufshakeel
(1+2j)
True

Adding new items to tuple

Once a tuple is created we can't add new items to the tuple.

Updating existing items of a tuple

Once a tuple is created we can't update existing items of a tuple.

Deleting existing items of a tuple

Once a tuple is created we can't delete existing items of a tuple.

Counting tuple items

We use the len method to count the total number of items present in the tuple.

In the following Python program we are printing the total number of items in the tuple.

# tuple
myTuple = (1, 3.14, 'yusufshakeel', 1+2j, True)

# output
print(len(myTuple))    # 5

Check existence of tuple item

To check if an item exists in a give tuple we use the in - membership operator.

The in operator will return True if the item exists and False if it is not present.

In the following example we are checking existence of items in a tuple.

# tuple
myTuple = (1, 3.14, 'yusufshakeel', 1+2j, True)

# output
print(1 in myTuple)               # True
print(2 in myTuple)               # False

Concat tuples

To concat two tuples we use the + operator.

In the following Python program we are concatenating two tuples.

# tuple
veg = ('Potato', 'Tomato')
fruit = ('Apple', 'Mango')

# concat
result = veg + fruit

# output
print("veg", veg)
print("fruit", fruit)
print("result", result)

We will get the following output.

veg ('Potato', 'Tomato')
fruit ('Apple', 'Mango')
result ('Potato', 'Tomato', 'Apple', 'Mango')
Share