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.

1 Like

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.

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.

1 Like

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)

2 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