We can implement the Priority queue using Python queue.priorityqueue methods. Priorityqueue is similar to Queue but it will remove items from it based on Priority. Fewer priority items will never be removed first. If elements are of equal priority, then the deletion will happen based on the first income like Queue.

In this article, we will discuss the methods supported by Priorityqueue. It is available in the queue module. So we need to import this module

1. Insert elements one by one into the priority queue using put() method
2. Return total number of elements present in priority queue using qsize()
3. Check whether the priority queue is full or not using full() method
4. Popping elements from the priority queue using get() method
5. Check whether the priority queue is empty or not using empty()

## 1. Quick Examples of Priorityqueue methods

Following are quick examples of using priorityqueue methods in python.

``````
# Below are some quick examples.

import queue

# Initializing a priority queue
pqueue = queue.PriorityQueue()

# Using put() function to insert elements
pqueue.put((4,'China'))
pqueue.put((1,'Russia'))
pqueue.put((2,'England'))
pqueue.put((5,'Nepal'))
pqueue.put((3,'Italy'))

# Get 3 lowest priority countries
print(pqueue.get())
print(pqueue.get())
print(pqueue.get())

print(pqueue.qsize())

# Use the full() function to check priority queue is full or not.
print(pqueue.full())

# Use the empty() function to check priority queue is empty or not.
print(pqueue.empty())
``````

## 2. put() method of Priorityqueue

Priorityqueue `put()` method is used to insert an item to the priority queue. At a time, we can insert only one item. It takes priority and item to be inserted as parameters.

### 2.1 Syntax for put() method

Let’s see the syntax of put() method.

``````
# Here, pqueue is the input priority queue implemented using PriorityQueue().
pqueue.put((priority,item))
``````

### 2.2 Example

Let’s create a priority queue with maxsize 10 and insert 5 countries with priorities one by one to it.

``````
import queue

# Initializing a priority queue
pqueue = queue.PriorityQueue()

# Using put() function to insert elements
pqueue.put((4,'China'))
pqueue.put((1,'Russia'))
pqueue.put((2,'England'))
pqueue.put((5,'Nepal'))
pqueue.put((3,'Italy'))
``````

## 3. get() method of Priorityqueue

Priorityqueue `get()` method is used to remove only one item from the priority queue at a time. It won’t take any parameter. It will remove the firstly inserted item from priority queue if the priority is same, Otherwise low priority item will be removed first.

### 3.1 Syntax for get() method

Let’s see the syntax of get() method.

``````
pqueue.get()
``````

### 3.2 Example

Let’s get 3 elements by deleting them.

``````
# Get 3 lowest priority countries
print(pqueue.get())
print(pqueue.get())
print(pqueue.get())
``````

Yields below output.

We inserted the elements in the order – [‘China’, ‘Russia’, ‘England’, ‘Nepal’, ‘Italy’]. We are deleting 3 elements one by one. ‘Russia’ is deleted first since it is having priority-1, ‘England’ is deleted second since it is having priority-2 and ‘Italy’ is deleted third since it is having priority-3.

`qsize()` method is used to return the total number of elements presents in the priorityqueue. It won’t take any parameter.

### 4.1 Syntax for qsize() method

Let’s see the syntax of qsize() method.

``````
pqueue.qsize()
``````

### 4.2 Examples

Example 1: Let’s create a priority queue with 5 elements and return the total number of elements.

``````
print(pqueue.qsize())

# Output:
# 5
``````

## 5. full() method of Priorityqueue

Priorityqueue `full()` method returns `True` if the priority queue is full i.e if total number of elements in the priority queue is equal to max size, Otherwise `False` is returned.

### 5.1 Syntax for full() method

Let’s see the syntax of full() method.

``````
pqueue.full()
``````

### 5.2 Example

Let’s create a priority queue with 5 elements and check whether the it is full or not.

``````
import queue

# Initializing a priority queue.
pqueue = queue.PriorityQueue(maxsize=5)

# Use the full() function to check priority queue is full or not.
print(pqueue.full())

pqueue.put((4,'China'))
pqueue.put((1,'Russia'))
pqueue.put((2,'England'))
pqueue.put((5,'Nepal'))
pqueue.put((3,'Italy'))

# Use the full() function to check priority queue is full or not.
print(pqueue.full())

# Output:
# False
# True
``````

Firstly, the priority queue is not full. So False is returned. After that we inserted 5 items into the queue. Now full() returned True (equal to maxsize).

## 6. empty() Method of Priorityqueue

Priorityqueue `empty()` method returns `True` if the priority queue is empty. Otherwise `False` is returned.

### 6.1 Syntax for empty() method

Let’s see the syntax of empty() method.

``````
pqueue.empty()
``````

### 6.2 Example

Let’s create a priority queue with 5 elements and check whether the priority queue is empty or not.

``````
import queue

# Initializing a priority queue.
pqueue = queue.PriorityQueue(maxsize=5)

# Use the empty() function to check priority queue is empty or not.
print(pqueue.empty())

pqueue.put((4,'China'))
pqueue.put((1,'Russia'))
pqueue.put((2,'England'))
pqueue.put((5,'Nepal'))
pqueue.put((3,'Italy'))

# Use the empty() function to check priority queue is empty or not.
print(pqueue.empty())

# Output:
# True
# False
``````

Firstly, the priority queue is empty. So True is returned. After that we inserted 5 items into the priority queue. Now empty() returned False.

## 7. Conclusion

We have seen how to insert elements into the Python priorityqueue using the put() method. To delete elements from the priority queue, we used the get() method. To check whether the priority queue is empty or not, we used full() and empty() methods. qsize() method will return the total number of elements present in the priority queue.