WeCodeAcademy

Stacks

A stack is a pile where the last item in is the first out (LIFO: last in, first out), like plates. You only touch the top. The two core operations are push (add on top) and pop (remove from the top). In Python a list is a stack: append pushes and pop() pops, both O(1).

Stacks appear everywhere: the undo button in an editor, the browser back button, and how a program keeps track of function calls.

Examplepython
Open in editor »
Quick check

In a stack, which item is removed first?

Your turn: write the code

Use a stack to reverse the word. Push each letter, then pop them all into a new string, and print it.

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.