Introduction
Lists help us keep many items together in one place. They make it easy to store, find, and change groups of things.
Jump into concepts and practice - no test required
Lists help us keep many items together in one place. They make it easy to store, find, and change groups of things.
my_list = [item1, item2, item3] # Example: numbers = [1, 2, 3, 4, 5]
Lists use square brackets [] to hold items.
Items can be different types like numbers, words, or even other lists.
empty_list = []
print(empty_list)single_item_list = [42] print(single_item_list)
mixed_list = [1, 'apple', 3.14] print(mixed_list)
nested_list = [1, [2, 3], 4] print(nested_list)
This program shows how to create a list, add and remove items, and get an item by position.
fruits = ['apple', 'banana', 'cherry'] print('Original list:', fruits) # Add a fruit fruits.append('date') print('After adding date:', fruits) # Remove a fruit fruits.remove('banana') print('After removing banana:', fruits) # Access first fruit print('First fruit:', fruits[0])
Lists keep items in order, so you can get them by their position.
Adding or removing items is easy and fast at the end of the list.
Common mistake: forgetting that list positions start at 0, not 1.
Lists store many items together in one variable.
They keep the order of items and let you add, remove, or change them.
Lists are useful whenever you need to work with groups of things.
fruits = ['apple', 'banana', 'cherry'] print(fruits[1])
my_list = [1, 2, 3] my_list.add(4) print(my_list)