Page 106 - tp_Modula_v2.0
P. 106

PYTHON FUNCTIONS

                  Python provides various built-in functions. The following table describes these functions.

                                      Function                                 Explanation
                          sorted( )                          Sorts the elements

                          len( )                             Returns the length
                          any( )                             Returns true if any of the dictionary item is true

                          all( )                             Returns true if all the dictionary items are true

                  Program 4: To perform various built-in dictionary functions.
                      Program4.py
                   File  Edit  Format   Run   Options  Window    Help
                   list = [13, 25, 41, 63, 82, 19, 26, 43, 55]

                   print("Length of the list:", len(list))
                   print("Largest element in the list:", max(list))

                   print("Smallest element in the list:", min(list))



                  On running the above program, you will get the following output:

                      Output

                   Length of the list: 9
                   Largest element in the list: 82
                   Smallest element in the list: 13



                      UPDATING A DICTIONARY

                  You can easily add or modify the elements of a dictionary. If the item is present in the dictionary,
                  then you can change its value by providing new value to its key term.
                  Program 5: To update a dictionary.
                      Program5.py
                   File  Edit  Format   Run   Options  Window    Help
                   dict1={'Name': 'A', 'Rollno': 1}
                   print(dict1)
                   dict1['Rollno']=2
                   dict1['Marks']=[15, 47, 54]
                   print(dict1)



                  On running the above program, you will get the following output:
                      Output

                   {'Name': 'A', 'Rollno': 1}
                   {'Name': 'A', 'Rollno': 2, 'Marks': [15, 47, 54]}




                  104     Touchpad MODULAR (Version 2.0)
   101   102   103   104   105   106   107   108   109   110   111