Pygorithm module in Python
Last Updated :
11 Jul, 2025
Pygorithm module is a Python module written purely in Python and for educational purposes only. One can get the code, time complexities and much more by just importing the required algorithm. It is a good way to start learning Python programming and understanding concepts. Pygorithm module can also help to learn the implementation of all major algorithms in Python language.
To install Pygorithm module:
pip3 install pygorithm
Example:
Python3
# import the required data structure
from pygorithm.data_structures import stack
# create a stack with default stack size 10
myStack = stack.Stack()
# push elements into the stack
myStack.push(2)
myStack.push(5)
myStack.push(9)
myStack.push(10)
# print the contents of stack
print(myStack)
# pop elements from stack
myStack.pop()
print(myStack)
# peek element in stack
print(myStack.peek())
# size of stack
print(myStack.size())
Output:
2 5 9 10
2 5 9
9
3
To see all the available functions in a module, just type
help()
with the module name as argument.
Python3
# Help on package pygorithm.data_structures
help(data_structures)
Output:
NAME
pygorithm.data_structures - Collection of data structure examples
PACKAGE CONTENTS
graph
heap
linked_list
quadtree
queue
stack
tree
trie
DATA
__all__ = ['graph', 'heap', 'linked_list', 'queue', 'stack', 'tree', '...
To get code for any of these data_structures using get_code().
Python3
# to get code for BinarySearchTree
BStree = tree.BinarySearchTree.get_code()
print(BStree)
Output:
class BinarySearchTree(object):
def __init__(self):
self.root = None
def insert(self, data):
"""
inserts a node in the tree
"""
if self.root:
return self.root.insert(data)
else:
self.root = BSTNode(data)
return True
def delete(self, data):
"""
deletes the node with the specified data from the tree
"""
if self.root is not None:
return self.root.delete(data)
def find(self, data):
if self.root:
return self.root.find(data)
else:
return False
def preorder(self):
"""
finding the preorder of the tree
"""
if self.root is not None:
return self.root.preorder(self.root)
def inorder(self):
"""
finding the inorder of the tree
"""
if self.root is not None:
return self.root.inorder(self.root)
def postorder(self):
"""
finding the postorder of the tree
"""
if self.root is not None:
return self.root.postorder(self.root)
@staticmethod
def get_code():
"""
returns the code of the current class
"""
return inspect.getsource(BinarySearchTree)
To get complexities for the following scripts:
Python3
# create a stack with default stack size 10
Bsort = sorting.bubble_sort.time_complexities()
Output:
Best Case: O(n), Average Case: O(n ^ 2), Worst Case: O(n ^ 2).
For Improved Bubble Sort:
Best Case: O(n); Average Case: O(n * (n - 1) / 4); Worst Case: O(n ^ 2)
Quick Link to source code of Pygorithm
Similar Reads
Pymatrix module in python Pymatrix is a lightweight matrix library which supports basic linear algebra operations. The elements in matrix should be numeric type to support basic algebra operations - int, float, rational, complex. Instantiating Matrix  Using Matrix constructor Matrix can be initialised using constructor of
3 min read
Platform Module in Python Platform module in Python is a built-in library that provides a portable way to access detailed information about the underlying platform (hardware and operating system) on which your Python program is running. This can include data such as the OS name and version, machine type, processor info and P
3 min read
PyDictionary module in Python AyDictionary It's a Python module used to fetch definitions of words, while googletrans provides translation services.meaningstranslationsInstallation To install AyDictionary run the following pip code on the terminal / command prompt: pip install AyDictionary googletrans==4.0.0-rc1Getting Started N
1 min read
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
numpy.mod() in Python numpy.mod() function returns the element-wise remainder of division between two arrays. It works like the modulo (%) operator but supports broadcasting and array operations.Example:Pythonimport numpy as np a = np.array([10, 20, 30]) b = np.array([3, 7, 9]) res = np.mod(a, b) print(res)Output[1 6 3]
2 min read
Python Fire Module Python Fire is a library to create CLI applications. It can automatically generate command line Interfaces from any object in python. It is not limited to this, it is a good tool for debugging and development purposes. With the help of Fire, you can turn existing code into CLI. In this article, we w
3 min read