Lesson 10 – Lists

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.

What Does a List Look Like?

A list is written using square brackets [].

Example 1: Creating a List

fruits = ["apple", "banana", "mango"]

print(fruits)
print(type(fruits))
        

Output:

['apple', 'banana', 'mango']

        

Example 2: Accessing List Items

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
        

Example 3: Finding the Length of a List

fruits = ["apple", "banana", "mango"]
print(len(fruits))
        

Output:

3
        

Example 4: Adding Items to a List

You can add items using append().

fruits = ["apple", "banana", "mango"]
fruits.append("orange")

print(fruits)
        

Output:

['apple', 'banana', 'mango', 'orange']
        

Example 5: Looping Through a List

fruits = ["apple", "banana", "mango"]

for item in fruits:
    print(item)
        

Output:

apple
banana
mango
        

Quiz: Test Your Understanding

1. What symbol is used to create a list?

()
{}
[]
<>
Correct answer: []

2. What is the index of the first item in a list?

0
1
-1
10
Correct answer: 0

3. What does len() return for this list?

numbers = [4, 7, 9, 2]
2
3
4
7
Correct answer: 4

4. What does append() do?

Removes an item
Adds an item
Sorts the list
Reverses the list
Correct answer: Adds an item

5. What will this code print?

fruits = ["apple", "banana"]
fruits.append("mango")
print(fruits)
            
["apple"]
["banana", "mango"]
["apple", "banana", "mango"]
["mango"]
Correct answer: ["apple", "banana", "mango"]
Back to Top