Tuple data type:
A tuple is a collection of elements/items of different data types is called tuple.
Difference between list and tuple is: List elements can be modified as it is mutable. Whereas tuple elements can not be modified as it is immutable. So tuple is treated as a read-only list.
Individual elements of the tuple can be referenced using square braces as a[0],a[1]
A tuple is a collection of elements/items of different data types is called tuple.
A tuple is similar to list. Elements are
separated by comma operator and enclosed within parentheses ().
Difference between list and tuple is: List elements can be modified as it is mutable. Whereas tuple elements can not be modified as it is immutable. So tuple is treated as a read-only list.
Syntax:
Variable=(item1,item2,….)
Example:
t=(10,-20,10,2.4,”raj”,”$”,True)Individual elements of the tuple can be referenced using square braces as a[0],a[1]
Ex:
a=(10,20,30,40)#collection of items like arrays
a=(10,-20,12.5,'Ravi',"Sita") # A tuple may have different types like objects
a=(10,-20,10) #duplication
a[1]=30 # Error
print(a[0]) à 10
a=(10,20,30,40)
print(a):-> (10,20,30,40)
print(a[1:3]) :-20,30
print(a[-1]):->40
print(a*2):->
10,20,30,40,10,20,30,40
Ex1:
a=(10,20,30)
a[0]=40 #error as it is
immutable
A tuple can be modified when
we list is initialised in a tuple
Example:
>>> t=(2,3,[4,5])
>>> t[0]
2
2
>>> t[1]
3
>>> t[1]=7
>>>t[1]
7
3
>>> t[1]=7
>>>t[1]
7
>>> t[2]
[4, 5]
>>> t[2][0]=6
>>> print(t)
(2, 7, [6, 5])
>>>
Creating a tuple with one element
within parentheses is not enough. We should give comma separator to indicate it
is tuple.
Ex:
>>>
a=("ravi")
>>> type(a)
<class 'str'>
>>>
a=("ravi",)
>>> type(a)
<class 'tuple'>
Individual elements can be
accessed by using square brackets [ ].
>>> a=(10,20,30)
>>> a[0]
10
>>> a[1]
20
>>> a(0) # Error as to access any element we should use [ ]
Packing and unpacking tuples:
A tuple can also be created
without using parentheses. This is called packing tuples
Ex:
>>>
t=3,4,6,"ravi"
>>> print(t)
(3, 4, 6, 'ravi')
Unpacking:
>>> a,b,c,d=t
>>> a
3
>>> b
4
>>> c
6
>>> d
'ravi'
>>> a,b,c=t # Error
because no of elements and no of variable should be matched.
1. Slicing operation in Tuple:
1. Slicing operation in Tuple:
We can access a range of items
in a tuple by using the slicing operator (:)
Example:
a=(2,3,4,5)
print(a[1:3])
output:
(3,4)
2. Concatenation of
tuple
We can use + operator to
combine two tuples. This is called concatenation.
Ex:
>>> a=(2,4,6) #tuple
a is created
>>> b=(7,2.4,8,True)
#tuple b is created
>>> print(a+b)
(2, 4, 6, 7, 2.4, 8, True)
Ex2:
a=(10,20,30)
print(type(a))
#<class 'tuple'>
print(a)#(10, 20, 30)
print(id(a))#59342480
a=a+(40,)
print("after
concatenation")
print(a)#(10, 20, 30,
40)
print(id(a))#66274000
3. Repeating the tuple
elements
We can also repeat the
elements in a tuple for a given number of times using the * operator.
Both + and * operations result
in a new tuple
Example:
>>> a=(4,6,7)
>>> print(a*3)
(4, 6, 7, 4, 6, 7, 4, 6, 7)
4. Deleting the tuple
We can not change or delete
the tuple elements. But deleting a tuple entirely is possible using the keyword
‘del’
Example:
a=(6,8,2,3)
>>> type(a)
<class 'tuple'>
>>> print(a)
(6, 8, 2, 3)
>>> del a[1] #Error
>>>a.remove(8) #Error
>>> del a # Valid,
entire tuple a is deleted
5. Converting into Tuple:
a=("ravi")#
string
print(type(a))
b=str(a)
print(type(b))
c=tuple(b)#converting
from string to tuple
print(type(c))
"""
<class 'str'>
<class 'str'>
<class 'tuple'>
"""
Tuple Membership:
We can test if an item exists
in a tuple or not, using the keywords :
in, not in
>>> t=('c','a','t')
>>> print('a' in t)
True
>>> print(a in t)
#Error
>>> print('t' in t)
True
>>> print('j' not in
t)
True
>>> print('a' not in
t)
False
#To get the nth element from last of a tuple
using positive and -ve index
a=("r","a","j","e","n","d","r","a")
print(type(a))#tuple
#get 4th element of
the tuple by index
n=a[3]
print(n)#e
#get 4 :nth element
from last by negative index
print(a[-4])#n
Advantages of tuple over list:
Since tuples are similar to
lists, both of them are used in similar situations.
We generally use tuple for
different data types (Heterogeneous) and list for homogeneous (same data type)
Difference between Lists and Tuples
Since
tuples are immutable, tuple is faster than list so there is a slight performanceDifference between Lists and Tuples
List
|
Tuple
|
The elements of a list are mutable
|
elements of a tuple
are immutable
|
When we need to change the data in
future, list would be a right data type.
|
When we do not want
to change the data over time, the tuple is a preferred data type
|
When we are iterating (in looping
statements) elements, performance is slow
|
Iterating over the
elements of a tuple is faster .
|
The elements of list are enclosed in square bracket.
Ex: a=[10,20,30,40]
|
Elements of a tuple
are enclosed in parenthesis
Ex: a=(10,20,30,40)
|
Assignments:
(1.) a = (1, 2, 4, 3), which of the following is incorrect?
a) print(a[3])
b) a[3] = 45
c) print(max(a))
d) print(len(a))
Answer: b
(2.) What will be the output?
>>>t=(1,2,4,3)
>>>t[1:-1]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
Answer: c
(3.) What will be the output?
Next topic: Set data
type
>>>t = (1, 2)
>>>2 * t
a) (1, 2, 1, 2)
b) [1, 2, 1, 2].
c) (1, 1, 2, 2)
d) [1, 1, 2, 2].
Answer: a
Previous topic: List
Related video Click here to see related videos
You may like the following topics:
What is Ordered List
What is Immutable?
What is Sequences in Python
No comments:
Post a Comment