From the course: Programming Foundations: Data Structures (2023)
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
What is a stack? - Python Tutorial
From the course: Programming Foundations: Data Structures (2023)
What is a stack?
- Stacks are another container type we can use to store our data. A stack is an ordered series of objects, just like a list, but its intended use is slightly different. We push objects onto the stack and pop objects off of it. Think of a stack of cards. Each card is stacked on top of each other. To take a card off of the stack, we remove it from the top, because it's difficult to remove from the bottom. To add a card to the stack, we place it on top, because it's difficult to add to the bottom. Naturally, if you wanted to add or remove from the bottom of the stack, you would have to lift the entire stack in order to add that item. This is why, for stacks, we add and remove from the top. Stacks follow a last in, first out policy, or a LIFO policy. We say LIFO because the last item on the stack will be the first item removed from the stack. The first item pushed onto the stack will be the last item popped off.…