Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 159 - CodePilot V5.0 C8
P. 159

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

                         Program19.py
                      File  Edit  Format   Run   Options   Window     Help

                      robot_list = ["Pepper", "Spot", "Nao"]
                      print("Initial list:", robot_list)

                      robot_list.append("ASIMO")
                      print("After append:", robot_list)
                      robot_list.extend(["Atlas", "Sophia"])
                      print("After extend:", robot_list)

                      robot_list.remove("Nao")
                      print("After remove 'Nao':", robot_list)
                      last_robot = robot_list.pop()
                      print("After pop:", robot_list)
                      print("Removed robot:", last_robot)
                      robot_list.insert(1, "Robonaut")
                      print("After insert at index 1:", robot_list)
                      print("Count of 'Pepper':", robot_list.count("Pepper"))
                      print("Index of 'Spot':", robot_list.index("Spot"))

                      robot_list.sort()
                      print("After sort:", robot_list)
                      robot_list.reverse()
                      print("After reverse:", robot_list)

                      robot_list.clear()
                      print("After clear:", robot_list)

                     The output of the preceding code is as follows:
                         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: []

                                                                                                                      157
                                                                                                Step Ahead with Python

| 154 155 156 157 158 159 160 161 162 163 164 |