Contents
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
Checkout this video:
Introduction
Python tuples are immutable data structures that allow you to store and access data. In this article, we’ll show you how to create a tuple in Python.
What is a Tuple?
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. For example −
Creating a Tuple
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.
Creating a tuple is as simple as putting different comma-separated values. For example −
tup1 = (‘physics’, ‘chemistry’, 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = “a”, “b”, “c”, “d”
Accessing Tuple Elements
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.. A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.
Creating a Tuple
A tuple can be created by just placing the items inside a parentheses separated by commas.
If you need to create a tuple with only one item, you need to add a comma after the item, otherwise Python will not recognize it as a tuple.
Accessing Tuple Elements
Tuples are unordered, so you cannot access items using indexes like you would with lists.
But you can access tuple items by referring to the item’s value:
Deleting Tuple Elements
In Python, tuples are immutable i.e. you cannot delete or add elements to a tuple.
immutable means you can’t delete, add or modify the elements in a tuple once it is created.
If you have the need to delete items from a tuple, you can create a new tuple without those items.
Creating a tuple without specified item(s) is known as deletion of tuple elements. For this, we use the slicing operator to create a new sub-tuple from the existing tuple by specifying the element position to be deleted.
Updating Tuple Elements
In order to update a tuple, one way would be to convert it into a list, then update the respective element and finally convert it back into a tuple.
Example:
tup = (‘Geeks’, 11, ‘for’, ‘geeks’)
print(type(tup)) # Output:
# Updating 2nd element of Tuple tup
# from 11 to 22 after converting it into a list
lst = list(tup) # Converting Tuple into List
lst[1] = 22 # Updating 2nd element of List lst from 11 to 22
tup = tuple(lst) # Converting List back intoTuple res= (‘Geeks’, 22, ‘for’, ‘geeks’)
Tuple Methods
Python has a number of tuple methods that allow us to perform various operations on tuples.
The len() method returns the number of elements in a tuple:
>>> len((1, 2, 3))
3
>>> t = (‘a’, ‘b’, ‘c’)
>>> len(t)
3
The max() and min() methods return the largest and smallest elements in a tuple:
>>> t = (‘a’, ‘b’, ‘c’)
>>> max(t)
‘c’
>>> min(t)
‘a’
Tuple Assignment
Python tuples are very similar to lists, but they are constructed using parentheses, rather than square brackets. They are also immutable, which means that once you have created a tuple, you cannot change its values.
To create a tuple, you can use the comma operator without any surrounding parentheses:
“`
>>> t = 1, 2, 3
>>> type(t)
“`
You can also use the built-in tuple function:
“`
>>> t = tuple(range(3))
>>> type(t)
“`
Tuple Packing and Unpacking
In Python, tuples are created by placing items separated by commas inside parentheses. Unlike lists, tuples are immutable, which means they cannot be modified after creation. Packing and unpacking tuples is a convenient way to exchange data between programs written in different languages, or between different parts of the same program.
To pack a tuple, you place the items to be packed inside parentheses and separate them by commas. For example:
t = (‘a’, ‘b’, ‘c’)
This tuple has three items, so it is sometimes called a 3-tuple. To unpack a tuple, you place the variable names where you want the items to be unpacked and separate them by commas. For example:
a, b, c = t
After this statement is executed, the variables a, b and c will contain the values ‘a’, ‘b’ and ‘c’, respectively.
Conclusion
In conclusion, creating a tuple in Python is an easy process. First, decide what data you want to include in your tuple. Second, use parentheses () to enclose the data. Finally, separate each piece of data with a comma. Once you have your tuple created, you can access individual elements by using their index values.