w3resource

Python: Create a FIFO queue

Python heap queue algorithm: Exercise-28 with Solution

Write a Python program to create a FIFO queue.

Sample Solution:

Python Code:

import queue
q = queue.Queue()
#insert items at the end of the queue 
for x in range(4):
    q.put(str(x))
#remove items from the head of the queue 
while not q.empty():
    print(q.get(), end=" ")
print("\n")

Sample Output:

0 1 2 3 

Flowchart:

Python heap queue algorithm: Create a FIFO queue.

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 find whether a queue is empty or not.

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

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz


Python: Tips of the Day

Different ways to test multiple flags at once in Python

Example:

# Different ways to test multiple
	# flags at once in Python
a, b, c = 0, 1, 0

if a == 1 or b == 1 or c == 1:
  print('passed')

if 1 in (a, b, c):
  print('passed')
  
  # These only test for truthiness:
if a or b or c:
  print('passed')

if any((a, b, c)):
  print('passed')

Output:

passed
passed
passed
passed