How to add elements to an array in Python?

In Python, you can add elements to an array (also known as a list) using the append() method.

Here is an example:

my_list = [1, 2, 3]  # 创建一个包含 1, 2, 3 的列表

my_list.append(4)  # 使用 append() 方法向列表中添加元素 4

print(my_list)  # 输出 [1, 2, 3, 4]

The append() method adds a new element to the end of the list.

bannerAds