0% found this document useful (0 votes)
3 views2 pages

Built Methods

The document demonstrates various built-in list methods in Python using a sample list. It covers methods such as append, insert, remove, pop, sort, reverse, index, count, copy, and clear, showcasing their effects on the list. Each method is illustrated with print statements to show the state of the list after each operation.

Uploaded by

arethinapriya95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Built Methods

The document demonstrates various built-in list methods in Python using a sample list. It covers methods such as append, insert, remove, pop, sort, reverse, index, count, copy, and clear, showcasing their effects on the list. Each method is illustrated with print statements to show the state of the list after each operation.

Uploaded by

arethinapriya95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Demonstrating Built-in List Methods in Python

def list_methods_demo():

my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]

print("Original List:", my_list)

# Append - Adds an element to the end

my_list.append(7)

print("After append(7):", my_list)

# Insert - Inserts an element at a specific index

my_list.insert(2, 8)

print("After insert(2, 8):", my_list)

# Remove - Removes the first occurrence of a value

my_list.remove(1)

print("After remove(1):", my_list)

# Pop - Removes and returns the last element

popped_element = my_list.pop()

print("After pop():", my_list, "(Popped Element:", popped_element, ")")

# Sort - Sorts the list in ascending order

my_list.sort()

print("After sort():", my_list)


# Reverse - Reverses the list

my_list.reverse()

print("After reverse():", my_list)

# Index - Finds the first occurrence of a value

index_of_five = my_list.index(5)

print("Index of 5:", index_of_five)

# Count - Counts occurrences of a value

count_of_five = my_list.count(5)

print("Count of 5:", count_of_five)

# Copy - Creates a shallow copy of the list

copied_list = my_list.copy()

print("Copied List:", copied_list)

# Clear - Removes all elements from the list

my_list.clear()

print("After clear():", my_list)

# Run the demonstration

list_methods_demo()

You might also like