Tuple
Tuple in python is used to store sequential multiple objects. All the objects are place within parenthesis [ ( ) ]. Tuple is similar to list but difference is that the list is mutable (changeable) and the tuple is immutable (unchanged).
Syntax:
For example if you want to store the ID number of 5 employees into Tuple as follows:
emp=(101,102,103,104,105)
Unlike other programming language Python provide negative indexes in tuple. The index start form right side. The right most index is -1 then -2 and so on.
for i in tuple1:
print(i)
20
30
40
50
60
70
>>>
tuple1=(10,20,30,40,50,60,70)
for i in tuple1:
del tuple1[1]
print(i)
Traceback (most recent call last):
File "G:\Python\tuple1.py", line 3, in <module>
del tuple1[1]
TypeError: 'tuple' object doesn't support item deletion
>>>
On the above example the output came error, because tuple does not support any del function and does not modified.
Syntax:
T1= (1,”Raj”, 2) T2= (“red”, “green”, “blue”) |
Indexing Process in Python:
The indexing process in tuple is similar to list. The objects in the tuple are store as a index wise automatically which is start from '0' and goes to length -1. The first object of tuple store into '0'th index and second object is '1' st index and so on.For example if you want to store the ID number of 5 employees into Tuple as follows:
emp=(101,102,103,104,105)
Figure no. 1.1 |
Example 1:
tuple1=(10,20,30,40,50,60,70)for i in tuple1:
print(i)
Output:
1020
30
40
50
60
70
>>>
Example 2:
tuple1=(10,20,30,40,50,60,70)
for i in tuple1:
del tuple1[1]
print(i)
Output:
Traceback (most recent call last):
File "G:\Python\tuple1.py", line 3, in <module>
del tuple1[1]
TypeError: 'tuple' object doesn't support item deletion
>>>
On the above example the output came error, because tuple does not support any del function and does not modified.
Example 3: Merging tow tuple:-
t1=(1,2,3,4)
t2=(5,6,7,8)
print("Frist tuple")
for a in t1:
print(a)
print("Second tuple")
for b in t2:
print(b)
print("After marging")
for c in (t1+t2):
print(c)
Output:
Frist tuple
1
2
3
4
Second tuple
5
6
7
8
After marging
1
2
3
4
5
6
7
8
>>>
Thank you.
ReplyDeleteThanks for Sharing This Article. It was a valuable content. I hope these Commenting lists will help to my website.
ReplyDeleteAmazon web services Training in chennai
Amazon web services Course in chennai