From the course: Python Data Structures and Algorithms
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Build a stack class in Python - Python Tutorial
From the course: Python Data Structures and Algorithms
Build a stack class in Python
- [Instructor] In this video, we're going to write a stack class in Python. If you are unfamiliar with Python classes, then don't worry, I will explain everything you need to know as we go along. We are using a class to represent a stack for learning purposes because the abstraction is helpful for gaining a deep understanding of how the data structure works, including using the appropriate terminology, such as push and pop. In practical situations, you might just use a list. There is another reason to use a class though. If your code needed a stack and you provide a list, there's nothing to stop another programmer from coding insert, remove, and other list functions that will affect the order of your stack. This fundamentally ruins the point of defining a stack, as it no longer functions the way it should. The existing list methods make it very easy to use a list as a stack. Remember, the last element added is the first element retrieved, Last In, First Out or LIFO. To add an item to…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.