w3resource

Python Exercise: Find whether a queue is empty or not

Python heap queue algorithm: Exercise-27 with Solution

Write a Python program to find whether a queue is empty or not.

Sample Solution:

Python Code:

import queue
p = queue.Queue()
q = queue.Queue()
for x in range(4):
    q.put(x)
print(p.empty())
print(q.empty())

Sample Output:

True
False

Flowchart:

Python heap queue algorithm: Find whether a queue is empty or not.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to create a queue and display all the members and size of the queue.

Next: Write a Python program to create a FIFO queue.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz


Python: Tips of the Day

Moves the specified amount of elements to the end of the list

Example:

def tips_offset(lst, offset):
  return lst[offset:] + lst[:offset]

print(tips_offset([1, 2, 3, 4, 5, 6, 7, 8], 3)) 
print(tips_offset([1, 2, 3, 4, 5, 6, 7, 8], -3))

Output:

[4, 5, 6, 7, 8, 1, 2, 3]
[6, 7, 8, 1, 2, 3, 4, 5]