Python 102: Introduction to Data Structures and Algorithms With Python. 
Tags: programming, python, tutorial, algorithms
Published: 2022-02-20 10:07:32

This article is beginner friendly to kick start learning data structure and algorithms using python programming language. This article discusses the in-built data structures such list, tuples, set, dictionaries, and user defined data structures such linked list, graph, trees etc., and algorithms. Data is crucial which means that it needs to be stored and accessed in timely manner (This is archived via data structures). The articles discussed data structured and algorithms in python.
#Data Structure Definition
Refers to data organization, storage, and data management format that enables efficient modification and access.  Data structure allows organization of data in such a way that storage of data collection, relation and and performance of operation between them is possible.
####Type of Data Structures in Python

*The following is a simple breakdown:*
![Types of data Structures](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irxu1gyvsmoazy0c5zo2.png)
######a.) Built-in Data Structures
These structures are called List, Dictionary, Tuple and Set.

#####i.) List
They have positive and negative index, the +ve index starts from 0 and the -ve index starts from -1.
- Elements are ordered, changeable and allow duplicates
- Items can be added or removed
- Defined using [] or the list() constructor
- Use dot(.) to get in-build method to perform sort, remove etc

####Example:
```python
my_list = [] #empty list
print(my_list)
my_list = [10, 22, 3, 'Sam', 3.132] #list with data
print(my_list)
```
*Results:*

```python
[]
[10, 22, 3, 'Sam', 3.132]
```
Some of operations that can be performed using List are *append
clear
copy
count
extend
insert
index
pop
remove
reverse
sort*

#####ii.) Dictionary
They are used to store key value pairs.
- They are ordered , changeable, and do not allow duplicates(from python 3.7 and above)

#####Example:
```python
my_dict = {} #empty dictionary
print(my_dict)
my_dict = {1: 'Django', 2: 'Laravel'} #With elements
print(my_dict)
```
*Results:*

```python
{}
{1: 'Django', 2: 'Laravel'}
```
The following are operations that can be performed using Dictoionary
*clear()
copy()
values()
update()
fromkeys()
get()
items()
keys()
pop()
popitem()
setdefault()*

##### iii.) Tuple
They are the same as lists with the exception that the data once entered into the tuple cannot be changed no matter what.
- Items are ordered, unchangeable, and allow duplicates values
- Items can be accessed by referring to the index number, inside square brackets

#####Example 
```python
names= ("apple", "banana", "cherry")
print(names)
```
*Results:*

```python
('apple', 'banana', 'cherry')
```

#####iv.) Sets
They are a collection of unordered elements that are unique. Meaning that even if the data is repeated more than one time, it would be entered into the set only once.
- Unordered, unchangeable, unindexed, and do not allow duplicate values
- Items in a set cannot be accessed using index but can be accessed using loop
#####Example:
```python
num_set = {1, 3, 6, 70}
print(num_set )
```
*Results:*

```python
{1, 3, 6, 70}
```
Operation in set are Union, intersection, Difference etc

####Example

```python
my_set = {1, 2, 3, 4}
my_set_2 = {3, 4, 5, 6}
print(my_set.union(my_set_2), '----------', my_set | my_set_2)
print(my_set.intersection(my_set_2), '----------', my_set & my_set_2)
print(my_set.difference(my_set_2), '----------', my_set - my_set_2)
print(my_set.symmetric_difference(my_set_2), '----------', my_set ^ my_set_2)
my_set.clear()
print(my_set)
```
*Results:*

```python
{1, 2, 3, 4, 5, 6} ———- {1, 2, 3, 4, 5, 6}
{3, 4} ———- {3, 4}
{1, 2} ———- {1, 2}
{1, 2, 5, 6} ———- {1, 2, 5, 6}
set()
```

######b.) User defined Data Structures
These include Queue, tree, Linked List, Stack, graph, map etc
#####NB Array Vs List
They are the same with one difference. Lists allow heterogeneous data element storage whereas Arrays allow only homogeneous elements to be stored within them.
#####i.) Stacks
They are linear Data Structures which are based on the principle of *Last-In-First-Out (LIFO)* where data which is entered last will be the first to get accessed. Built using the array structure and has operations namely, *pushing (adding)* elements, *popping (deleting)* elements and accessing elements only from one point in the stack called the *TOP*.TOP is the pointer to the current position of the stack
![Stack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/epnpwo08xpyfsr83cd7t.png)

#####Operation in Stacks
######PUSH
Adds an element at the top of the stack
![push](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0l1mt1e544cdg68qelvh.png)

######POP
Removes an element from the top of the stack 
![pop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olape90sbrr0hf1mndde.png)

#####ii.) Queue
Is a linear data structure which is based on the principle of *First-In-First-Out (FIFO)* where the data entered first will be accessed first.It is built using the array structure and has operations which can be performed from both ends of the Queue, i.e, head-tail or front-back. Operations such as adding and deleting elements are called En-Queue and De-Queue and accessing the elements can be performed.
![Queue](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqiho1ndnhfq9h74rfvo.png)

#####iii.) Tree
They are non-linear data structure that have roots and node.The root is the node from where the data originates and the nodes are the other data points that are available to us. The node that precedes is the parent and the node after is called the child. There are levels a tree has to show the depth of information. The last nodes are called the leaves.
![Trees](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gpsdsgrhh2w4d1dc73q.png)

#####iv.) Linked List
Linked lists are linear Data Structures which are not stored consequently but are linked with each other using pointers. The node of a linked list is composed of data and a pointer called next. 
![Linked lists](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt3hnwqcl3d4lczn7vvx.png)

##### v) Graph
Graphs are used to store data collection of points called vertices (nodes) and edges (edges). Graphs can be called as the most accurate representation of a real-world map. They are used to find the various cost-to-distance between the various data points called as the nodes and hence find the least path. 
![Grapgh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vl1jqsjjbtkjuv7lu3al.png)

##### v) HashMaps
are the same as what dictionaries are in Python. They can be used to implement applications such as phonebooks, populate data according to the lists 
![HashMaps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrhn5u3zm0wtd4wv3p8o.png)

#ALGORITHMS
They are instructions or rules that are formulated in a finite sequential order to solver problems and get results. They provide pseudo-code for problems and they are not language specific as they can be implemented in any language.
#####How to write an algorithm
1. Figure out what is the exact problem
2. Determine where you need to start
3. Determine where you need to stop
4. Formulate the intermediate steps
5. Review your steps
#####Algorithm Classes:
![Algorithms classes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yps4pzlb6fdlk67z4mr5.png)
Some examples of algorithms include Tree Traversal Algorithms, Searching Algorithms, Sorting Algorithms, etc.