Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 121 - 2615_KEC
P. 121

    Output
                      Initial list: ['Pepper', 'Spot', 'Nao']
                      After append: ['Pepper', 'Spot', 'Nao', 'ASIMO']
                      After extend: ['Pepper', 'Spot', 'Nao', 'ASIMO', 'Atlas', 'Sophia']
                      After remove 'Nao': ['Pepper', 'Spot', 'ASIMO', 'Atlas', 'Sophia']
                      After pop: ['Pepper', 'Spot', 'ASIMO', 'Atlas']
                      Removed robot: Sophia
                      After insert at index 1: ['Pepper', 'Robonaut', 'Spot', 'ASIMO', 'Atlas']
                      Count of 'Pepper': 1
                      Index of 'Spot': 2
                      After sort: ['ASIMO', 'Atlas', 'Pepper', 'Robonaut', 'Spot']
                      After reverse: ['Spot', 'Robonaut', 'Pepper', 'Atlas', 'ASIMO']
                      After clear: []

                     Program 18: A program to demonstrate the use of various built-in list methods.

                         Program18.py

                      File  Edit  Format   Run    Options   Window    Help
                      # Starting with a list of numbers.
                      numbers = [3, 1, 4, 1, 5, 9, 2, 6]
                      print("Original list:", numbers)
                      # List's Functions
                      # use of len() fucntion
                      length = len(numbers)
                      print("Length of list (len):", length)

                      # use of list() fucntion
                      tuple_data = (10, 20, 30)
                      new_list = list(tuple_data)
                      print("Converted tuple to list (list):", new_list)

                      # use of max() fucntion
                      max_value = max(numbers)
                      print("Maximum value in list (max):", max_value)

                      # use of mix() fucntion
                      min_value = min(numbers)
                      print("Minimum value in list (min):", min_value)

                      # use of sum() fucntion
                      total_sum = sum(numbers)
                      print("Sum of list items (sum):", total_sum)

                      # use of sorted() fucntion
                      sorted_numbers = sorted(numbers)
                      print("Sorted list (sorted):", sorted_numbers)

                                                                                       Functions, String and List in Python  119

| 116 117 118 119 120 121 122 123 124 125 126 |