Complete the code to add an element to the top of the stack.
def push(stack, element): stack.[1](element)
The append method adds an element to the end of the list, which represents the top of the stack.
Complete the code to remove and return the top element from the stack.
def pop(stack): if len(stack) == 0: return None return stack.[1]()
The pop method removes and returns the last element of the list, which is the top of the stack.
Fix the error in the code to check if the stack is empty.
def is_empty(stack): return len(stack) [1] 0
To check if the stack is empty, we compare the length to zero using ==.
Fill both blanks to peek at the top element without removing it.
def peek(stack): if len(stack) == 0: return None return stack[1][2]
Using stack[-1] accesses the last element, which is the top of the stack, without removing it.
Fill all three blanks to implement a function that returns the size of the stack and clears it.
def clear_and_size(stack): size = len(stack) stack[1]() return [2], [3]
The clear() method empties the list. We return the saved size and the current length (which is 0) using len(stack).