Learn it
A list holds many values in order: snacks = ["apple", "popcorn"]. Use append() to add, remove() to remove, len() to count, and a for loop to go through them.
🧰 Tool: any free online Python editor such as Online Python or Trinket, or install Python from python.org and use IDLE.
Code examples
Python
snacks = ["apple", "popcorn", "grapes"]
snacks.append("cheese")
snacks.remove("popcorn")
print(snacks) # ['apple', 'grapes', 'cheese']
print(len(snacks)) # 3
print(snacks[0]) # apple
print(snacks[-1]) # cheese (last item)
for s in snacks:
print("I like", s)
Project: Shopping list app
- Start with an empty list: items = [].
- Use a while loop to ask for items until the user types ‘done’.
- Print the list numbered from 1.
Challenge: Sort the list alphabetically with items.sort().