Python Stack đ
Stack Data Structure
A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner. In stack, a new element is added at one end and an element is removed from that end only.
⨠Stack using List
Python's built-in data structure list can be used as a stack.
Instead of push(), append() is used to add elements to the top of the stack while pop() removes the element in LIFO order.
python
stack = []
# append() function to push element in the stack
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack')
print(stack)
# pop() function to pop element from stack in LIFO order
print('
Elements popped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
print('
Stack after elements are popped:')
print(stack)đŽ Interactive Stack Demo
Stack Operations
âšī¸ Ready to stack items!
LIFO: Last In, First Out
Book A
Book B
Book C
Stack Bottom
đĄ Did You Know?
Stacks are used in many algorithms, such as Depth First Search (DFS) and expression evaluation.