Python

Data structure and Algorithms using Python

Data structure and Algorithms using Python

Data structure

We have a lot of data around us and we create new data every moment. How can this large amount of random and unstructured data be handled efficiently and structurally? If we examine the data carefully we can see some patterns. These patterns allow us to structurally sort and rearrange data. Some data need to be indexed, others not. As we have learned sets in mathematics – a collection of things (traditionally things have to be unique) their order does not matter.

We can perform some mathematical operations on the sets to get the required or relevant data from the collection. We need to not only structure or rearrange the data, but also store the data efficiently for quick access and perform some computational activities. And thus the data structures came for. When dealing with large amounts of data (big data) the data structure helps us a lot to store and access data efficiently.

In other words, data structures allow us to organize, store, and manage data for efficient access and stability. Here we are going to discuss about the data structures in Python. Python has four built-in data structures: table, tuple, set, and dictionary. As we know, Python helps us to write code very easily, so here we choose Python.

List

The list is an ordered collection of elements of any data type, enclosed in square brackets and separated by commas. The order of the elements here is important, not the data types. The list is also a mutable(can add, edit, delete items) data type.

Ex:- some_list = [1,’apple’,’and’,1.5,’banana’, ‘contain’,{‘vitamin’, ‘c’, ‘b’, ‘e’}]

Tuple

Like the list, the tuple is another container. It is an immutable(can not add, edit, delete elements) data type and an ordered collection of elements of any data type, enclosed in simple brackets and separated by commas. It maintains the order of the elements like list.

Ex:- some_tuple = (‘this’, ‘is’, ‘a’, [‘of elements’, 1,2,3])

Set

This is a unordered collection of elements of any data type, enclosed in double brackets and separated by commas. It is a mutable data type. This allows us to quickly remove duplicate elements from the list or tuple. Ex:- some_set = {1, ‘is’, ‘real’, ‘and’, ‘not’, ‘irrational’, ‘but’, ‘square root of’, 2 , ‘is.’}

Dictionary

Dictionary is a unordered collection of pairs of elements of any data type separated by colon and these pairs are separated by commas and enclosed in double brackets. It is a mutable data type. Ex:- some_dictionary = {“Raju”:”9588888880”, “Lakshmi”:”9588776655”}

User Defined Data Structures:-

Python also provides user-defined data structures. Queue, stack, and tree are some examples of user-defined data structures.

Stack using list(array)

What is a stack? Libraries are familiar to us, aren’t they? In libraries, we notice books arranged on shelves. How to choose a book without mistaking the order of the books. Select one from the top and check it out. This process continues until we get the book we need. We are only allowed to select the book from the top and insert it from the top.

We can consider the shelf as a container or a stack. In other words, a stack is a data structure in which the elements are arranged in sequence. It uses a mechanism called L.I.F.O Last In First Out. Here we can perform two operations push and pop.

Push – inserting an item into the stack

Pop – it delete an item from the stack(last item)

Ex:- As we know, Python helps us to write code very easily, so we choose Python.

class Stack: 
def __init__(self):		
                self.data = []
	def push(self, element):
		self.data.append(element)
	def pop(self):
		data = self.data[-1]
		del self.data[-1]
		return data
some_stack = Stack()
some_stack.push(1) # self.data will be [1]
some_stack.pop() # return 1, self.data will be []

Queue using list(array)

Queues are very familiar to us. Everywhere we look, the que is.. in ATM, Theater, Govt. Offices(just for kidding). Like these queue, we can define some data structure and it is linear data structure that means First In First Out (F.I.F.O). Queues are linear data structures where elements are in a sequence. The first element can be called frond and last is rear. In Queue we can perform two operations, enqueue and dequeue. And also two conditions, overflow and underflow.

Enqueue – Inserting an elements into the queue. And it will be at the rear Dequeue – Deleting an elements from the queue. And it will be done at front Overflow – Inserting into a queue that is full Underflow – deleting from the empty queue

Tree

As the name implies it is used to define range. A tree structure starts from the root and goes to the last node. The last nodes are called child nodes. The binary tree data structure is one of several types of tree structures. It has two children at its node.

Note: – There is also other user-defined data structure such as LinkedIn-list and graph.

Algorithms

Algorithms have existed since ancient times. They used algorithms to solve problems, especially in ancient Egypt and Greek. The word algorithm is derived from the Persian mathematician Muhammad ibn Musa al-Khwarizmi. Today we use algorithms to solve problems, especially with computers. Algorithms are step-by-step procedures for solving a problem. There are three main approaches to solving algorithms.

Divide and conquer, dynamic programming and greed algorithm. When we write an algorithm, we need to know what the exact problem is, determine where to start, stop and formulate intermediate steps. Algorithms (cleaner code and fast implementation view) are very easy to implement using Python.

Summary

Data structures and algorithms are very handy when it comes to big data problem. Python allows us to code and execute the algorithm very quickly. Python also provides some built-in data structures such as list, tuple, set and dictionary, and in Python we can define our own data structures. Here we discuss several useful and familiar data structures such as stack, queue, and tree. Also we discussed the algorithms very briefly.

But there is more to it than we have discussed. If you want to know more about Python programming, check out the best python training in Kerala for beginners and advanced learners. As we all know, becoming a Python developer nowadays is in emerging demand and dream of every IT professional. It is not that hard to become a Python developer as it seems. You can also checkout best python developer training institutes or courses.

Author: STEPS

Leave a Reply

Your email address will not be published. Required fields are marked *