Advertisement



< Prev
Next >



Python - Tuple





Today we are going to understand one of Python's most important collection data types, tuple. A tuple is an ordered sequence of items. The items of a tuple are arranged in the order in which they are declared in a tuple, where the first item is stored at the index zero, the second item at the index one and so on.





We could even create a new tuple by concatenating different tuples by using the arithmetic + operator. The arithmetic + operator is overloaded to add integers, floating values, concatenate string values and concatenate tuples.

#Python - Concatenating tuples using the arithmetic + operator


#First tuple
tup1 =('A', -20)

#Second tuple
tup2 =('have a good day', 2019)

#Printing tuples
print('Tuple1 : ', tup1)
print('Tuple2 : ', tup2)

#Creating a new tuple by concatenating existing tuples
tup3 = tup1 + tup2

#Printing new tuple
print('Tuple3 : ',tup3)

Output


Tuple1 :  ('A', -20)
Tuple2 :  ('have a good day', 2019)
Tuple3 :  ('A', -20, 'have a good day', 2019)

In the program just above, we have created two tuples and have then created a third one by concatenating the first two tuples by using the arithmetic + operator.




  • Some important built-in functions to work with tuple


  • Functions Description
    max() Returns the maximum value in a tuple.
    min() Returns the minimum value in a tuple.
    len() Returs the total length of a tuple.
    sum() Returns sum of all elements in a tuple.
    sorted() Returns a sorted copy of a tuple.
    any() Returns true if any element in a tuple is true, else false.
    all() Returns true if all elements in a tuple are true, else false.

    #Python - Functions working with tuples 
    
    
    #First tuple
    tup1 =(19, -20, 209, 799.99, -15.4)
    
    #Second tuple
    tup2 =(1, True, False, 0) # 1 is boolean true, 0  for boolean false
                              # True(with capital T) is boolean true, False(with capital F) is boolean false
    
    #Printing tuples
    print('Tuple1 : ', tup1)
    print('Tuple2 : ', tup2)
    
    
    
    #Using max() function with a tuple.
    print('Maximum value in Tuple1 : ', max(tup1))
    
    
    #Using min() function with a tuple.
    print('Minimum value in Tuple1 : ', min(tup1))
    
    
    #Using sum() function with a tuple.
    print('Sum of values in Tuple1 : ', sum(tup1))
    
    
    #Using sorted() function with a tuple.
    print('Sorted copy of Tuple1 : ', sorted(tup1))
    
    
    #Using all() function with a tuple.
    print('all() boolean true values in Tuple2 : ', all(tup2))
    
    
    #Using any() function with a tuple.
    print('any boolean true value in Tuple2 : ', any(tup2))


    Output


    Tuple1 :  (19, -20, 209, 799.99, -15.4)
    Tuple2 :  (1, True, False, 0)
    Maximum value in Tuple1 :  799.99
    Minimum value in Tuple1 :  -20
    Sum of values in Tuple1 :  992.59
    Sorted copy of Tuple1 :  [-20, -15.4, 19, 209, 799.99]
    all() boolean true values in Tuple2 :  False
    any boolean true value in Tuple2 :  True





    Please share this article -




    < Prev
    Next >
    < Python global variables
    Python List >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement