Introduction to data structures and algorithms with python
Tags: python, algorithms
Published: 2022-02-20 18:58:13

This article will analyze data structures and how they are implemented in python. 
Towards the end I will also analyze a few sample algorithms. 

## List
A list is a mutable and ordered data structure that is defined using the [] brackets. A list can contain items of different data types. 

```
languages = ['CSS', 'Java', 'JavaScript', 'Python', 'Ruby', 'C++',
             'PHP', 'SQL', 'Pascal', 'Swift']
#add a new element to the end of the list
languages.append('Kotlin')
print(languages)
#access element on index 0
print(languages[0])

```

>

```
 Output : ['CSS', 'Java', 'JavaScript', 'Python', 'Ruby', 'C++', 'PHP', 'SQL', 'Pascal', 'Swift', 'Kotlin']
CSS
```

## Tuple
A tuple is an immutable data structure that contains a sequence of elements. The elements cannot be altered through adding or removing elements.

```
#Creating a tuple to store the connection values on a server
#The information cannot be changed in the entire program
host, IP, port = ('Local host', '127.0.0.1', 8080)
print('The connection variables are {} {} {} '.format(host, IP, port))
```

> 

```
Output : The connection variables are Local host 127.0.0.1 8080
```

## Set
A set is a collection of unique elements. It is unique and unordered. One can remove elements or add unique elements to the set.
 

```
#create a set from list of numbers
numbers = [1, 4, 6, 8, 3, 2, 7] 
num_set = set(numbers)
print(num_set)
#create a list for programming languages
language = {'CSS', 'Java', 'JavaScript', 'Python', 'Ruby', 'PHP'}
print('CSS' in language)
language.add('Kotlin')
print(language.pop())
```

```
> Output : {1, 2, 3, 4, 6, 7, 8}
True
JavaScript
```

## Dictionary
A dictionary stores items in key value pairs. It is mutable and the elements are unordered. 

```
City = {'Kenya':'Nairobi', 'Uganda':'Kampala', 'Ghana':'Accra', 'South Africa':'Pretoria'}
print(City)
```

```
> Output {'Kenya': 'Nairobi', 'Uganda': 'Kampala', 'Ghana': 'Accra', 'South Africa': 'Pretoria'}
```

## Queue
A queue is a data structure that stores elements in a sequential manner. It supports two operations to either dequeue or enqueue elements. The queue has the front end that points to the starting element and the rear end that points to the last element. Elements on a queue are removed based on the FIFO(first in first out) principle. 

## Stack
The stack is the opposite of a queue in that elements are accessed following the LIFO(Last in first out) principle. The last element is removed first. The basic operations on stack are; push, pop, IsEmpty, IsFull.   
## Linked List
A linked list is a data structure where all elements are connected to one another. The connection is made through pointers. To create a linked list we use the node class and pass in the necessary objects. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/unyo03kkbru8yl891x4e.png)
 https://media.geeksforgeeks.org/wp-content/cdn-uploads/gq/2013/03/Linkedlist.png
A linked list supports some operations such as traversal, insertion, and deletion. 

## Algorithms
An algorithms is a step-by-step procedure that outlines the instructions that are to be followed when solving a problem. Algorithms are not language specific and can be implemented in any programming language. An algorithms can simply be explained through the many procedures that people follow when doing everyday activities for example when preparing a favorite meal one will always start with getting the ingredients, prepare them, and finally get into the cooking process. Similarly when solving programming problems a programmer will always start by defining the problem, getting necessary inputs, and then following the programming procedure to attain the desired output. Some of the algorithms that can be implemented in python include quick sort, greedy search, and binary search algorithms.