DEV Community

tarakuds
tarakuds

Posted on

Data Structures and Algorithm

Data structure

This explains how data is organized and stored when been processed so as to ensure that it is accessible and stored efficiently without loosing accuracy

Algorithm

This describes the flow / set of instruction required to process data for a specific purpose.This deals with certain logic to solve problems.

In python, the following data structure exists

  1. Lists
  2. Tuples
  3. Dictionary

These three are somewhat synonymous to an Array as commonly described by other programming languages. However, they differ in a few little ways.

Don't get confused, I'll explain.

Lists
they are an array that can contain the various data types as an element. They can however be modified and can be accessed using their number indexes using the form (where the first index starts from 0 and the last n-1)

Example

list1 = ['physics', 'chemistry', 1997, 2000, "c", "d"]

print(list1[0]) 
#output : physics

Enter fullscreen mode Exit fullscreen mode

You can perform a few additional operations such as append()-add list

Tuples
In the case, they are also an indexed array but are immutable (that is cannot be changed or are said to be constant through the entire program) once declared. This however doesn't make them useless, they can be used and accessed when a need arises.

Example
Even though immutable can be concatenated

tup1 = (12, 34.56);
tup2 = ('abc', 'xyz');

#concatenation
tup3 = tup1 + tup2;

print tup3;

#output will be - (12, 34.56, 'abc', 'xyz')

Enter fullscreen mode Exit fullscreen mode

Dictionaries
Here they are stored and accessed using key-value pairs. Just like an object in other programming languages.

Example

dict = {
        'Name': 'Zara', 
        'Age': 7, 
        'Class': 'First'
       }
print "dict['Name']: ", dict['Name'] 
#output  - Zara

print "dict['Age']: ", dict['Age'] 
#output - 7

Enter fullscreen mode Exit fullscreen mode

Other types of data structures includes

Set
They are mutable collection of data. They are useful as they dont allow for duplicates. Note that a frozen set is immutable. They remain the same at all times

Linked List
As the name implies, its elements are linked using pointers. Therefore, they have no contiguous memory location for storage. the structure is as follows, the first node is called the ** Head **. Each node contains a data and a pointer.

Stack
This is also another linear data structure storing items in a particular manner. Last-In/First-Out or First-In/Last-Out. Here, as an element is added one one end, the element comes out from that same end. simple explanation for this is when you pile books, to access any of the book, you need to remove from the ones above till you get to you choice book. removing the book you need suddenly can scatter the whole structure.

Queue
It stores items in a First-In/First-Out manner. Just as the name implies, when on a bank or coffee shop queue, the first customer to arrive will be the first to be attended to.

Tree
This is a data structure arranged according to their hierarchy, from the root to the least node.

KEYS

Element: each item stored in an array
Index: location of an element stored in an array
concatenation: combining more than one variable to one

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.