DEV Community

Cover image for Introduction to data structures and algorithms.
Kouti Divine
Kouti Divine

Posted on • Edited on

Introduction to data structures and algorithms.

Like in every other programming language we need to know about its data structures as well as its algorithms. Python is not an exception.
This article is going to take us through the basics of data structures and algorithms๐Ÿค—.

OBJECTIVES

By the end of this article we should know more about;

1- Definition of data structures and algorithms
2- classification of data structures.
3- Input and output functions , control flow functions.

In order for us to know more about data structures and algorithms we need to first start by their definitions.๐Ÿค“

DEFINITIONS

Data structure:

A data structure refers to a particular way of organizing data in a computer so that it can be used effectively.
Example
Data structures include
Arrays, linked list, integers, float and many others.

Algorithms:

An algorithm is a step by step set of instructions to process data for a specific purpose. Also an algorithm utilizes various data structures in a logical way to solve a specific computing problems. In other words an algorithm refers to a set of well elaborated, step by step instruction used to solve a particular task by the computer.

Image description

CLASSIFICATION OF DATA STRUCTURES.

Data structures are classified into two main types which are,

  • Primitive
  • Non-primitive data structures. Image description

PRIMITIVE DATA STRUCTURES.

They are basic data structures that stores only one type of data.
They include; float, integers, strings/characters, boolean, pointers.
lets now define each of the following so we can have a better understanding of what each means.

Definition

Integers.
They refer to whole numbers ranging from negative infinity to positive infinity. Example of an integer is 2,-3.

Float
They represent floating point numbers. They include fractions which are represented in decimal format. EXAMPLE 2.666666.

Strings/Characters
A strings is a sequence of characters. They can contain any sequence of characters, visible or invisible, and characters may be repeated.
Example 'hello world.'

Boolean/pointers
BOOLEAN
They refer to inbuilt data types which uses two values. Either TRUE or FALSE.
They are interchangeable with 0 or 1.
POINTER
Refers to a variable that hold an address in computer memory.
that is it points to a specific location in memory.

Image description

NON-PRIMITIVE DATA STRUCTURES.

Non- primitive data types are data types which are user defined and can store data of different types in a single entity. They are categorized into two parts which are linear and non linear data structures.

Linear data structures is a sequential type of data structure. Meaning that all element in memory are store in a sequential manner.
Examples are Arrays, linked list, stack, queue.

Non linear data structures is a random type of data structure. Example are trees and graph.

Now lets talk of each of these detailly for better understanding.๐Ÿ˜Š

ARRAYS
In pyhton arrays are not popular as in other programming languages like c, c++, java etc.
In Python arrays are list that carry element of a particular data type. That is they hold elements of the same data type.

LIST
In python , list are easily recognized by square brackets[].
example.

number[]={1, 2, 3, 4}
Enter fullscreen mode Exit fullscreen mode

STACK

It refers to a linear data structure that help user to either add of remove elements from a list.
also it follows the principle of LIFO(last in first out.)
Looking at a basic example of a stack is a a stack of plates. Also looking on the browser the last tab opened is usually the first in the history.
when dealing with stack, we have two operations;
push or pop.
Push is the process of inserting elements onto the stack whereas pop is the process of removing elements from the stack.

Image description

QUEUE

It refers to a linear data structure that follows the FIFO (First in first out). they can be used in CPU scheduling.

GRAPHS
This refers to a non-linear binary data structure.
It's a collection of nodes that have data and are connected to other nodes.
A graph in python consist of

  • collection of vertices.

  • collection of edges represented as ordered pairs of vertices.

TREES
This refers to a non linear data structure in which data items are connected using references in a hierarchical manner.
Each tree consist of a root node from which we can access each element of the tree. Leaf node and internal nodes.
Each node is connected to it's child via a reference which is called edge.

Image description

ALGORITHM IN PYTHON.

As we earlier defined an algorithm is a step by step procedure on how a specific task is to be performed by the computer.

Example
Let us write an algirithm that has to boil water.
1-Put water in a pot and close the pot
2=Put the the pot in the gas cooker
3-Turn on the gas cooker.
4-Once the water has boiled turn off the gas cooker.

Learn more about algorithms here

INPUT OUTPUT FUNCTIONS

Well when we talk of input it mean we are inserting information.
In python, input functions are that which takes data from the user and convert it to a string.
EXAMPLE

name= input('what is your name?')
Enter fullscreen mode Exit fullscreen mode

the above code ask the user to type his/her name
In python the print function is used as the output. that is the print function is that which is used to display information on the screen. and it cannot be modified.
Example.

name= input('what is your name?')
print('my name is', name.)
Enter fullscreen mode Exit fullscreen mode

the above code ask the user to input type his name. when the user is done the computer print the next line

Image description

CONTROL FLOW FUNCTIONS.

control flow is the order in which the programs code executes.
In python this is regulated by conditional statements, loops and functional calls.
there are three main types of control structures that python supports.

Sequential;
The default mode.
Here the execution of the program is done in a sequence that is one after the other.

Selection;
Used for decision and branching. Some include simple if statements, nested if statements, if-elif-else.

Repetition;
Used for looping, iteration or recursion. That is repeating the program over and over till a break.

learn more about control flow statements here

Thank you for reading.
You can connect with me on Twitter through this link
https://twitter.com/DivineKouti?t=Nd9otw4v4XJ3odOjL4MpXA&s=08


Top comments (0)