![]() Finally, we simply call ems and we can see that ‘Orange’ is now in the stack. Then we test the push() method by running stack.push(‘Orange’). Once we are in the terminal we can create a new instance of a stack with the stack = Stack() code. So to run this code we can use python -i stacks.py. We can test this out in the terminal by running the Python file interactively. The push() method accepts an item as a parameter, appends it to the end of our list, and returns nothing. We’ll also need to pass into push the item that we want to add to the stack. The first thing that we want to be able to do is add an item to the stack. Lastly, we’ll create a variable called ems, and initialize that to our empty list. We can also create an init method that uses a list to hold the items in the stack. First, we need to define the class itself. ![]() Now that we’ve covered the stack abstract data type, we know what we want the stack to do, and we can start to stub out a stack class and its methods.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |