xxxxxxxxxx
import queue
q = queue.Queue # initialisation
# get() – Remove and return an item from the queue. If queue is empty, wait until an item is available.
# put(item) – Put an item into the queue. If the queue is full, wait until a free slot is available before adding the item.
# empty() – Return True if the queue is empty, False otherwise.
# maxsize – Number of items allowed in the queue.
# full() – Return True if there are maxsize items in the queue. If the queue was initialized with maxsize=0 (the default), then full() never returns True.
# get_nowait() – Return an item if one is immediately available, else raise QueueEmpty.
# put_nowait(item) – Put an item into the queue without blocking. If no free slot is immediately available, raise QueueFull.
# qsize() – Return the number of items in the queue.
xxxxxxxxxx
from queue import Queue # watch out with the capital letters
# Making the queue
queuename = Queue()
# We use .put() to insert value and .get() to pop the first one.
# You can think this as a normal queue
queuename.put(1) # adds int 1 to index 0
# To access the queue, you need to change it to list
print(list(queuename.queue))
# Output : [1]
queuename.put(2) # adds int 2 to index 1
print(list(queuename.queue))
# Output: [1,2]
queuename.get() # removes index 0 (int 1)
print(list(queuename.queue))
# Output: [2]
# We can simulate the same effects using normal list, but the longer the queue
# the more ineffecient it becomes
queuesimulate.append(1)
print(queuesimulate)
# Output : [1]
queuesimulate.append(2)
print(queuesimulate)
# Output: [1,2]
queuesimulate.pop(0) # 0 is the index number
print(queuesimulate)
# Output: [2]
xxxxxxxxxx
# Python program to
# demonstrate implementation of
# queue using queue module
from queue import Queue
# Initializing a queue
q = Queue(maxsize = 3)
# qsize() give the maxsize
# of the Queue
print(q.qsize())
# Adding of element to queue
q.put('a')
q.put('b')
q.put('c')
# Return Boolean for Full
# Queue
print("\nFull: ", q.full())
# Removing element from queue
print("\nElements dequeued from the queue")
print(q.get())
print(q.get())
print(q.get())
# Return Boolean for Empty
# Queue
print("\nEmpty: ", q.empty())
q.put(1)
print("\nEmpty: ", q.empty())
print("Full: ", q.full())
# This would result into Infinite
# Loop as the Queue is empty.
# print(q.get())
xxxxxxxxxx
from collections import deque
my_queue = deque
my_queue.append(5)
my_queue.append(10)
print(my_queue)
print(my_queue.popleft())