Python Tuple

A Tuple is a collection of immutable Python objects separated by commas. Tuples are just like lists, but we cannot change the elements of a tuple once it is assigned whereas in a list, elements can be changed. The main difference being that tuple manipulation are faster than list because tuples are immutable. Since they're immutable, they can be used as keys for a dictionary. Also, Tuples are used whenever you want to return multiple results from a function.
Creating a Tuple
A tuple is defined using parenthesis. An empty tuple can be formed by an empty pair of parentheses.
example
output
Creating Tuple with values
example
output
Python Tuple with mixed datatypes
example
output
example
output
Accessing tuple values
To access individual elements, we use [] square brackets where the index starts from 0.
example
output
Adding Tuple
example
output
Loops and Tuple
example
output
Tuple print with index number
example
output
Concatenation of Tuples
You can add two or more Tuples by using the concatenation operator "+".
example
output
Tuple length
The function len returns the length of a Tuple, which is equal to the number of its elements.
example
output
Slicing Python Tuples
Python slice extracts elements, based on a start and stop.
example
output
str[1:3] - The 1 means to start at second element in the Tuples (note that the slicing index starts at 0). The 3 means to end at the fourth element in the list, but not include it. The colon in the middle is how Python's Tuples recognize that we want to use slicing to get objects in the list.
example
output
example
output
Delete Tuple Elements

Tuples in Python are immutable. This means that once you have created a tuple, you can't change the elements contained within it. To explicitly remove an entire tuple, just use the del statement.
example
output
Updating a Tuple
Since tuples are immutable, it cannot be changed once it has been assigned. But, if the element is itself a mutable datatype like list, its nested items can be changed.
example
output
Tuples as return multiple values
Functions are always only return a single value, but by making that value a tuple, we can effectively group together as many values as we like, and return them together.
example
output
Nesting of Tuples
example
output
Converting list to a Tuple
You can convert a List to a Tuple by using tuple()
example
output
Repetition in Tuples
Using the * operator repeats a list a given number of times.
example
output
Tuple repetition Count
Tuple.count(x) return the number of times x appears in the Tuple.
example
output
zip() function
To loop over two or more sequences at the same time, the entries can be paired with the zip() function.
example
output
Tuple min(), max()
The min() returns the minimum value from a tuple and max() returns the maximum value from the tuple.
example
output
Tuple Packing and Unpacking
Process of creating collection by comma separating items within parentheses ( ) is known as packing of tuple while assigning individual items from a tuple to individual variables using assignment operator is known as unpacking of tuples.
