How to add numbers to a list in Python?

You can add numbers to a list by using the append() method of the list, which is used to add an element at the end of the list.

Here is an example code:

numbers = [1, 2, 3, 4]
new_number = 5

numbers.append(new_number)

print(numbers)  # 输出: [1, 2, 3, 4, 5]

In the above code, we created a list called ‘numbers’ with some numbers. Next, we defined a new number called ‘new_number’. We used the append() method to add ‘new_number’ to the end of the ‘numbers’ list. Finally, we printed out the content of the list, showing that the new number had been successfully added.

bannerAds