in the following figurethere is no existing node after creating our new node thus self top will point to this new node the else part of the if statement guarantees that this happensin scenario where we have an existing stackwe move self top so that it points to the newly created node the newly created node must have its next pointerpointing to the node that used to be the top node on the stackpop operation now we need pop method to remove the top element from the stack as we do sowe need to return the topmost element as well we will make the stack return none if there are no more elementsdef pop(self)if self topdata self top data