FAQ: Stacks: Python - Stacks Python Review

This community-built FAQ covers the “Stacks Python Review” exercise from the lesson “Stacks: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Linear Data Structures

FAQs on the exercise Stacks Python Review

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Ok, I actually didn’t notice this until I saw a similar format in the quiz to follow this… Notice the if: else: statements below:

  def push(self, value):
    if self.has_space():
      item = Node(value)
      self.top_item = item
      self.size += 1
      print("Adding {} to the pizza stack!".format(value))
      print("No room for {}!".format(value))

I don’t recall learning this following. It appears to be an if else statement without the else? Is that right? When did we learn this, so that I can go back to it an review it? I tried to remove the else in above statement to see if it worked that way, and it didn’t. It printed No room for {}!".format(value) each time. So what’s happening?

def pop(self):
    if not self.is_empty():
      item_to_remove = self.top_item
      self.top_item = item_to_remove.get_next_node()
      self.size -= 1
      print("Delivering " + item_to_remove.get_value())
      return item_to_remove.get_value()
    print("All out of pizza.")

  def peek(self):
    if not self.is_empty():
      return self.top_item.get_value()
    print("Nothing to see here!")

Here are two images of what I am talking about to help make sure, in case I typed the code wrong above.


When the action in an if branch is return then there is no need to protect the default (else) case. It can be the next statement following the if.


Why is the pizza stack used as an example?
Wouldn’t this be a bad use case for a LIFO data structure?
Wouldn’t making / delivering pizza be more suited for a queue / FIFO structure?

Just wondering.



Came here wondering the same thing! My guess is they were initially going for a ‘stacking empty pizza boxes’ example and then changed it to deliveries halfway through to make it a more fun example.

When we stack the pizza boxes, the stack grows upward. When we reach for a box it will come off the top of the stack, so that is LIFO.

Delivery, on the other hand will be in the order in which the order was received, FIFO, in this case. The oldest order is delivered first.

This is good to know about using return and else, however, why is the return statement being used in the first place when the call to the pop() method, pizza_stack.pop(), isn’t using the returned value?

return item_to_remove.get_value()

To fit the logic between the states and give the caller an outcome to move forward with.

Sure but my real question is why use a return statement :

    return item_to_remove.get_value()
print("All out of pizza.")

in favor of an else statement:

    print("All out of pizza.")

When I run the code with the else statement I don’t see any difference in the output.

Programmatically, there’s no difference between

if condition:
    return value


if condition:
    return value

They do the same thing and will only print if the condition is false.
The difference is more a matter of style and how explicit you like to be. Using an else can certainly help follow the logic of the program. The version without an else is a bit trickier and takes some programming knowledge to understand what is happening an how to use it. At this point in the course, it looks like codecademy trusts that students have that knowledge to understand it or know how to find out like asking on here.

That’s not true. The if/else will only do one or the other. The second snippet will only do the if statement if the condition is met AND always do the print.

How can it do the print if return has been sent back? It’s left the function.

If the condition is met, we will hit the return statement and the function ends. Nothing after the return will be run.

That’s true. I just caught that this time. But its not the same as an if/else like the person mentioned.

To clarify why they are different, since I said that, in this case they are not. But only in this one case. Had the return been anything else, then they are not and never are the same. That’s why they are still different. The difference is the return, not the logic that went into it.


if condition:

Or whatever you want to put in that line will never do this

if condition:
    return value

Unless it was a return in this one specific case. To me its misleading to say that is how it is an why I disagreed.

But they are the same. We don’t need an else in either case since return is there.

You’re right that this structure only works if the conditional leads to a return statement. That specific case is what the whole thread was about though. I didn’t cover other uses of an if-else clause because anyone who has made it this far in the course should have a decent understanding of them. The return statement makes it possible to do an if-else without the else, but that method is a bit trickier and non obvious for those who haven’t seen it before.