BeginnerCoder

Lesson 1 — Stack

Stack: last in, first out

A data structure is a way of organising data so certain operations are fast. A stack works like a pile of plates: you only add to the top (push) and only take from the top (pop). The last thing in is the first thing out — LIFO. A Python list is a perfect stack: append() pushes, pop() removes and returns the last item.

stack = [1, 2]
stack.append(3)   # [1, 2, 3]
stack.pop()       # returns 3, stack is [1, 2]

Why it matters: the Undo button in every editor is a stack of your recent actions.

Want it another way?
Task

Push the three actions onto history, then undo (pop) the last one. Expected: undo type "!" and history with two actions.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.