CodeBlanks Logo Icon CodeBlanks

Adding items

Add items with add_item.

Adding items (append)

In this lesson we'll introduce the append list method. append adds an item to a list:

cart = []
cart.append("Apple")
print(cart)  # ['Apple']

cart.append(...) places a new item at the end of the cart. Since the cart list is mutable, items can be added later. This is the most common use of lists.

Adding multiple items

To add several items to the cart, call append a few times:

cart = []
cart.append("Apple")
cart.append("Bread")
cart.append("Milk")
print(cart)  # ['Apple', 'Bread', 'Milk']

Each append call adds one more item to the list. The final cart holds all items.

Multiple append calls are the simplest way to fill the cart step by step.

Multiple append calls are the simplest way to fill the cart step by step.

Multiple append calls are the simplest way to fill the cart step by step.

Multiple append calls are the simplest way to fill the cart step by step.

Practice Add items and print

Define the empty cart with cart = []. Add cart.append("Apple") and cart.append("Bread"). Print print(cart).

Starter code

main.py
# Add items to the cart

Sample solution

main.py
cart = []
cart.append("Apple")
cart.append("Bread")
print(cart)

Expected output

['Apple', 'Bread']

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does cart.append("Bread") do?

  • Removes Bread from the cart
  • Shows Bread
  • Empties the cart
  • Adds Bread to the cart Correct answer

Explanation: append adds an item to the list.

2 Complete the append call

Question
cart = []
cart.____("Apple")
Answer
cart = []
cart.append("Apple")

Explanation: append adds an item to the list.

3 Match the list methods

Term Meaning
append Add an item
[] Empty list
cart Cart variable
print(cart) Show the cart

All lessons in Beginner