BBampaa

Lessons › Python

Python lesson

.append()

After append, the list holds three items. Notice the dot: append belongs to the list it changes, so you write orders.append(...).

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Lists can grow. .append() adds one item to the end:

orders = [70, 15]
orders.append(24)
print(orders)
print(len(orders))

Output:

[70, 15, 24]
3

Key points

orders.append(24)Add 24 to the end of orders.
The dotappend is attached to the list it changes: list.append(item).
One at a timeappend adds one item. Call it again to add another.

Put together: start with an empty list, totals = [], then totals.append(70).

Practice

Try each one in your head first, then open the answer.

1. Build a line that adds 35 to the end of orders.
orders.append(35)
2. Build a line that adds Cable to the products list.
products.append("Cable")
3. Add 140 to the end of orders, then print orders.
orders = [70, 15, 24]
orders.append(140)
print(orders)
4. Start an empty list called names. Append Ada, then Tunde. Then print names.
names = []
names.append("Ada")
names.append("Tunde")
print(names)
5. Add 100 to sales, then print how many sales there are.
sales = [96, 120, 175]
sales.append(100)
print(len(sales))

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed