In Python, append() is a method of the list object, used to add an element to the end of the list. It should be noted that the append() method can only be used for list objects and cannot be used for other types of objects. In addition, the append() method will directly modify the original list without returning a new list.
In Python, append() is a method of the list object, used to add an element to the end of the list. Its usage is as follows:
Create a list object, for example:
my_list = [1, 2, 3]
Call the append() method and pass in the elements to be added as parameters, for example:
my_list.append(4)
In this way, element 4 will be added to the end of the list my_list.
You can call the append() method multiple times in succession to add multiple elements, for example:
my_list.append(5) my_list.append(6)
In this way, elements 5 and 6 will also be added to the end of my_list.
It should be noted that the append() method can only be used for list objects and cannot be used for other types of objects. In addition, the append() method will directly modify the original list without returning a new list.
The following is a complete sample code:
my_list = [1, 2, 3] my_list.append(4) my_list.append(5)print(my_list) # 输出结果为:[1, 2, 3, 4, 5]
The above is the detailed content of How to use append in python. For more information, please follow other related articles on the PHP Chinese website!