May 2019
Beginner
528 pages
29h 51m
English
The preceding chapter introduced the function-call stack. Python does not have a built-in stack type, but you can think of a stack as a constrained list. You push using list method append, which adds a new element to the end of the list. You pop using list method pop with no arguments, which removes and returns the item at the end of the list.
Let’s create an empty list called stack, push (append) two strings onto it, then pop the strings to confirm they’re retrieved in last-in, first-out (LIFO) order:
In [1]: stack = []In [2]: stack.append('red')In [3]: stackOut[3]: ['red']In [4]: stack.append('green')In [5]: stackOut[5]: ['red', 'green']In [6]: stack.pop()Out[6]: 'green'In [7]: stackOut[7]: ['red'] ...
Read now
Unlock full access