📌  相关文章
📜  my_list = ['a','b'] my_list.append('c') print(my_list) - Python (1)

📅  最后修改于: 2023-12-03 15:03:04.351000             🧑  作者: Mango

Python List - Adding an Element

In Python, lists are one of the most frequently used data types. They are mutable, which means elements can be added, removed or modified after the list has been created.

One way to add an element to a list is by using the 'append()' method. This method adds an element to the end of the list.

my_list = ['a', 'b']
my_list.append('c') 
print(my_list)  # ['a', 'b', 'c']

In the above code snippet, 'my_list' is a list with two elements - 'a' and 'b'. The 'append()' method is then used to add the string element 'c' to the end of the list. The output of the print statement shows that 'c' has been successfully added to the list.

The 'append()' method is quite useful when you want to add elements to a list one at a time. However, if you want to add multiple elements to a list at once, you can use the 'extend()' method.

We hope this introduction to adding elements to a Python list has been helpful!