Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 122 - 2615_KEC
P. 122

    # use of reversed() fucntion
                       reversed_numbers = list(reversed(numbers))
                       print("Reversed list (reversed):", reversed_numbers)
                       # List's Methods
                       # use of append() method
                       numbers.append(7)
                       print("After appending 7 (append):", numbers)

                       # use of extend() method
                       numbers.extend([8, 9])
                       print("After extending with [8, 9] (extend):", numbers)

                       # use of insert() method
                       numbers.insert(0, 0)
                       print("After inserting 0 at index 0 (insert):", numbers)

                       # use of remove() method
                       numbers.remove(1)  # Removes the first occurrence of 1.
                       print("After removing first occurrence of 1 (remove):", numbers)

                       # use of pop() method
                       popped_item = numbers.pop(3)
                       print("Popped item at index 3 (pop):", popped_item)
                       print("List after pop(3):", numbers)

                       # use of clear() method
                       numbers.clear()
                       print("After clearing the list (clear):", numbers)

                          Output

                       Original list: [3, 1, 4, 1, 5, 9, 2, 6]
                       Length of list (len): 8
                       Converted tuple to list (list): [10, 20, 30]
                       Maximum value in list (max): 9
                       Minimum value in list (min): 1
                       Sum of list items (sum): 31
                       Sorted list (sorted): [1, 1, 2, 3, 4, 5, 6, 9]
                       Reversed list (reversed): [6, 2, 9, 5, 1, 4, 1, 3]
                       After appending 7 (append): [3, 1, 4, 1, 5, 9, 2, 6, 7]
                       After extending with [8, 9] (extend): [3, 1, 4, 1, 5, 9, 2, 6, 7, 8, 9]
                       After inserting 0 at index 0 (insert): [0, 3, 1, 4, 1, 5, 9, 2, 6, 7, 8, 9]
                       After removing first occurrence of 1 (remove): [0, 3, 4, 1, 5, 9, 2, 6, 7, 8, 9]
                       Popped item at index 3 (pop): 1
                       List after pop(3): [0, 3, 4, 5, 9, 2, 6, 7, 8, 9]
                       After clearing the list (clear): []

                      120   Premium Edition-VIII

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