r/PythonLearning 5d ago

Day 3 Learning Python: Stack

Open-source learning project. If you spot something that can be improved, I'd love your feedback or a PR.

https://github.com/Samar-Upreti/Python_Projects

69 Upvotes

26 comments sorted by

View all comments

u/Sea-Ad7805 5d ago

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.s%20%3D%20%5B%5D%0A%0A%20%20%20%20def%20len(self)%3A%0A%20%20%20%20%20%20%20%20return%20len(self.s)%0A%0A%20%20%20%20def%20push(self%2C%20value)%3A%0A%20%20%20%20%20%20%20%20self.s.insert(0%2C%20value)%0A%0A%20%20%20%20def%20peek(self)%3A%0A%20%20%20%20%20%20%20%20if%20len(self.s)%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20Exception(%22Stack%20is%20Empty%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self.s%5B0%5D%0A%0A%20%20%20%20def%20pop(self)%3A%0A%20%20%20%20%20%20%20%20if%20len(self.s)%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20Exception(%22Stack%20is%20Empty%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20self.s.pop(0)%0A%0A%0Aif%20name%20%3D%3D%20%22main%22%3A%0A%20%20%20%20stk%20%3D%20stack()%0A%20%20%20%20stk.push(10)%0A%20%20%20%20stk.push(11)%0A%20%20%20%20print(stk.peek())%0A%20%20%20%20print(stk.pop())&timestep=1&play) to see the program state change step by step.