INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS IN PYTHON
Published: 2022-02-19 11:42:17

## DATA STRUCTURES

In my previous post, we learned about the history of the python programming language and how wrote our first code in python. In this article, we are going to learn about data structures in python.
Data structures are containers that hold a different set of data. They include Lists, Tuples, and Dictionaries.

1. **<u>LISTS</u>**

This is a collection of ordered items.
Items on a list are usually enclosed by the square brackets [] and each item is separated by a comma.
   _Example_ 
We can create a list of days of the week
days_of_the_week = ["Monday","Tuesday","Wednesday"]

**_List manipulation_**
Various inbuilt functions are used to manipulate data in a list.

i. To access an item in the list above
     element[index of the item]
Note: The index of elements begins at 0 so if we want to     print the first element in our days of the week list we use the following code
     print(days_of_the_week[0])

ii. To add an item to a list
     element.append(new item)
This function adds an item at the end of the list

To add an item at a different position we use the following method
      element.insert(index, item)

ii. To delete an item
      element.remove(item to be removed)
      element.pop() - Removes the last item in the list

**NESTED LISTS**
This is a list within a list

     _Example_
days_of_the_week = [["Monday", "Tuesday"],["Wednesday"],["Thursday"]]

To access an item in a nested list, for example, to access "Wednesday" in the above-nested list we use
   days_of_the_week[1][0]
i.e. days_of_the_week[index of the item to be displayed][index of the item]

2. **<u>TUPLES</u>**

Tuples are a collection of ordered items, however, they differ from lists in that the information within tuples cannot be changed. The other difference is that items within a tuple are encircled within parenthesis ().

    _Example_
    days_of_the_week = ("Monday", "Tuesday","Wednesday")

3. **<u>DICTIONARY</u>**

This is a collection of key, value pairs.
The items in a dictionary are usually enclosed within curly brackets and are separated by a comma.
 
   _Example_
days_of_the_week = {"day1":"Monday", "day2":"Tuesday"}
In the above example "day1" is the key while "Monday" is the value

**_Key value pair manipulation_**

i. To add items
   days_of_the_week["day3"] = "Wednesday"
    Will add a new key-value pair to the above dictionary.

ii. To access value in a dictionary
    print(days_of_the_week["day2"])
    The above code will display "Tuesday" to the user

3. **<u>SETS</u>**

This is an unordered list of items that do not allow duplication of elements.

There are two ways to create a set;
a. Using the inbuilt set function
element = set()

   _Example_
days_of_the_week = (("Monday", "Tuesday","Wednesday"))

b. A set can be defined using the curly braces
   _Example_
days_of_the_week = {"Monday","Tuesday","Wednesday"}

 4. **<u>OTHER DATA STRUCTURES</u>**

i. **_Stacks_** - This is a linear data structure that uses the last in first out (LO-FO), this means that when an element is added at one end an element can only be removed from that end

ii. **_Queues_** - They are similar to stacks however they are based on the First in/First our principle (FI-FO). The element that is least added is removed first.

To learn more on stacks and queues check out https://www.javatpoint.com/python-stack-and-queue 

##ALGORITHMS
This is a set of instructions that are executed to get the solution to a given problem.

1. if 

This checks if a certain condition is met and gives the desired outcome

  _Example_

```
age = 16
if age > 18:
   print("You are an adult")
```
The above checks whether the person has achieved the age of 18.

2. if.....else

This checks if a condition is met, if the condition is not met, the program gives an alternative outcome

 _Example_

```
age = 12
if age > 18:
   print("You are an adult")
else:
   print("You are not an adult")
```

3. if....else if

This checks for multiple conditions.

 _Example_

```
age = 12
if age <13:
   print("You are a baby")
else if age <20:
   print("You are a teenager")
else:
   print("You are an adult")

```
4. For loops

This is used to iterate over a sequence. This sequence can either be a list, a tuple, or a dictionary.

_Example_

```
days_of_the_week = ["Monday","Tuesday","Wednesday"]
for item in days_of_the_week:
   print(item)
```
The outcome of the above code will be
Monday
Tuesday
Wednesday

5. While loops

This is used to iterate over a sequence until a certain condition is met

   _Example_

```
days_of_the_week = ["Monday","Tuesday","Wednesday"]
count = 0

while count <4:
  for item in days_of_the_week:
      print("item")
count += 1
```
The above code will run until it reaches a count of 4. And it will loop through the list and give an output of the items contained in the list.

For more on data structures and algorithms check out
https://www.w3schools.com/python/

_**NOTE:**_ Indentations errors are common when dealing with algorithms. 

_Wrong indentation_

```
for item in items:
print(item)
```
_Correct indentation_

```
for item in items:
    print(item) 
```
* Its also important not to forget the colon

For conventions check out
https://www.datacamp.com/community/tutorials/pep8-tutorial-python-code