Javaexercise.com

Python Tuple Introduction

Tuple is a data structure in Python that is used to store data. It allows to store any type of data like integer, float, string, char, etc, and we can store similar type of elements as well.

The tuple is immutable in nature, it means we cannot change or update tuple elements once it is created.

In this topic, we are discussing the tuple and its operations like creating a tuple, accessing tuple elements, updating tuple, removing tuple elements or iterating tuple, etc.

How to create Tuple in Python?

Let's start with an example to create a tuple which can store heterogeneous data. Here, we created several tuples, a tuple of numbers, a tuple of string, and a tuple of mix values(integer, string, and float).

# Working with Python tuple

# Create a tuple
numbers = (100,200,400,122)
print(numbers)

# Create a tuple
fruits = ("Apple","Orange","Mango")
print(fruits)

# Create a tuple
mix = (12,"India","Delhi",11.20)
print(mix)

Output:

(100, 200, 400, 122)
('Apple', 'Orange', 'Mango')
(12, 'India', 'Delhi', 11.2)

How To Access Tuple Elements in Python?

After creating a tuple, let's access its elements. We can use an index value to fetch any elements. The tuple index starts from 0, so the first element will be at 0 index value.

# Working with Python tuple

# Create a tuple
numbers = (100,200,400,122)
print(numbers)

# Access tuple elements
item = numbers[0] # first element
print(item)

# Access tuple elements
item = numbers[2] # third element
print(item)

# Access tuple elements
item = numbers[len(numbers)-1] # last element
print(item)

Output:

(100, 200, 400, 122)
100
400
122

How To Update Tuple in Python?

We can not update tuple because the tuple is immutable which means it does not allow to modify its elements. If we try to modify its elements, Python throws an error, TypeError: 'tuple' object does not support item assignment.

# Working with Python tuple

# Create a tuple
numbers = (100,200,400,122)
print(numbers)

# change tuple
numbers[0] = 150
print(numbers)

Output:

(100, 200, 400, 122)

TypeError: 'tuple' object does not support item assignment

How To Remove Tuple Elements in Python?

Like modification, tuple does not allow to remove an element from it. We can not delete its elements but if we try to remove then Python throws an error "TypeError: 'tuple' object doesn't support item deletion".

# Working with Python tuple

# Create a tuple
numbers = (100,200,400,122)
print(numbers)

# Remove tuple element
del numbers[0]
print(numbers)

Output:

(100, 200, 400, 122)
TypeError: 'tuple' object doesn't support item deletion

How To Traverse Tuple Elements in Python?

Since a tuple is a sequence of data, we can iterate it using for loop to traverse its elements. Here, we used for loop to print each element of the tuple.

# Working with Python tuple

# Create a tuple
numbers = (100,200,400,122)
print(numbers)

# Iterate tuple element
for item in numbers:
  print(item)

Output:

(100, 200, 400, 122)
100
200
400
122


Conclusion

Well, in this topic, we have learned to create a tuple, access, update, delete its elements. We created tuple and traverse its elements as well.

If we missed something, you can suggest us at - info.javaexercise@gmail.com