Adding a new element to a list #
We can add a new element to the end of a list using the append() function. It takes a single argument, which is the element that needs to be added to the list.
For example:
my_list = ['apple', 'banana', 'cherry']
my_list.append('orange')
print(my_list) # Output: ['apple', 'banana', 'cherry', 'orange']
Exercise #
- Use the function
append()any value to the variablemy_list
Tests #
- The length of
my_listshould have more than 4 elements - Use the function
append() - Do not change the first line of the code
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules