Page 351 - Computer Science Class 11 With Functions
P. 351

What will be  the output produced on the execution of following code?
                     tuple1 = 30, 40
                     tuple2 = (30, 40)
                     print(tuple1 == tuple2)
                     print(tuple1[1])




            13.7.5 Nested Tuples
            As mentioned earlier, tuples may contain other compound objects, including lists, dictionaries, and other tuples. So, as
            a special case, tuples may be nested inside of other tuples.
            Example:

             >>> birthDates = ((8, 'August'), (1, 'October'), (30, 'September'))
            13.7.6 Elements of Tuple may Mutable
            We  have  seen  above  that  a  tuple  is  an  immutable  object.  However,  an  element  of  a  tuple  may  be  mutable.
            For example,

             >>> discipline = ('Science', ['Physics', 'Chemistry', 'Biology'])
             >>> discipline[1].append('Environment Science')
             >>> discipline
                 ('Science', ['Physics', 'Chmistry', 'Biology', 'Environment Science'])
                                         Table 13.2: Commonly Used Built-in Functions on Tuples

              S. No.          Function                Description                        Examples
             1.      len()                      It returns the number of  >>> len((1, 3, 'Mon', 'Wed'))
                                                elements in a tuple.            4
                                                                        >>> len(())
                                                                                0
             2.      max(tpl)                   It returns the largest  >>> tpl=(3, -2, 0, 78, 25)
                                                argument  element from  >>> max(tpl)
                                                the tuple: tpl.                 78
             3.      min(tpl)                   It  returns the smallest  >>> tpl=(3, -2, 0, 78, 25)
                                                element   from  the  >>> min(tpl)
                                                tuple: tpl.                     -2
             4.      sum(tpl [,num])            It returns the sum of all  >>> tpl =(1, 3, 5, 7)
                                                values in the tuple: tpl.  >>> sum(tpl)
                                                The  optional  argument          16
                                                num, when provided, is  >>> sum(tpl, 5)
                                                added to the sum of the           21
                                                elements of the tuple.

             5.      mean(tpl)                  It returns  the mean of  >>> from statistics import mean
                                                the elements of a tuple.  >>> tpl = (4, 2, 5, 9)

                                                                        >>> mean(tpl)
                                                                                5





                                                                                                Lists and Tuples  349
   346   347   348   349   350   351   352   353   354   355   356