Introduction to Tuples

We saw that lists and strings have many common properties, such as indexing and slicing operations.
They are two examples of sequence data types (Sequence Types — list, tuple, range). Since Python is an evolving language, other sequence data types may be added..
There is also another standard sequence data type: the tuple.
A tuple consists of a number of values separated by commas & enclosed in parentheses
Tuple items are ordered, unchangeable, and allow duplicate values.

Tuple Creation

Creating empty tuple

Tuple1 = ()
print(Tuple1)

Creating a Tuple with the use of string

Tuple1 = ('RGUKT', 'For')
print(Tuple1)

Creating a Tuple with the use of list

list1 = [1, 2, 4, 5, 6]
print(tuple(list1))

Creating a Tuple with Mixed Datatype

Tuple1 = (5, 'Welcome', 7, 'RGUKT')
print(Tuple1)

Creating a Tuple with nested tuples

Tuple1 = (0, 1,2,3)
Tuple2 = ('python', ,'RGUKTWEB')
Tuple3 = (Tuple1, Tuple2)
print(Tuple3)

Creating a Tuple with repetition

Tuple1 = ('RGUKTWEB',) * 3
print(Tuple1)

##A tuple consists of a number of values separated by commas & enclosed in parentheses

t=(12,"RGUKTWEB",145.89,2+5j,"SKLM","RGUKT")
print(t)

Note: Creation of Python tuple without the use of parentheses is known as Tuple Packing.

Accessing of Tuples

Tuples are immutable, and usually, they contain a sequence of heterogeneous elements that are accessed via unpacking or indexing (or even by attribute in the case of named tuples).
Note: In unpacking of tuple number of variables on the left-hand side should be equal to a number of values in given tuple a.
Accessing Tuple with Indexing

Tuple1 = tuple("RGUKTWEB")
print(Tuple1[0])
Tuple unpacking
Tuple1 = ("RGUKT", "SKLM", "ANDHRA PRADESH")
 a, b, c = Tuple1
print(a)
print(b)
print(c)
Concatenation of Tuples

Concatenation of tuple is the process of joining two or more Tuples.
Concatenation is done by the use of ‘+’ operator.
Concatenation of tuples is done always from the end of the original tuple.
Other arithmetic operations do not apply on Tuples.
Note: Only the same datatypes can be combined with concatenation, an error arises if a list and a tuple are combined.
Concatenation of tuples

Tuple1 = (0, 1, 2, 3)
Tuple2 = ('RGUKT', 'For', 'RURAL') 
Tuple3 = Tuple1 + Tuple2
print(Tuple3)
Slicing of Tuple

Slicing of a Tuple is done to fetch a specific range or slice of sub-elements from a Tuple.
Slicing can also be done to lists and arrays.
Indexing in a list results to fetching a single element whereas Slicing allows to fetch a set of elements.
Note- Negative Increment values can also be used to reverse the sequence of Tuples.
Slicing of a Tuple with Numbers

Tuple1 = tuple('RGUKTSKLMAP')
print("Removal of First Element")
print(Tuple1[1:]) ## Removing First element
print(Tuple1[::-1]) ## Reversing the Tuple
print(Tuple1[4:9]) ## Printing elements of a Range

Deleting a Tuple

Tuples are immutable and hence they do not allow deletion of a part of it.
The entire tuple gets deleted by the use of del() method.
Note- Printing of Tuple after deletion results in an Error.
Deleting a Tuple

Tuple1 = (0,1,2,3,4)
del Tuple1
print(Tuple1)

Python Tuple Methods

Built in MethodDescription
index()Find in the tuple and returns the index of the given value where it’s available
count() Returns the frequency of occurrence of a specified value

Python Built-In Functions

Built-In FunctionsDescription
all()Returns true if all element are true or if tuple is empty
any()Return true if any element of the tuple is true. if tuple is empty, return false
len()Returns length of the tuple or size of the tuple
enumerate()Returns enumerate object of tuple
max()return maximum element of given tuple
min()return minimum element of given tuple
sum()Sums up the numbers in the tuple
sorted()input elements in the tuple and return a new sorted list
tuple() Convert an iterable to a tuple.

Tuples VS Lists

Similarities Differences
Functions that can be used for both lists and tuples: len(), max(), min(), sum(), any(), all(), sorted() Methods that cannot be used for tuples: append(), insert(), remove(), pop(), clear(), sort(), reverse()
Methods that can be used for both lists and tuples: count(), Index() we generally use ‘tuples’ for heterogeneous (different) data types and ‘lists’ for homogeneous (similar) data types.
Tuples can be stored in lists. Iterating through a ‘tuple’ is faster than in a ‘list’.
Lists can be stored in tuples. ‘Lists’ are mutable whereas ‘tuples’ are immutable.
Both ‘tuples’ and ‘lists’ can be nested. Tuples that contain immutable elements can be used as a key for a dictionary.