How to use lists in Python

Lists are one of the most versatile data types in Python. They are used to store an ordered collection of items, and can contain items of any data type, including other lists.

To create a list in Python, use square brackets to enclose the list items. Each item should be separated by a comma.

For example, to create a list of numbers from 1 to 10:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

To access individual elements in a list, use the index of the element. List indexes start at 0, so the first element in the list is at index 0.

For example, to access the first element in the list:

first_element = numbers[0]

To add items to a list, use the append() method.

For example, to add the number 11 to the end of the list:

numbers.append(11)

To remove items from a list, use the remove() method.

For example, to remove the number 5 from the list:

numbers.remove(5)

To iterate over the items in a list, use a for loop.

For example, to print out each number in the list:

for number in numbers:
print(number)

Lists are a powerful and useful data type in Python, and can be used for a variety of tasks.