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

●  Multiplication Operator *: The multiplication operator concatenates the string the specified number of times.
               >>> greetings * 2
                    ('Good Morning', 'Good Afternoon', 'Good Morning', 'Good Afternoon')

               >>> greetings * 0
                    ()
              ●  Membership operator in:

               >>> 'Good Afternoon' in greetings # Membership operation in
                    True

               Program 13.4  Define  a  function  wordLength(lst)  which  takes  a  list  of  words  and  returns  a  list  of  tuples
               where each tuple comprises a word and its corresponding length. Make use of the function wordLength() to
               write a program that accepts a list of words from a user and prints a list of tuples where each tuple is word-length
               pair, that is, word and its corresponding length.
                01 def wordLength(lst):
                02     '''
                03     Purpose: To return word-length pairs for the words in a list.
                04     Input Parameter: lst - list containing words
                05     Return Value: List of (word, length) tuples
                06     '''
                07     '''
                08     Approach:
                09        initialise an empty word-length list wordLenPairs
                10        for each word in lst:
                11          append the tuple (word, length) to wordLenPairs
                12     '''
                13     wordLenPairs = list()
                14     for word in lst:
                15         wordLenPairs.append((word, len(word)))
                16     return wordLenPairs
                17
                18
                19 #Objective: Accept a word-list from a user and print list of (word.lenngth)tuples
                20
                21 lst = eval(input('Enter the list: '))
                22 print('List of tuples:: ', wordLength(lst))
              Sample Output:

               >>> Enter the list: ['ORANGE','EDUCATION']
                    List of tuples::  [('ORANGE', 6), ('EDUCATION', 9)]
              13.9 Tuple Methods

              Python provides several methods for manipulating the tuples.  Like a string, a tuple is also an immutable object, All
              the methods that operate on a tuple leave the original tuple unchanged. To invoke a method associated with a tuple
              object, the tuple object is followed by a dot, followed by the method's name, followed by a pair of parentheses that
              enclose the arguments (if any) required for invoking the method. Next, we describe the functionality of some methods

              for the tuple objects on a tuple object, say, tpl.
              1.  tpl.count(value): The method returns the number of occurrences of  the given value in a tuple.

                  Examples:
               >>> (0, 1, 2, 3, 2, 3, 1, 3, 2).count(2)
                    3
               >>> age = (20, 18, 17, 19, 18, 18)
               >>> age.count(18)
                    2

               388   Touchpad Computer Science (Ver. 2.0)-XI
   397   398   399   400   401   402   403   404   405   406   407