-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.py
More file actions
58 lines (44 loc) · 1.21 KB
/
lists.py
File metadata and controls
58 lines (44 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Creating a list
my_list = [1, 2, 3, 4, 5]
# Accessing elements
print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: 5
# Modifying elements
my_list[2] = 10
print(my_list) # Output: [1, 2, 10, 4, 5]
# Appending elements
my_list.append(6)
print(my_list) # Output: [1, 2, 10, 4, 5, 6]
# Removing elements
my_list.remove(2)
print(my_list) # Output: [1, 10, 4, 5, 6]
# Slicing
print(my_list[1:4]) # Output: [10, 4, 5]
# Length of the list
print(len(my_list)) # Output: 5
# Sorting the list
my_list.sort()
print(my_list) # Output: [1, 4, 5, 6, 10]
# Reversing the list
my_list.reverse()
print(my_list) # Output: [10, 6, 5, 4, 1]
# Inserting elements at a specific index
my_list.insert(2, 3)
print(my_list) # Output: [10, 6, 3, 5, 4, 1]
# Counting the occurrences of an element
count = my_list.count(5)
print(count) # Output: 1
# Clearing the list
my_list.clear()
print(my_list) # Output: []
# Copying the list
new_list = my_list.copy()
print(new_list) # Output: []
# Extending the list with another list
my_list.extend([7, 8, 9])
print(my_list) # Output: [7, 8, 9]
# Checking if an element exists in the list
if 7 in my_list:
print("7 exists in the list")
else:
print("7 does not exist in the list")