FAQ: Queues: Python - Queues Python Size

This community-built FAQ covers the “Queues Python Size” exercise from the lesson “Queues: Python”.

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

Computer Science

Linear Data Structures

FAQs on the exercise Queues Python Size

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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!

def init(self, max_size=None):
self.head = None
self.tail = None
self.max_size = max_size
self.size = 0

As you can see from the method above, one parameter has been defined inside the method, which is max_size that’s been set to None as default inside the method. And then there’s the other parameter like head and tail that has been said to None as default as well but below the method. What kind of difference does it make exactly?

The Difference is that this makes it easy for a max size to be set at default. The other variables are 100% reliant on each other such as head and tail, you wouldn’t want someone to set those to a certain value that contradicts itself at the start, so they are set later on when working on the queue. Think about them like size which must start at 0.

1 Like

I am a c programmer but I am learning python as well.I am not getting the feel of data structures in python on code academy.For me It was easier to implement data structures in c language .The codeacademy is spoon feeding me.I am frustrated with so many lines of code in python as compared to c.

2 Likes

Although i have near zero knowledge in c, i guess you are very capable to recognize data structures. I think what you are learning here is more about the syntax instead of the fundamentals (data structures), i mean, how python works. Comparing both would be frustrating to you, i think you can always check the python documentation to strengthen your understanding.

1 Like

FYI

https://docs.python.org/3/tutorial/datastructures.html

We should not be afraid of, or neglectful of the documentation right from the horse’s mouth. Always start there then branch out to articles and other tutorials until the basics are cemented in place.

Lists can be linear lists, queues, stacks, or comprehensions. Python gives us the tools to work with each. The thing they have in common is that they are all lists in square brackets.

2 Likes

Just wondering, in Queues Python Size the last point says:
“At the top of your peek() method body, use get_size() to see if the queue is empty.”

It instructs me to use “get_size()” but I just finished making an “is_empty()”, why wouldn’t I use that? (and I did use that, TBH)

4 Likes

Hello, I am learning Queues and I have a question on how the return works in the code below. Help please!

from node import Node

class Queue:
def init(self,max_size = None):
self.head = None
self.tail = None
self.max_size = max_size
self.size = 0

def peek(self):
return self.head.get_value()

def get_size(self):
return self.size

def has_space(self):
if self.max_size == None:
return True
else:
return self.max_size > self.get_size()

No.1

else:
return self.max_size > self.get_size()

No.2

elif self.max_size != None and self.max_size > self.get_size():
return True

in the code, the code block above (No.1) with the code block below(No.2) result same?
will it give same return value?

Thank you !:slight_smile:

Did you try to run it?

In your accepted code (all check marks present) add an instantiation so you can run the methods.

q = Queue()
q.peek()
# should print, Nothing to see here!
print (q.has_space())
# should print, True

Aside

Granted, None can be evaluated, but why use the literal when we have, not. This will cast us a nice new boolean, True when max_size is None, and False otherwise.

not self.max_size

Now with that boolean we can proceed to the AND expression, or rather, the OR…

not self.max_size or self.max_size > self.get_size()

and rather than returning True, we can just return that expression’s outcome.

  return not self.max_size or self.max_size > self.get_size()

Recall that OR short-circuits on True. Only when the first operand yields False is the second operand evaluated. Only the order we evaluate them connects the two, otherwise they are mutually exclusive.

AND, on the other hand relies on both operands being True, and sometimes we wrestle with the logic to get what we want. Very precarious ground, that.


No matter, the LE writes in the code for us in each new lesson, regardless what we had in the previous. You’re stuck with this, for now…

  def has_space(self):
    if self.max_size == None:
      return True
    else:
      return self.max_size > self.get_size()
    
  def is_empty(self):
    return self.size == 0
1 Like

I have a question! In the body of is_empty() method, we return self.size == 0. why we don’s say self.get_size() == 0. I mean get.size() return self.size. Would you explain around it a little bit? Thanks

I write the method to check if there is space in the queue as below:

def has_space(self):
    if self.max_size == None:
        return True
    else:
        if self.max_size > self.get_size:
            return True

but it gives error. Instead, it says the else should be

else:
  return self.max_size > self.get_size()

Can anyone help please?

1 Like

This looks out of place.

Alright, this is the second time I’m posting something like this.

These data structure lessons need to be tested for QA by individuals that don’t know the answer. I’m finding it frustrating that I’m getting stuck where I shouldn’t be because of bad language. At this stage it’s a paid service and should be of better quality than the free services.

This particular instance is: " Below get_size() , define another method has_space() . Inside the method, check if a self.max_size has been set.

  • If there’s no max_size set, then we will always have space in the queue, so we can return True
  • If so, return True if the max_size is greater than self.get_size()"

This is what solution I proposed based off of the last line:
def has_space(self):
if self.max_size == None:
return True
elif self.max_size > self. get_size():
return True

I believe that should be correct. However, it was not. And after multiple variations and changes I could not progress and had to look up the solution. the solution turned out to be an else statement. The statement “If so, …” is very misleading and gives negative training.

P.S. forgive the indentation, I have them written but its not posting it.

2 Likes

If you have an else statement, you do not need to put the If statement below it. You can try an else if statement instead. (although that does not work for this solution, I wrote about that and how it needs to be changed.)

An else statement covers every other possible option besides the If statements above if. Therefore, the if statement within the else statement in this circumstance does not make sense.

I hope that helps!

def init (self, max_size=None):
self.head = None
self.tail = None
self.max_size = max_size
self.size = 0
As you can see from the method above, one parameter has been defined inside the method, which is max_size that’s been set to None as default inside the method. And then there’s the other parameter like head and tail that has been said to None as default as well but below the method. What kind of difference does it make exactly?

I am somewhat confused about the exact same thing.

It has never been made Cristal clear to me, when to instantiate a variable/property directly within the class (as was done with “max_size”) in this case, and when to only instantiate it afterwards right below the constructor (as done with “tail, head and size” here).

@mtf Maybe you can help? I have read multiple great explanations from you :slight_smile:

Would be very much appreciated

Default parameters are positional arguments that MAY take a value when an instance is invoked.

q = Queue(100)

max_size will take the value in the argument, rather than the default.

Along similar lines, I got really stuck on:

def has_space(self):
if self.max_size == None:
return True
else:
return self.max_size > self.get_size()

Before adding () to self.get_size in the else statement I kept receive an error about checking that the size was > 0 in the method. I couldn’t work out what was wrong until I consulted the solution. I was then pondering when to use () after a method and when not to.

I would say that self.max_size is defined in the parameters for the Queue class. It’s set, so we need not retrieve it, hence no (). On the other hand, get_size is not static, so we need to retrieve a value with ().

I’m thinking that’s a rather clumsy way to express that concept though. Could one of the mods expand upon that idea a bit more elegantly…? Much obliged!

The operative word is, method. Generally speaking we are more interested in invoking the method with () so we get a return value than citing a reference to the method.

1 Like

Hi guys, I am quite confused as well with this exercice, firs of all:

4.

Define another method is_empty for Queue . The method should return True if the queue is empty (if the size of the queue is 0).

But the solution given is def is_empty(self): return self.size == 0

Second:

5.

Now we’ll make sure we aren’t attempting to peek() on an empty queue. After all, a deli server can’t get an order from a line with no customers!

At the top of your peek() method body, use get_size() to see if the queue is empty.

  • if so, the method should just print “Nothing to see here!”
  • if not, peek() will perform the same as it did before

Why we just don’t use the method is_empty created before?

Just looking at your code, what would be returned if the size of the queue is equal to the max size?