Skip to main content

Detailed explanation of Tuple data structure in the Python Programming language

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

####   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

Comments

  1. In reversing a tuple you have used : twice and wrote -1in front of it why ?what does it mean.

    ReplyDelete
    Replies
    1. We 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

Post a Comment

Popular posts from this blog

Kotlin :A New Beginning

Hello guys, You might have heard about a new language supported for the android Officially by Google,Named as Kotlin. Kotlin is a statically typed Programming language.By statically typed language i mean that the type checking is done during complile time. It was developed in July 2011 by Jetbrains in Russia,In 2017 Google declared Kotlin as the official langauge for android. However the Traditional Java developers are sticking to java. New programmers who want to make android apps,Web apps can use Kotlin as a language of preference. With this Post i welcome you to the world of Kotlin. There is no good book for kotlin but there is the official documentation on Kotlin.I refer the official Kotlin documentation. The main aim of this post if to Introduce you Kotlin and its Uses and also how to write programs in Kotlin. The official documentation can be found on the website   KOTLIN I belive that this post will help you to write codes in Kotlin. So lets begin Fir...

Web programming part 2

Hello guys, I am back with another post on web programming. In the last post you guys had learnt how to create a simple HTML document. As i told you there are many tags that you might have to remember. In this Post i will be creating a small Login form using only HTML. By using only HTML i mean No styling will be given to the document. The purpose of this code is to demonstrate you tags like <form> and <label> you will also learn how to make a button in HTML. The code goes like this: < form  action ="/action_page.php" >      < label > < b > Username < /b > < /label >      < input  type ="text"  placeholder ="Enter Username" >      < label > < b > Password < /b > < /label >      < input  type ="password"  placeholder ="Enter Password" >      < button  type ="submit" ...

Using Java and Python simultaneously

Hi friends,                                                                                                                                    Firstly, I would like to thank you all for your love and support towards our blog.                   We here are happy to know that our previous posts on tutorials and mini projects have helped you a great deal.         So far many of you might have done some really amazing projects using a particular programming language. But in this post of mine, I would like to share with you my mini project for which I have collaborated two of the most am...