Computer Science Researcher • Educator • Programming Instructor
A list is a collection that allows you to store multiple items in a single variable. Lists are ordered, changeable, and allow duplicate values. They are one of the most commonly used data structures in Python.
A list is written using square brackets [].
fruits = ["apple", "banana", "mango"]
print(fruits)
print(type(fruits))
Output:
['apple', 'banana', 'mango']
You access list items using their index number (starting from 0).
fruits = ["apple", "banana", "mango"]
print(fruits[0]) # first item
print(fruits[2]) # third item
Output:
apple
mango
fruits = ["apple", "banana", "mango"]
print(len(fruits))
Output:
3
You can add items using append().
fruits = ["apple", "banana", "mango"]
fruits.append("orange")
print(fruits)
Output:
['apple', 'banana', 'mango', 'orange']
fruits = ["apple", "banana", "mango"]
for item in fruits:
print(item)
Output:
apple
banana
mango
1. What symbol is used to create a list?
()2. What is the index of the first item in a list?
03. What does len() return for this list?
numbers = [4, 7, 9, 2]2
4. What does append() do?
Removes an item5. What will this code print?
fruits = ["apple", "banana"]
fruits.append("mango")
print(fruits)
["apple"]