## DEV Community # Introduction to Data Structures and Algorithms with Python

Data structures and data algorithms are integral tools to a programmer. Data structures in Python deal with the organization and storage of data in the memory while the computer is processing it. Python algorithms refers to detailed set of instructions that helps in the processing of data for a specific purpose.

DATA STRUCTURES IN PYTHON
They are a way of organizing and storing data. Data structures are classified into primitive data types and non-primitive data types. Primitive data types include integers, float, strings and boolean. The non-primitive data types include list, tuples, dictionaries and sets. In addition, there is user-defined data structures in Python which are stack, queue, linked list, tree, graph and hashmap.

Primitive Data Structures
These are data structures that stores the data of only one type. Types of primitive data structures:

• Integers: This data type is used to represent numerical data, that is, positive or negative whole numbers without a decimal point. For example, -4, 6, 21.

• Float: Used to represent rational numbers usually in decimal point like 2.673, 1.443.

• String: It is created by a series of characters within '' or"". To concatenate more than two strings, we use '+'. For examples; 'red', 'Zhan'.

• Boolean: Used mainly in conditional expressions taking the form of 'TRUE' or 'FALSE'.

Non-Primitive Data Structures
Type of data structure that can store the data of more than one type.

• Lists: Used to store a collection of data enclosed in [] and separated by , . It is mutable and applicable to append(), remove(), insert() e.t.c

Syntax

``````todo= ['exercise', 'cleaning', 'assignments']
todo.append('eat')
print(todo)
``````

Output

``````['exercise', 'cleaning', 'assignments', 'eat']
``````
• Tuples: Similar to lists but immutable and enclosed n (). It ensures the values enclosed are not manipulated.

Syntax

``````todo= ('cook','clean','code','sleep')
print(todo)
print(todo[0:2])
``````

Output

``````('cook', 'clean', 'code', 'sleep')
('cook', 'clean')
``````
• Dictionaries: Involves a key associated with a value in a key-value pair. The key-value pair are in {} syntax to create a dict. Each key-value pair is separated by a comma, and within a pair the key and value are separated by a colon : Syntax
``````user = {
"name" : 'Liz',
"age" : 25,
"height" : "173"
}
print(user)
print(user['age'])
``````

Output

``````{'name': 'Liz', 'age': 25, 'height': '173'}
25
``````
• Sets: Similar to a list except that it is unordered. It can store heterogenous data and it is mutable. Creating a set enclosed in a curly bracket {}. Syntax
``````set1 = {'Tate', 22, 163, True}
print(set1)
print(set1.pop())
print(set1)
``````

Output

``````{True, 'Tate', 163, 22}
True
{'Tate', 163, 22}
``````

User-Defined Data Structures
Stacks: Linear data structures in Python stored with principle of FIFO or LIFO. Functions related to its operations are pop(), push(), size() etc.

• Queue: Similar to stacks, they are linear data structures but store data based on FIFO principle. Operations related to Queue include Enqueue (adding elements), Dequeue (deleting elements), Front and Rear. Like Stacks, Queues can be implemented using modules and data structures from the Python library – list, collections.deque, and queue.

• Tree: Non-linear data structures in Python. The properties of a Tree are one node is designated the root node, other than the root, every other node has an associated parent node, and each node can have an arbitrary number of children nodes.

• Linked List: Linear data structure that contains a series of data elements joined together via links. They are implemented using the concept of nodes.
Creating a Linked list. Create a Node class containing some data and a pointer Next which will be used to point to the next node.
Syntax

``````class Node:
def__init__(self, data, next=None):
self.data = data
self.next = next
s = Node(5)
print(s.data)
``````
• Graph: Pictorial representation of a set of objects in Python.

• HashMaps:a Hash function generates the address or index value of the data element. The index value serves as the key to the data value allowing faster access of data.

DATA ALGORITHMS IN PYTHON
An Algorithm is a procedure for solving a problem. It describes a sequence of operations then when performed will result in a solution to a problem. In order to analyze algorithms, one needs to develop some tools to understand how algorithms behave as a function of the problem size.

• Simple Recursive Algorithms: Recursion is the process of defining something in terms of itself. Consider a factorial example;
``````def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""

if x == 1:
return 1
else:
return (x * factorial(x-1))

num = 3
print("The factorial of", num, "is", factorial(num))
``````

Output

``````The factorial of 3 is 6
``````
• Divide and Conquer Algorithm: Is the strategy of solving a complex problem by dividing it into smaller sub-problems, solving them and combine to get the desired output. Consider a Fibonacci Sequence. Syntax of Divide and Conquer
``````fib(n)
If n < 2, return 1
Else , return f(n - 1) + f(n -2)
``````

Which differs from the dynamic overall approach of Fibonacci Sequence.

Hopefully, this will give you a kickstart into understanding Data Structures and Data Algorithms. Additional input is welcomed.