Page 401 - Computer Science V2.0 Class 11
P. 401

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.3: 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
                                                     elements of the tuple.          21
                    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
                 13.8 Tuple Operations

                 Likewise, strings and lists, Python also supports a variety of operations on tuples. Below we illustrate some important
                 operations on tuples.
                  >>> greetings = ('Good Morning', 'Good Afternoon')
                  >>> marks = (78, 99, 34, 45)
                  >>> dateOfBirth = (1, 'October', 1990)
                 ●  Concatenation Operator +: The concatenation operator concatenates and yields a tuple comprising the elements
                   of the first tuple, followed by the elements of the second tuple.
                  >>> dayGreetings = greetings + ('Good Night',)
                  >>> dayGreetings
                      ('Good Morning', 'Good Afternoon', 'Good Night')
                                                                                                     Lists and Tuples  387
   396   397   398   399   400   401   402   403   404   405   406