Hi guys,
I am
posting a detailed explanation of Tuple data structure in the Python
Programming language.
I
have explained the code with detailed comments. I hope the explanation is sufficient.
Please comment or contact us in case of any inconsistencies.
Hope you like it.
Thank you very much. :)
#CODE BEGINS
#tuples
tup1 = ('Hi',3.1416,125,[1,2,3])
tup2 = (1,2,3,4,5,6,7,8,9,10)
tup3 = ('a','b','c','d','f')
tup4 = (1,2,3,4,5,6,7,8,9,10)
mini = ('i')
l = ['l','m','n']
#Displays the entire tuple
print tup2,"\n"
#display selected elements
print "tup2[3] = ",tup2[3],"\n"
#Slicing
print "tup2[2:] = ",tup2[2:],"\n"
#Reversing a tuple
print tup3[::-1]
print "##########################################################"
########################## TUPLE FUNCTIONS ################################33
#length of tuple
print "The length of tup1 = ",len(tup1),"\n"
#Comparision between two tuples
#Returns 1 if tup1 > tup2
#Returns 0 if tup1 == tup2
#Returns -1 if tup1 < tup2
print "The comaparision of tup2 and tup4 = ",cmp(tup2,tup4),"\n"
print "The comaparision of tup3 and tup4 = ",cmp(tup3,tup4),"\n"
print "The comaparision of tup4 and tup3 = ",cmp(tup4,tup3),"\n"
#MAX of two tuples
print "The maximum element in tup2 = ",max(tup2)
print "The maximum element in tup3 = ",max(tup3)
#MIN of two tuples
print "The minimum element in tup2 = ",min(tup2)
print "The miniimum element in tup3 = ",min(tup3)
#LIST to TUPLE conversion
tup5 = tuple(l)
print "\nThe converted tupple is: ",tup5,"\n"
#TUPLE to LIST conversion
l = list(tup5)
print "\nThe converted list is: ",l,"\n"
print "##########################################################"
################# TUPLE OPERATIONS ##########################
#TUPLE ADDITION
#METHOD 1 ----> ADDING TUPLES
tup6 = tup1 + tup2
print "The newly created tuple is:\t",tup6
#Method 2 -----> ADDING TUPLE VALUES
tup6 = (1,2,3) + (['a','b'],2.66,'l','m','n')
print "The newly created tuple is:\t",tup6
#REPEATING VALUES
r = int(input("How many times do you want to repeat?\n"))
print "\nThe repeated vales is:\t",mini*r
#Tuple Checking
print "To check if 2 is present in tup4:\t",2 in tup4
#Looping a tuple
for t in tup6:
print t
tup1 = ('Hi',3.1416,125,[1,2,3])
tup2 = (1,2,3,4,5,6,7,8,9,10)
tup3 = ('a','b','c','d','f')
tup4 = (1,2,3,4,5,6,7,8,9,10)
mini = ('i')
l = ['l','m','n']
#Displays the entire tuple
print tup2,"\n"
#display selected elements
print "tup2[3] = ",tup2[3],"\n"
#Slicing
print "tup2[2:] = ",tup2[2:],"\n"
#Reversing a tuple
print tup3[::-1]
print "##########################################################"
########################## TUPLE FUNCTIONS ################################33
#length of tuple
print "The length of tup1 = ",len(tup1),"\n"
#Comparision between two tuples
#Returns 1 if tup1 > tup2
#Returns 0 if tup1 == tup2
#Returns -1 if tup1 < tup2
print "The comaparision of tup2 and tup4 = ",cmp(tup2,tup4),"\n"
print "The comaparision of tup3 and tup4 = ",cmp(tup3,tup4),"\n"
print "The comaparision of tup4 and tup3 = ",cmp(tup4,tup3),"\n"
#MAX of two tuples
print "The maximum element in tup2 = ",max(tup2)
print "The maximum element in tup3 = ",max(tup3)
#MIN of two tuples
print "The minimum element in tup2 = ",min(tup2)
print "The miniimum element in tup3 = ",min(tup3)
#LIST to TUPLE conversion
tup5 = tuple(l)
print "\nThe converted tupple is: ",tup5,"\n"
#TUPLE to LIST conversion
l = list(tup5)
print "\nThe converted list is: ",l,"\n"
print "##########################################################"
################# TUPLE OPERATIONS ##########################
#TUPLE ADDITION
#METHOD 1 ----> ADDING TUPLES
tup6 = tup1 + tup2
print "The newly created tuple is:\t",tup6
#Method 2 -----> ADDING TUPLE VALUES
tup6 = (1,2,3) + (['a','b'],2.66,'l','m','n')
print "The newly created tuple is:\t",tup6
#REPEATING VALUES
r = int(input("How many times do you want to repeat?\n"))
print "\nThe repeated vales is:\t",mini*r
#Tuple Checking
print "To check if 2 is present in tup4:\t",2 in tup4
#Looping a tuple
for t in tup6:
print t
#### CODE
ENDS ##############
The output of the above code would be:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tup2[3] = 4
tup2[2:] =
(3, 4, 5, 6, 7, 8, 9, 10)
('f', 'd', 'c', 'b', 'a')
##########################################################
The length of tup1 =
4
The comaparision of tup2 and tup4 = 0
The comaparision of tup3 and tup4 = 1
The comaparision of tup4 and tup3 = -1
The maximum element in tup2 = 10
The maximum element in tup3 = f
The minimum element in tup2 = 1
The minimum element in tup3 = a
The converted tupple is: ('l', 'm', 'n')
The converted list is: ['l', 'm', 'n']
##########################################################
The newly created tuple is: ('Hi', 3.1416, 125, [1, 2, 3], 1, 2, 3, 4, 5, 6, 7, 8, 9,
10)
The newly created tuple is: (1, 2, 3, ['a', 'b'], 2.66, 'l', 'm', 'n')
How many times do you want to repeat?
5
The repeated vales is: iiiii
To check if 2 is present in tup4: True
1
2
3
['a', 'b']
2.66
l
m
n
What is tuple in simple words .
ReplyDeleteIn reversing a tuple you have used : twice and wrote -1in front of it why ?what does it mean.
ReplyDeleteWe shall be posting more detailed and from the scratch tutorials in python and web programming soon. We kindly request you to wait until then. We appreciate your patience. Thank you.
Delete