Introduction to Data Structures and Algorithms
Published: 2022-02-21 20:34:48

In this post we will go through what data structures are, the different types of data structures and why they are important. We will also briefly talk about algorithms. In this post we shall be using Python as our programming language. But, if you are from a different programming language don't worry, the fundamentals remains the same and still apply to your language. The only difference is the implementation will be different. Alright then, let's get to it!

---
## What is a Data Structure? ##
A data structure is a way of organizing data in a computer memory to facilitate the organization, storage and retrieval of data in an efficient manner. Say you are in the supermarket, you picked your ice cold coke from the chiller and you are about the open for that first thirst quenching and refreshing sip but you remember you have to pay first. You walk to the cashier and find two other customers with their shopping waiting in line. That is an example of a data structure. It is a simple way of organizing and retrieving customers at the cashier. It's called a queue. If you look closely, you will find implementations of data structures everywhere in everyday life. 

Python groups data structures into two main groups:

1. Built in data structures.
2. User defined data structures.

## Built in data structures ##
### Lists ###
List in Python is the same as 'array' in other languages. Also, Python lists are heterogeneous, which means you can add any kind of value to a list. It can be a string, number, boolean, or even a list itself.
```python
listA = [1,2,3,4,5]                 #Create list using square braces []
 
listA.extend([6,7])                 #Extend the list
print(list)
 
listA.append([8,9])                 #Append elements to the list
print(list)
 
listB = [10,11,12]                  #Create another list
 
listoflist = [listA, listB]         #Join two lists
print(listoflist)                   #Print
```

### Tuples ###
A tuple is a data structure that is an immutable, or unchangeable, ordered sequence of elements. Because tuples are immutable, their values cannot be modified. Tuples have values between parentheses ( ) separated by commas ,. Empty tuples will appear as coral = (), but tuples with even one value must use a comma as in:
```python
coral = ('blue coral',)
```
```python
# indexing is from 0
# Same as list apart from tuples are immutable,
# which means you can not change the content
# once the tuple is created.
# Use () instead of []
 
tuple = (2, 4, 6, 7)
 
#print length of tuple
print(len(tuple))
 
#print element at index 2
print(tuple[2])
 
#just like lists you can merge tuples too
a = (1, 2, 3)
b = (4, 5, 6)
merge = (a, b)
print(merge)
```
### Set ###
A set is an unordered collection of unique elements that is mutable. A set is initialized and defined as follows:

```python
set1={1,2,3,4,5}
```
When a value is repeated in the definition of a set, upon printing it, the repeated value is done away with.

Add elements by using the function add()
```python
set1.add(0)
```
### Dictionary ###
In Python programming, the dictionary stores the information with the help of a unique key. Whenever you need that data, call it by the key it was assigned. Here the key is ‘ship1’, ‘ship2’ and the values are the name of captains.

```python
#create the dictionary
captains = {}
 
#add values into it
captains["ship1"] = "Kirk"
captains["ship2"] = "Marcus"
captains["ship3"] = "Phillips"
captains["ship4"] = "Mike"
 
#fetch the data
#two ways to fetch the result from dictionary
print (captains["ship3"])
print (captains.get("ship4"))
```