How to append data to a list in Python?

To add data to a list in Python, you can use the append() method of the list. This method is used to add an element at the end of the list.

Here is an example:

my_list = []  # 创建一个空列表

# 使用 append() 方法将数据添加到列表中
my_list.append(1)
my_list.append(2)
my_list.append(3)

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

In the example above, we start by creating an empty list named my_list. Next, we use the append() method to add integers 1, 2, and 3 to the list individually. Finally, by printing the list, we can see that these data have been successfully added to the list.

bannerAds