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

26 for tpl in frequency:
                   27     print(f'Count of {tpl[0]} in tuple is: {tpl[1]}')
                 Sample Output:
                  >>> Enter a tuple
                      (1,2,3,2,3,5)
                      Tuple: (1, 2, 3, 2, 3, 5)
                      Count of 1 in tuple is: 1
                      Count of 2 in tuple is: 2
                      Count of 3 in tuple is: 2
                      Count of 5 in tuple is: 1
                      Let's Summarise


                   Ø   A list comprises a sequence of objects enclosed in square brackets [].
                   Ø   A pair of square brackets [] denotes an empty list.

                   Ø   The len() function returns the number of objects in a list.
                   Ø   (+) operator is used to concatenates a pair of list.
                   Ø   (*) operator produces a list, concatenated with itself a specified number of times.
                   Ø   Traversing a list means accessing each element of a list. This can be  done by using looping statement, either
                       for or while.
                   Ø   List Methods: Methods are defined for list objects. These methods are as follows:
                         append(elem): inserts the object elem, passed as an argument, at the end of the list.
                          insert(index, elem): inserts the object elem, passed as an argument, at the specified index and
                         shifts the existing objects to the right.
                          extend(lst): inserts the elements in the list lst, passed as an argument, at the end of the elements

                         of the list lst.
                         index(elem): returns the index of the first occurrence of the element elem in the list.

                         reverse(): reverses the order of the elements in the list.
                         sort(): arranges the elements of the list in the ascending order.
                         remove(element): searches for the first instance of the element in the list lst and removes it.
                          pop(index): removes the element from the specified index and returns the element removed from
                         the list.
                   Ø   The operator  in  checks  for  the  membership  of  a  data  object  in  the  list  and  returns  True or  False,
                       depending on whether the specified object is included in the list or not.
                   Ø   The membership operator not in is used to check the membership of an element in a list. It returns True
                       if the specific element being looked for is not present in the list,  otherwise, False.
                   Ø   There are various other methods available like: lem(), mean(), max(), min(), sum().
                   Ø   A tuple is a sequence of elements, separated by commas and usually enclosed between parentheses().
                   Ø   An empty pair of parentheses defines an empty tuple.
                   Ø   A function tuple() is used to convert a sequence of elements to a tuple.

                   Ø   Tuple Methods: Python provides several methods for manipulating the tuples.
                         count(element): returns the number of times the given element appears in a tuple.
                         index(element): returns the index of the first occurrence of an element in a tuple.
                         sorted(t): sorts the tuple elements in ascending order by default.



                                                                                                     Lists and Tuples  393
   402   403   404   405   406   407   408   409   410   411   412