Tuple
- Tuple is an ordered collection, mostly preferred for heterogeneous type of data collection, but can be used for homogeneous data.
#program
t1 = ("Techno Xpresss", 28, 80.72) # heterogeneous data
t2 = (10, 20, 30) # homogeneous type of data
print("t1 : ", t1)
print("t1 : ", t2)
print("type(t1)", type(t1))
print("type(t2)", type(t2))
#output
t1 : ('Techno Xpresss', 28, 80.72)
t1 : (10, 20, 30)
type(t1) <class 'tuple'>
type(t2) <class 'tuple'>
- To define a tuple, we use ( ) (round brackets), each member is separated by ','
Syntax :
Tuple_name = (member1, member2,..)
Tuple_name = (member1, member2,..)
#program
t1 = ("Techno Xpresss", 28, 80.72)
print("t1 : ", t1)
print("type(t1) : ", type(t1))
#output
t1 : ('Techno Xpresss', 28, 80.72)
type(t1) : <class 'tuple'>
- Tuple can be defined as Empty Tuple.
#program
t1 = ("Techno Xpresss", 28, 80.72)
t2 = () # empty tuple
print("t1 : ", t1)
print("t1 : ", t2)
print("type(t1)", type(t1))
print("type(t2)", type(t2))
#output
t1 : ('Techno Xpresss', 28, 80.72)
t1 : ()
type(t1) <class 'tuple'>
type(t2) <class 'tuple'>
- Tuple with single element can also be defined
- but need to add ',' with single member,
- if ',' missed, it will take as the type of data, either int, float or str.
#program
t1 = ("Techno Xpresss", 28, 80.72)
t2 = ("Techno Xpresss")
t3 = (10)
t4 = (80.72)
t5 = ("Techno Xpresss",)
t6 = (10,)
t7 = (80.72,)
print("t1 : ", t1)
print("type(t1)", type(t1))
print("t2 : ", t2)
print("type(t2)", type(t2))
print("t3 : ", t3)
print("type(t3)", type(t3))
print("t4 : ", t4)
print("type(t4)", type(t4))
print("t5 : ", t5)
print("type(t5)", type(t5))
print("t6 : ", t6)
print("type(t6)", type(t6))
print("t7 : ", t7)
print("type(t7)", type(t7))
#output
t1 : ('Techno Xpresss', 28, 80.72)
type(t1) <class 'tuple'>
t2 : Techno Xpresss
type(t2) <class 'str'>
t3 : 10
type(t3) <class 'int'>
t4 : 80.72
type(t4) <class 'float'>
t5 : ('Techno Xpresss',)
type(t5) <class 'tuple'>
t6 : (10,)
type(t6) <class 'tuple'>
t7 : (80.72,)
type(t7) <class 'tuple'>
- To access individual element of Tuple, indexing is used.
#program
t1 = ("Techno Xpresss", 28, 80.72)
print("t1 : ", t1)
print("t1[0] :", t1[0])
#output
t1 : ('Techno Xpresss', 28, 80.72)
t1[0] : Techno Xpresss
- Tuple allows Positive and negative indexing.
#program
t1 = ("Techno Xpresss", 28, 80.72)
print("t1 : ", t1)
print("t1[0] :", t1[0])
print("t1[-3] :", t1[-3])
print("t1[2] :", t1[2])
print("t1[-1] :", t1[-1])
#output
t1 : ('Techno Xpresss', 28, 80.72)
t1[0] : Techno Xpresss
t1[-3] : Techno Xpresss
t1[2] : 80.72
t1[-1] : 80.72
- Tuple allows multiplication and addition operations.
#program
t1 = ("Techno Xpresss",)
t2 = (28, 80.72)
print("t1 : ", t1)
print("t2 : ", t2)
print("t1 + t2 : ", t1+t2)
print("t1 * 2 ; ", t1*2)
print("t2 * 3 ; ", t2*3)
#output
t1 : ('Techno Xpresss',)
t2 : (28, 80.72)
t1 + t2 : ('Techno Xpresss', 28, 80.72)
t1 * 2 ; ('Techno Xpresss', 'Techno Xpresss')
t2 * 3 ; (28, 80.72, 28, 80.72, 28, 80.72)
adding 2 Tuple, will cascade 2nd Tuple in 1st Tuple.
multiplying Tuple with any number n, resulted Tuple will have original Tuple data n times.
- Limitation:
- we can not add any data to Tuple via addition
#program
t1 = ("Techno Xpresss",20)
print("t1 : ", t1)
t2 = t1 + 80.72
#output
t1 : ('Techno Xpresss', 20)
Traceback (most recent call last):
File "C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 5, in <module>
t2 = t1 + 80.72
TypeError: can only concatenate tuple (not "float") to tuple
- we can not multiply 2 Tuple with each other
#program
t1 = ("Techno Xpresss",)
t2 = (28, 80.72)
print("t1 : ", t1)
print("t2 : ", t2)
print("t1 * t2", t1*t2)
#output
t1 : ('Techno Xpresss',)
t2 : (28, 80.72)
Traceback (most recent call last):
File "C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 7, in <module>
print("t1 * t2", t1*t2)
TypeError: can't multiply sequence by non-int of type 'tuple'
- Element values in Tuple can repeat
#program
t1 = ("Techno Xpresss",28, 80.72, "Techno Xpresss", 80.72)
print("t1 : ", t1)
#output
t1 : ('Techno Xpresss', 28, 80.72, 'Techno Xpresss', 80.72)
- Tuple are immutable, means we can modify members value
#program
t1 = ("Techno Xpresss",28, 80.72, "Techno Xpresss", 80.72)
print("t1 : ", t1)
t1[3] = "Er M S Dandyan"
print("t1 : ", t1)
#output
t1 : ('Techno Xpresss', 28, 80.72, 'Techno Xpresss', 80.72)
Traceback (most recent call last):
File "C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 5, in <module>
t1[3] = "Er M S Dandyan"
TypeError: 'tuple' object does not support item assignment
Tuple have few Tuple class Built in Methods
Comments
Post a Comment