Python Tuples
Python Tuples
Tuples are an immutable data type, meaning their elements cannot be changed after they are generated. Each
element in a tuple has a specific order that will never change because tuples are ordered sequences.
Forming a Tuple:
All the objects-also known as "elements"-must be separated by a comma, enclosed in parenthesis ( ). Although
parentheses are not required, they are recommended.
Any number of items, including those with various data types (dictionary, string, float, list, etc.), can be contained in a
tuple. A comma must separate the element to be recognized as a tuple.
single_tuple = ("Tuple")
print( type(single_tuple) )
single_tuple = ("Tuple",)
print( type(single_tuple) )
single_tuple = "Tuple",
print( type(single_tuple) )
Indexing
Indexing We can use the index operator [ ] to access an object in a tuple, where the index starts at 0.
The indices of a tuple with five items will range from 0 to 4. An Index Error will be raised assuming we attempt to get
to a list from the Tuple that is outside the scope of the tuple record. An index above four will be out of range in this
Code
# Creating a tuple
print(tuple_[0])
print(tuple_[1])
# trying to access element index more than the length of a tuple . This will result in an error.
print(nested_tuple[0][3])
print(nested_tuple[1][1])
Negative Indexing
Python's sequence objects support negative indexing. The last thing of the assortment is addressed by - 1, the second
last thing by - 2, etc.
Slicing
Tuple slicing is a common practice in Python and the most common way for programmers to deal with practical issues.
Look at a tuple in Python. Slice a tuple to access a variety of its elements. Using the colon as a straightforward slicing
operator (:) is one strategy. To gain access to various tuple elements, we can use the slicing operator colon : .
Deleting a Tuple
A tuple's parts can't be modified, as was recently said. We are unable to eliminate or remove tuple components as a
result. However, the keyword del can completely delete a tuple.
Code
# Creating a tuple
print(tuple_)
Code
# Creating a tuple
print(item)
The + operator can be used to combine multiple tuples into one. This phenomenon is known as concatenation.
We can also repeat the elements of a tuple a predetermined number of times by using the * operator.
The aftereffects of the tasks + and * are new tuples. ( ie., a new tuple is created )
Since tuples are quite similar to lists, both of them are used in similar situations. However, there are certain
advantages of implementing a tuple over a list:
We generally use tuples for heterogeneous (different) data types and lists for homogeneous (similar) data
types.
Since tuples are immutable, iterating through a tuple is faster than with a list. So there is a slight performance
boost.
Tuples that contain immutable elements can be used as a key for a dictionary. With lists, this is not possible.
If you have data that doesn't change, implementing it as tuple will guarantee that it remains write-protected.
Tuple is immutable, although you can use the + operator to concatenate several tuples. The old object is still present
at this point, and a new object is created.
Example
s=(2,5,8)
print(s_append)
print(s)
Output
(2, 5, 8)
Note− Concatenation is only possible with tuples. It can't be concatenated to other kinds, such lists.
s=(2,5,8)
print(s_append)
print(s)
Output
You can concatenate a tuple with one element if you want to add an item to it.
Example
s=(2,5,8)
s_append_one = s + (4,)
print(s_append_one)
Output
(2, 5, 8, 4)
Note − The end of a tuple with only one element must contain a comma as seen in the above example.
# A program to append new elements to a tuple , getting the values from the keyboard.
t = tuple()
for i in range(5) :
print ( t )
big = max(t)
You’ll learn another method to append a value to a Python tuple. Specifically, you’ll learn tuple unpacking (using the *
) operator, to append a value to a tuple. Similar to the methods described above, you’ll actually be creating a new
tuple, but without needing to first create a list.
a_tuple = (1, 2, 3)
a_tuple = (*a_tuple,18)
print(a_tuple)
There is one fringe exception where you can append to a tuple without raising an error. Specifically, if one of your
tuples’ items is a mutable object, such as a list, you can modify the item. Really, we’re not appending to the tuple itself,
but to a list contained in the tuple.
a_tuple[2].append(27)
print(a_tuple)
You can insert new elements to a tuple or do any modification by this method. This is suitable for tuples, sets.
t = ( 1, 2, 3 )
print( t )
output will be : ( 1, 2, 3, 19 )
The methods find() and count() of the list can also be used as the methods of the tuple to find an occurrence of an
element and to count the frequency of an element respectively.
~~~~~ ~~~~~