温馨提示×

python定义空数组怎么添加数据

小亿
106
2024-01-02 20:53:19
栏目: 编程语言

在Python中,我们可以使用列表(list)来定义一个空数组,并通过append()方法来向数组中添加数据。

以下是一个示例代码:

# 定义一个空数组
arr = []

# 添加数据到数组中
arr.append(1)
arr.append(2)
arr.append(3)

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

在上述代码中,我们首先定义了一个空数组arr,然后使用append()方法向数组中添加数据。每次调用append()方法时,都会在数组的末尾添加一个元素。最后,我们使用print()函数来输出数组的内容。

0