本文共 1583 字,大约阅读时间需要 5 分钟。
append() is an inbuilt method in Python, which is used to add an element/item to the list. append() adds the element at the end.
append()是Python中的一种内置方法,用于将元素/项目添加到列表中。 append()在最后添加元素。
Syntax:
句法:
list.append(element)
Here,
这里,
list is the name of the list.
list是列表的名称。
element is an item/element to be appended.
元素是要附加的项目/元素。
Return type: Method append() does not return any value, it just modifies the existing list.
返回类型:方法append()不返回任何值,它仅修改现有列表。
Example:
例:
# Python program to demonstrate# an example of list.append() method # listcities = ['New Delhi', 'Mumbai']# print the listprint "cities are: ",cities# append two more citiescities.append('Bangalore');cities.append('Chennai');# print the updated listprint "cities are:",cities
Output
输出量
cities are: ['New Delhi', 'Mumbai'] cities are: ['New Delhi', 'Mumbai', 'Bangalore', 'Chennai']
Explanation:
说明:
Initially there were two elements in the list ['New Delhi', 'Mumbai']
最初,列表中有两个元素['New Delhi','Mumbai']
Then, we added two more city names (two more elements) - 1) 'Bangalore' and 2) 'Chennai', by using following python statements:
然后,我们使用以下python语句添加了另外两个城市名称(另外两个元素)-1) 'Bangalore'和2) 'Chennai' :
cities.append('Bangalore'); cities.append('Chennai');
'Bangalore' and the 'Chennai' will be added at the end of the list named cities.
“班加罗尔”和“钦奈”将添加到名为城市的列表的末尾。
Then, we printed the updated list; the output will be a new list with 4 elements ['New Delhi', 'Mumbai', 'Bangalore', 'Chennai'].
然后,我们打印了更新的列表; 输出将是一个包含4个元素的新列表['New Delhi','Mumbai','Bangalore','Chennai'] 。
翻译自:
转载地址:http://wivzd.baihongyu.com/