DEV Community


Posted on

Python: A Beginner's Guide to Data Structures and Algorithms

what are Data structures
Data Structures are a remarkable way of organizing, managing and storing data in computers in such a way that we can perform operations on the saved data very efficiently.

Image description

In this article, I will briefly explain the commonly used data structures in python every programmer must know.
There are four types of built-in data structures in Python offers: list, tuple, set, and dictionary.

A list is well defined using square brackets which holds data that is separated by commas. A list is mutable and ordered. It can contain a mix of different data types.

# declaring an empty lists
lists = []

# initializing and defining lists with different data types
list_Numbers = [1, 2, 3, 4, 5]

# initializing and defining lists with string data types
list_string = ["Bale", "Kane", "Sterling"]

# initializing and defining lists with mixed data types
mix_list = [9, 4, "Anne"]
Enter fullscreen mode Exit fullscreen mode

Now lets try to access the data stored in the list using their index numbers, which begins count from 0 i.e. the first element in the list.
open your python shell console

>>> print the first element
>>> lists = [1, 2, 3, 4, 5]
>>> print(lists[0])
>>> print(lists[0:3])
[1, 2, 3]
>>> # prints the last element in the list
>>> print(lists[-1])

>>> # sorting the list in ascending order
>>> lists = [2, 7, 9, 3, 1]
>>> lists.sort()
>>> print(lists)
[1, 2, 3, 7, 9]
>>> # sorting the list in descending order
>>> lists.sort(reverse=True)
>>> print(lists)
[9, 7, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

since list are mutable, we can perform addition and deletion of element in list.
to add more elements we can use these functions: append(), extend(), and insert().
to delete elements, we can use del(), pop() and remove() function

A tuple is a data type for storing immutable ordered sequences of elements. tuples are Immutable because you can neither add nor remove elements from tuples, or sort them in place unlike lists which are mutable.

>>> # declaring an empty tuple
>>> emptY_tuple = ()
>>> print(empty_tuple)

Enter fullscreen mode Exit fullscreen mode

Set is a mutable and unordered collection of unique elements. It can permit us to remove duplicate quickly from a list.

Dictionary is a mutable and unordered data structure, used to store data. It permits storing a pair of items which are in keys and values.

>>> Dict1 = {1: 'Hello', 2: 'To', 3: 'You'}
>>> print(Dict1)
{1: 'Hello', 2: 'To', 3: 'You'}
Enter fullscreen mode Exit fullscreen mode

User-Defined Data structure
A stack is a LIFO (Last In First Out — the element placed at last can be accessed at first) data structure which can be commonly found in many programming languages. This structure is named as “stack”. example of stack is in

Image description

Operations carried out on stack are push and pop.
push is used in inserting an element into the stack
pop is used in deleting an element in the stack

Queues is a also a linear data structure which uses First-In/First Out(FIFO) to store items.

Image description

Operations that can be performed on the queue are:
Enqueue - which is used to add item to the queues
Dequeue - It removes an item from the queue
Front - the front item from the queue
Rear - the last item from the queue

Binary Trees
Trees are also non-linear data structures that represent nodes connected by edges. Each Tree consists of a root node from which we can access each element of the tree.

Image description

There are other data structures which can be implemented linked list, graphs and hash tables.

Algorithm is a step-by-step course of action, which defines a set of instructions to be executed in a certain order to get the desired output. Algorithms are generally created independent of underlying languages

the common operations that can be done are:

  • Search
  • Sort
  • Insert
  • Delete
  • Update

There are three main approaches in solving an algorithm
Greedy Algorithm
Dynamic Algorithm
Divide and conquer Algorithm



Top comments (1)

henry_lee_1787e739b0c8191 profile image
Develop Mastery

nice contents! thanks