How can elements be added to a list in Python?
To add elements to a Python list, you can use the append() method or the “+” operator.
Option 1: Utilizing the append() method.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
Output:
[1, 2, 3, 4]
Option 2: Utilize the “+” operator.
my_list = [1, 2, 3]
my_list = my_list + [4]
print(my_list)
Output:
[1, 2, 3, 4]
Please note that you can directly add elements to the end of a list using the append() method, while using the “+” operator requires enclosing both the list and the element to be added in square brackets before assigning it to the original list.