Lesson 21 of 55
9 mins readPython Stack & Queue Ops: push, pop, shift & unshift
In Plain English
Perl arrays provide built-in primitives for stack (LIFO) and queue (FIFO) data structures: `push`/`pop` at the end, and `unshift`/`shift` at the beginning.
Deep Dive: How It Works
push(@arr, $item): Appends item(s) to the end of the array.
pop(@arr): Removes and returns the last element.
unshift(@arr, $item): Prepends item(s) to the beginning of the array.
shift(@arr): Removes and returns the first element.
Core Rules to Remember

Stack (LIFO): Use `push` and `pop` for stack operations.

Queue (FIFO): Use `push` to enqueue and `shift` to dequeue.
Live Interactive Example
Hit Run Code to see it liveManipulating Array Boundaries
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: shift removed the front element; pop removed the rear element.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Push and Pop Stack Items
Declare `my @stack = (10, 20);`
Call `push(@stack, 30);` then `my $top = pop(@stack);`
Print `"Popped: $top, Top is now: $stack[-1]\n"`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.