How to add an integer to a list in Python
To add an integer to a list in Python, you can use the append()
method. Here is an example:
# Create a list of integers
numbers = [1, 2, 3]
# Add a new integer to the list
numbers.append(4)
# Print the updated list
print(numbers) # [1, 2, 3, 4]
The append()
method adds the new element to the end of the list. It takes the element to be added as its argument, and it modifies the list in-place. Therefore, you don't need to assign the result of the append()
method to a variable, unless you want to save the modified list for later use.