A list keeps related values together in order.
A variable can refer to one value. A list lets one variable refer to a collection of related values, kept in a defined order. This is useful when your program needs to work with several fruits, names, grades, or products instead of giving every value its own variable.
fruits = ["apple", "banana", "orange"]
print(fruits)Square brackets [] mark a list, and commas separate its items (also called elements). Here, fruits refers to one list containing three strings. Python keeps their order: apple comes before banana, and banana comes before orange.
['apple', 'banana', 'orange']Each item has an index: the position Python uses to find it. List indexes start at 0, so the first item is fruits[0], the second is fruits[1], and the third is fruits[2]. Think: fruits → 1 → "banana".
A useful picture: fruits → ["apple", "banana", "orange"]
indexes: 0 1 2. The first position is numbered 0; the index is not the item itself.
fruits = ["apple", "banana", "orange"]
print(fruits[0])
print(fruits[1])Read list_name[index] as: find the item at this position. fruits[0] prints "apple", and fruits[1] prints "banana". First learn to find an item; then you can change a specific one.
fruits = ["apple", "banana", "orange"]
fruits[1] = "pear"
print(fruits)fruits[1] names the second item, which was "banana". Assigning "pear" replaces that value at the same position, so the result is ["apple", "pear", "orange"]. Lists are mutable: their contents can change after the list is created; you did not create a new list just by changing one item.
fruits = ["apple", "banana", "orange"]
print(len(fruits))len(fruits) means “how many items are in this list?” and prints 3. A three-item list has indexes 0, 1, and 2: its length is 3, but its last valid index is 2. Length and last index are not the same thing.
fruits = ["apple", "banana"]
fruits.append("orange")
print(fruits)append() adds one item to the end of a list. Here the list changes from ["apple", "banana"] to ["apple", "banana", "orange"]. It is a simple first way to grow a list.
profile = ["Ana", 16, True]Python allows a list to hold values of different types, such as text, a number, and a Boolean value. In many practical examples, though, a list groups values with a similar meaning—for example names, grades, or products—so the list is easier to understand.
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)Lists and loops work naturally together. The list supplies the values, and for visits them one by one: fruit becomes "apple", then "banana", then "orange". Think: list → next item → loop variable → run block. This is like for number in range(3), except you now move directly through a collection of values.
colors = ["red", "blue", "green"]
print(colors[0])
colors[1] = "yellow"
colors.append("purple")
print(colors)
print(len(colors))Before running this code, predict four things: the first printed color, which item changes, where append() puts "purple", and the final length. The first print is "red"; index 1 changes blue to yellow; append adds purple at the end; and the final list has 4 items.
Common beginner mistakes
- Starting with index 1The first list item is at index
0, not index1. Index1names the second item. - Using an index that does not existFor
["apple", "banana", "orange"], valid indexes are0,1, and2. Asking forfruits[3]raises anIndexErrorbecause there is no fourth item. - Treating len() as the last indexIf a list has length
3, its final index is2.len()counts items; it does not name the final position.
