A stack is like a pile of plates: you only put on the top and only take from the top. Python needs no special type; a list does it: append puts on top, pop takes the top, stack[-1] looks at it.
PAIR = {")": "(", "]": "["}stack = []for ch in text:if ch in "([":stack.append(ch)elif not stack or stack.pop() != PAIR[ch]:return Falsereturn not stack
You know you need a stack when you are setting things aside to deal with later, and the last one set aside is the first you must deal with. Nested things (brackets, folders, function calls) are always like that.