Skip to content

Commit 01da55b

Browse files
Update list_operations_demo.py
1 parent b1e9e33 commit 01da55b

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

list_operations_demo.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
11
# list_operations_demo.py
2-
# This script demonstrates basic list operations in Python such as append, insert, remove, reverse, extend, sort, and pop.
2+
# A demonstration of Python list methods including append, insert, remove, slicing, extend, sort, pop, membership check, and clear.
33

4-
# Initial list
4+
# Create an initial list
55
List = [10, 20, 30, 40, 50]
6-
print(List) # Output the original list
6+
print(List) # Output: [10, 20, 30, 40, 50]
77

8-
# Append 60 to the end of the list
8+
# Append 60 to the list
99
List.append(60)
10-
print(List) # List becomes: [10, 20, 30, 40, 50, 60]
10+
print(List) # Output: [10, 20, 30, 40, 50, 60]
1111

1212
# Print the length of the list
1313
print(len(List)) # Output: 6
1414

1515
# Insert 15 at index 2
1616
List.insert(2, 15)
17-
print(List) # List becomes: [10, 20, 15, 30, 40, 50, 60]
17+
print(List) # Output: [10, 20, 15, 30, 40, 50, 60]
1818

19-
# Remove the first occurrence of 15
19+
# Remove the value 15
2020
List.remove(15)
21-
print(List) # List becomes: [10, 20, 30, 40, 50, 60]
21+
print(List) # Output: [10, 20, 30, 40, 50, 60]
2222

23-
# Create a reversed copy of the list using slicing
23+
# Create a reversed version of the list
2424
list_reverse = List[::-1]
2525
print(list_reverse) # Output: [60, 50, 40, 30, 20, 10]
2626

27-
# Find the index of the first occurrence of 40
27+
# Find the index of value 40
2828
print(List.index(40)) # Output: 3
2929

30-
# Create a new list and extend the original list with it
30+
# Extend the list with another list
3131
new_List = [20, 30, 70, 60]
3232
List.extend(new_List)
33-
print(sorted(List)) # Print a sorted version of the list (does not modify original)
33+
print(sorted(List)) # Output: [10, 20, 20, 30, 30, 40, 50, 60, 60, 70]
3434

3535
# Remove the first occurrence of 20
3636
List.remove(20)
37-
print(List) # 20 is removed from the first match only
37+
print(List) # Output: [10, 30, 40, 50, 60, 20, 30, 70, 60]
3838

39-
# Remove the last element from the list using pop
39+
# Remove the last item using pop()
4040
List.pop()
41-
print(List) # Last element (60) is removed
41+
print(List) # Output: [10, 30, 40, 50, 60, 20, 30, 70]
42+
43+
# Check if 20 is still in the list
44+
if 20 in List:
45+
print("Yes") # Output: Yes
46+
47+
# Clear all elements from the list
48+
List.clear()
49+
print(List) # Output: []

0 commit comments

Comments
 (0)