2.6.1.4 Methods and Operations on Lists
Python Programming
Python provides several built-in methods to work with lists. Here are a few useful ones:
- append (item) - Adds an item to the end of the list.
- remove (item) - Removes the first occurrence of an item from the list.
- sort () - Sorts the list in ascending order.
- reverse () - Reverses the order of the list.
Example: Add a new student to the list of students and then sort the list.
python
students = ["Ahmed", "Sara", "Ali"]
students.append("Hina")
students.sort ()
print(students)
# Output : [’Ahmed’, ’Ali’, ’Hina’, ’Sara’]Explanation: The code creates a list of students, adds 'Hina' to it, sorts the list alphabetically.