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

Intro to Python Programming - 2. Basic Output

Hello everyone!           Considering the lot of requests I received from my dear friends and viewers, I shall now start posting step by step tutorials for mastering Python programming. I proceed with the assumption that you have a basic foundation of the C programming language . If not much of it, at least having some idea about the type specifiers such as ‘%d for integer, %f for floating point variables etc.’ and also, some basic ideas about data types such as int , float , char etc. would be of great help. :)           Before we begin with the coding part of python, I want you to understand a few basic concepts, like: 1.      Python is scripting language. 2.      Python programs do not make use of statement terminators like semicolons ‘;’ as in C, C++ and Java. 3.      A basic Python program does not require any header ...

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

Assembly code generator for non parenthesized arithmetic expressions using Python Programming Language.

Hi guys, I hope you like the code I'm posting. This program is written in Python Programming language and it converts the non parenthesized arithmetic expressions to Assembly level code. Try a = b + c / d to test the code. And this should be the expected output: The Genereated code is: LD R0 ,c LD R1 ,d DIV R0 ,R0 ,R1 LD R1 ,b LD R2 ,temp ADD R1 ,R1 ,R2 ST temp , R1 Please Note: Do include a space (" ") between the operators and operands. And keep it in your mind that this is a code generator. Hence please do try it with valid expressions only. Upgrading the code for implementing the 'if' and looping constructs like 'while' and 'for' and sharing the modified code with us and other coding enthusiasts is really very well appreciated. I shall make another post about the explanation of the code soon. Until then, please do comment on our post and share our link to other coding lovers like yourself! I ho...