Tuple data type is immutable.
Let's denote the tuple in round brackets.
Create Tuple
a=(1,2,3,4)
print(a)
''''
Output:-
(1, 2, 3, 4)
''''
A tuple can have more than one multiple value.
A tuple can contain values of various data types.
a=("sakthi",2.5,5)
print(a)
''''
output:
('sakthi', 2.5, 5)
''''
Let's see what happens if we assign a single value to a tuple.
a=(1)
print(a)
''''
output:
1
''''
Description:-
In output we get 1 but it doesn't come with round bracket. The Python interpreter takes this as an integer.
a=(1,)
print(a)
''''
output:
(1,)
''''
If you want to give only single value in Tuple then you should give it as (1,). Must give comma. Otherwise it is not treated as a tuple.
num=1,2,3,4
print(num)
''''
output:
(1, 2, 3, 4)
''''
Description:-
We have given a multiple value in the variable called num, but we have not given it inside the round bracket. But for us the value is displayed as a tuple in the output.
When we create a tuple, it is not mandatory to give the value in the round bracket. You can also give it without giving it inside the round bracket. But you must give the comma (,) separator.
Operation In Tuple
len()
Counts the number of values inside a tuple using the len() method.
val=(1,2,3,4)
print(len(val))
''''
output:
4
''''
val=(1,2,("good"),78)
print(len(val))
output:
4
Description:-
A tuple is created in this program.
Another tuple is created inside that tuple.
len(val) -> returns 4 of the value contained in the tuple val.
Tuple Concatenation
Concatenation operation can be used to join more than one tuple together. (+) is the concatenation operator.
a=(1,2,3)
b=(4,5,6)
print(a + b)
''''
output:
(1,2,3,4,5,6)
''''
Description:-
a+b -> Here (+) concatenates two tuples a and b into a single tuple.
Tuple repetition
Repetition will repeat the number of times depending on the value we give.
a=(1,2,3)
print(a*2)
''''
output:
(1, 2, 3, 1, 2, 3)
''''
Description:-
(a*2) -> Here a tuple repeats the value contained in it twice.
a=(1,2,3)
b=(4,5,6)
print(a+2)
''''
output:
TypeError: can only concatenate tuple (not "int") to tuple
''''
Description:-
Always return a two-sided tuple during concatenation.
But we have given int value here. That's why the error is coming.
Tuple Value Access
Index value can be used to retrieve the value contained in the tuple.
a=("c","c++","java")
print(a[0])
print(a[1])
''''
output:
c
c++
''''
Packing and Unpacking
a=("c","2.5","20")
(name,height,age)=a
print(name,height,age)
''''
output:
c 2.5 20
''''
Description:-
A tuple of variable a is saved.
The tuple has 3 values inside it. The values are (“c”, “2.5″,”20″).
(name,height,age)=a
In this statement variable a has 3 values. So name takes the first value “c” and height takes the second value 25. Age takes the last value of 20.
The statement print(name,age,height) prints the value stored in name,age,height.
a=("c",20,2.5)
name,age,height=a
print(name,age,height)
''''
output:
c 20 2.5
''''
Description:-
name,age,height =a -> In this statement round bracket is not given around the tuple. But we get the same output.
Because tuple can be created without round bracket. The comma(,) separator is the key to the tuple.
a=("c",20,21,22,23,"js")
name,*age,height=a
print(name,age,height)
''''
output:
c [20, 21, 22, 23] js
''''
Description:-
The tuple in variable a has 6 values.
name, *age, height = a -> In this statement there are only 3 variables namely name, age, height.
Here name takes the first value in the tuple and height takes the last value.
*age converts the middle 4 values into a list.
Built-in method
Python has few built-in methods for tuple.
count()
a=(1,2,3,4,1,1)
print(a.count(1))
''''
output:
3
''''
Description:-
Here the value “1” is present 3 times in my tuple.
index()
a=(1,2,3,4)
print(a.index(2))
''''
output:
1
''''
Description:-
Here the value 2 is at index position one.