Samwel Njihia

Posted on

# Python 102: Introduction to Data Structures and Algorithms With Python.

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:

###### 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:

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

Results:

``````[]
[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:
``````my_dict = {} #empty dictionary
print(my_dict)
my_dict = {1: 'Django', 2: 'Laravel'} #With elements
print(my_dict)
``````

Results:

``````{}
{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
``````names= ("apple", "banana", "cherry")
print(names)
``````

Results:

``````('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:
``````num_set = {1, 3, 6, 70}
print(num_set )
``````

Results:

``````{1, 3, 6, 70}
``````

Operation in set are Union, intersection, Difference etc

#### Example

``````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:

``````{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

##### Operation in Stacks
###### PUSH

Adds an element at the top of the stack

###### POP

Removes an element from the top of the stack

##### 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.

##### 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.

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.

##### 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.

##### 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

# 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: Some examples of algorithms include Tree Traversal Algorithms, Searching Algorithms, Sorting Algorithms, etc.