my_list = [1, 2, 3, 4, 5]
print(my_list[::-1])
Slice [::-1] reverses the list by using step -1 (backwards through entire list). It returns a reversed copy.
my_list = [1, 2, 3]
my_list.insert(1, 99)
print(my_list)
insert(1, 99) inserts 99 at index 1, shifting existing elements right. Result is [1, 99, 2, 3].
my_list = [1, 2, 3, 4, 5]
print(my_list[-2])
Negative indexing starts from the end: -1 is 5, -2 is 4, -3 is 3, etc. So -2 returns 4.
Both .copy() method and list() constructor create shallow copies. Simple assignment creates only a reference.
my_list = [1, 2, 3]
my_list_copy = my_list
my_list[0] = 99
print(my_list_copy[0])
Assignment creates a reference, not a copy. Both variables point to the same list object, so changes are reflected in both.
my_list = [3, 1, 4, 1, 5]
my_list.sort()
print(my_list)
sort() arranges list elements in ascending order by default. It modifies the list in-place.
my_list = [[1, 2], [3, 4], [5, 6]]
print(my_list[1][0])
my_list[1] returns [3, 4], and [3, 4][0] returns 3 (first element of the nested list).
my_list = [1, 2, 2, 3, 2, 4]
print(my_list.count(2))
count() returns the number of occurrences of a value. The value 2 appears 3 times in the list.
my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list)
extend() unpacks the iterable and adds individual elements. append() would add the list as a single element.
Lists (created with []) can be modified after creation, while tuples (created with ()) cannot be changed after initialization.