AdaChime

Posted on

# Introduction to data structures and algorithms

Before we start we are going to define the terms in our topic.
So the first term that needs to be defined there is data, so what is data? Data simply means processed information that is stored in a computer.
Data structures are simply different ways that data can be organized in a computer for efficient use.
Algorithms are step by step methods of solving a particular problem to get an expected output.

Types of data structures

1. List
2. Dictionaries
3. Tuples
4. Sets
5. Queues
6. Stacks
7. Linked lists etc

List is an ordered collection of items that can be altered. List can contain integers(2, 3), strings("hello") etc. A list uses square brackets[] to contain its items.

``````#type of a list
int_list = [1, 2, 3, 5, 7, 3, 9, 2]
print(int_list)

#output
[1, 2, 3, 5, 7, 3, 9, 2]

``````

In python, items can be numbered the first one being 0, the second 1 and so on.
When I say a list can be altered it means you can add an item to it or remove an item from it.
To remove an item we use we either use clear(), pop() or remove() and to add an item to the end of the items we use append().

For example

``````#adding 3 to the end of the list
int_list = [1, 2, 3, 5, 7, 3, 9, 2]
int_list.append(3)
print(int_list)

#output
[1, 2, 3, 5, 7, 3, 9, 2, 3]

#removing 7 from the list using remove
int_list = [1, 2, 3, 5, 7, 3, 9, 2]
int_list.remove(7)
print(int_list)

#output
[1, 2, 3, 5, 3, 9, 2]

#pop is used to remove the nth terms in a list of items. Eg:
int_list = [1, 2, 3, 5, 7, 3, 9, 2]
int_list.pop(2)
print(int_list)

#output
[1, 2, 5, 7, 3, 9, 2]

#It removed 3 which is technically the 2nd term in the list

``````

Dictionaries are unordered collection of item which stores the items in key-value pairs. Dictionary uses curly brackets{}

``````#an example of a dictionary
Student = {
"name" : "Sharon",
"age" : 20,
"country" : "Nigeria"
}

print(Student)

#output
{'name': 'Sharon', 'age': 20, 'country': 'Nigeria'}
``````

Remember python is case and quotation mark sensitive.

Tuples are ordered collection of items that cannot be altered. It uses parenthesis() and the items are separated by commas.
Example:

``````#example of a tuple
my_tuple(1, 2, 3, 5, 7, 3, 9, 2)
print(my_tuple)

#output
(1, 2, 3, 5, 7, 3, 9, 2)

``````

Sets are unordered collection of items that can be altered and has no duplicate item. Set uses curly brackets{}. You can't change the items in a set but you can add to it using the add() fuunction.

``````#example of a set
myset = {1, 2, 3, 5, 7, 3, 9, 2}
print(myset)

#output
{1, 2, 3, 5, 7, 9}
``````

Queues are linear list of items that stores the items in First In First Out (FIFO) manner. In a queue, the first item is removed first.
The insert and delete operations are called enqueue and dequeue. Deletion of an element can take place only at one end called the front and insertion can take place on the other end which is termed as the rear.
Queues can be used to serve clients.

Stacks is a linear list of items that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner. In stack, a new element is added at one end and an element is removed from that end only. The insert and delete operations are often called push and pop.
Using a stack of books for example, the last book that was added to the stack is the first the leaves.

Linked list is a linear set of data elements which is also termed as nodes. To learn more about linked list, click here.

Remember google is your friend.

Thanks for reading!