Python: A Beginner's Guide to Data Structures and Algorithms
Tags: python, algorithms
Published: 2022-02-21 22:49:39

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvtk31jm4rcfydvsrhhs.png)

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.

_**List**_
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"]
```
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])
1
>>>
>>> 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]
>>>
```
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

**_Tuples_**
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)
()

```

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

**_Dictionaries_**
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'}
```

**User-Defined Data structure**
**_stacks_**
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsz4c6sso8flv9afs8tc.png)

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_**
Queues is a also a linear data structure which uses First-In/First Out(FIFO) to store items.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kyq4tazf2fkoxdais91x.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3h2rkvoai6j0c9it1swv.png)

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

**Algorithms**
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















**References**
1. https://www.google.com/url?sa=i&url=https%3A%2F%2Fintellipaat.com%2Fblog%2Ftutorial%2Fpython-tutorial%2Fdata-structures-with-python-cheat-sheet%2F&psig=AOvVaw3cDRoqhfQgVk_oGdzqPnJl&ust=1645563970045000&source=images&cd=vfe&ved=2ahUKEwiFyLHQ2ZH2AhU8_rsIHenHDpcQr4kDegUIARCnAQ
2. https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.programiz.com%2Fdsa%2Fstack&psig=AOvVaw2T5OGqPtqkDe7BuB325GD6&ust=1645568362317000&source=images&cd=vfe&ved=2ahUKEwi2yeT-6ZH2AhU9i_0HHVIMAX0Qr4kDegUIARC7AQ
3. https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.delftstack.com%2Fhowto%2Fpython%2Ftrees-in-python%2F&psig=AOvVaw1Nzy6BTVm4IlHi13UlVBGX&ust=1645569306571000&source=images&cd=vfe&ved=2ahUKEwjIn4XB7ZH2AhWSD-wKHYXTC58Qr4kDegUIARCGAg
4.