Python DSALesson 94Free preview
Stacks
This lesson is part of Python · All levels
Summary
This lesson explains stacks, data structures that work with the LIFO principle or Last In First Out. The elements can only be added and removed from one side of the same thing, the top. You'll learn the operations of push, pop, and peek, as well as see their application to undo features, parentheses checking, and expression evaluation.
Key points
- Stack works LIFO: the last element entering is the first to exit.
- Push adds elements to the top, pop removes the top element, and peek reads the top element without deleting it.
- In Python stacks are most easily represented by regular lists using append() and pop().
- The structure can also be used as a stack and is more efficient for operations at both ends.
- Pop on the empty stack will generate an IndexError error on the Python list.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.