Tuples in Python

Tuples in Python

Tuples are sequences that function like lists, the only difference is that they are immutable, which means that we cannot add, delete or modify its elements once created. It also makes them super efficient compared to the list. Tuples are used to store a list of items that do not change.

The values of a tuple are syntactically separated by "commas". Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This makes it easier to understand Python tuples.

Creation of a Tuple

In Python, tuples are created by placing a sequence of values separated by commas with or without parentheses ().

Notes !  Creating Python tuple without the use of parentheses is known as "tuple packing".
Example 1 :
                                # Create a tuple
tuple1=()
print(tuple1)

#tuple with 3 string elements
tuple2=("Meknes","Marrakech", "ESSAOUIRA")
print(tuple2)

#Tuple with single element
tuple3 =(4,)
print(tuple3)

#Tuple without a parentheses
tuple4 = 3,5,7,10
print(tuple4)

()
('Meknes', 'Marrakech', 'ESSAOUIRA')
(4,)
(3, 5, 7, 10)

We can also use the constructor i.e. tuple (), which accepts any type of sequence or iterative object.

Example 2 :
                                # Tuple with signle string element
t1 = tuple("abcd")
print(t1)

#tuple containing a range
t2 = tuple(range(1, 10))
print(t2)

# Tuple from a list
t3 = tuple([1,2,3,4])
print(t3)

('a', 'b', 'c', 'd')
(1, 2, 3, 4, 5, 6, 7, 8, 9)
(1, 2, 3, 4)

Tuples allow you to assign values to more than one variable at a time.
The number of variables (left) and the number of elements in the tuple (right) must match, otherwise you will get an error.

Example 3 :
                                name,age=("Ismail",25)
print("Name : ",name)
print("Age : ",age)


Name : Ismail
Age : 25

Tuples operations

A tuple is essentially an immutable list. As a result, most of the operations that can be performed on a list are also valid for tuples. Here are some examples of such operations:

•     Access a single item or item slices using the [] operator.
•     Built-in functions such as max (), min (), sum () are valid on a tuple.
•     membership operators in and not in
•     Comparison operators to compare tuples.
•     Operators + and *.
•     loop for to go through the elements.
• ...

The only operations that tuple does not support are those that modify the list itself. Therefore, methods such as append(), insert(), remove(), reverse(), and sort() do not work with tuples.

Example 4 :
                                tuple1=(2,4,5,7,19)
tuple2=(5,9,5)

# Show the fourth element
print("tuple1[3] : ",tuple1[3])

# First three elements
print("tuple1[:3] : ",tuple1[:3])

# The max element
print("max : ",max(tuple1))

# The sum of elements
print("Sum : ",sum(tuple1))

tuple3=tuple1 + tuple2
print("concatenation of 2 tuples : ",tuple3)

# repeat a tuple 2 times
tuple4=tuple1 * 2
print("repeat a tuple 2 times : ", tuple4)

for elem in tuple1:
print(elem,end=' , ')


tuple1[3] : 7
tuple1[:3] : (2, 4, 5)
max : 19
Sum : 37
concatenation of 2 tuples : (2, 4, 5, 7, 19, 5, 9, 5)
repeat a tuple 2 times : (2, 4, 5, 7, 19, 2, 4, 5, 7, 19)
2 , 4 , 5 , 7 , 19 ,

Since tuples are very similar to lists, both are used in similar situations. However, implementing a tuple than a list has certain advantages. Below are some of the key benefits:

•     We generally use tuple for heterogeneous (different) data types and list for homogeneous (similar) data types.
•     As the tuples are immutable, looping over a tuple is faster than with list. There is therefore a slight improvement in performance.
•     Tuples containing immutable elements can be used as a key for a dictionary. With lists, this is not possible.
•     If you have data that does not change, implementing it as a tuple will ensure that it remains write-protected.