Introduction to Data Structures and Algorithms With Python

donaldtweede

Donaldtweede

Posted on February 21, 2022

Introduction to Data Structures and Algorithms With Python

*Understanding Data Structure *

Python has four inbuilt basic data structures which are:
Lists, Dictionary, Tuple and Sets.
To start with:

Python List

Lists define a set of values which are ordered in a sequence of elements enclosed in square brackets, [].The elements in a list can be changed otherwise said to be mutable.

Example
myList = ["Rono", "Python", "Code"]

Lists can also be defined by using the keyword list()
numbers = list()
numbers.append(4)
numbers.append(5)
numbers.append(6)
print(numbers)
[4, 5, 6]

There are set of built-in methods used with lists.
clear() - Removes the elements of a list
copy() - Returns a copy of the list
pop() - Removes the element at the specified position
insert() - Adds an element at the specified position
reverse() - Reverses the order of the list
sort() - Sorts the list
remove - removes the first item with the specified value
count - returns the number of elements with the specified value.

Python Dictionaries

Dictionaries referred to as hashmaps in other languages. It consists of key, value pairs. Key are unique and immutable objects.Each key and value pair are seperated by a colon.
Dictionaries are defined by curly brackets {}.

myDict =
{"name": Rono',
"Age":7,
"gender": 'male'
}

There are also a set of inbuilt python dictionary methods

clear() - Removes all items from a dictionary
copy() - Returns a copy of the dictionary
get() - gets the value of the specified parameter
items() - gets all items of a dictionary in the key value format
popitem() - Removes and returns the last element in the dictionary
update() - updates the dictionary with the key-value pairs
keys() - returns a list containing the dictionary's keys
values() - returns a list of all the values in the dictionary

Python Tuples

Tuples are used to store several items in a single variable.
Tuples are unchangeable unlike lists and dictionaries. Meaning once you create a tuple you cannot alter its contents.
They are also indexed from item [0] like in lists. Therefore you can access the tuple elements.

mytuple = ("cat", "mouse", "cow", "chicken")

Tuples can be accessed through indexing

`mytuple = ("cat", "mouse", "cow", "chicken")
print(mytuple[1])

output:

mouse`

Sets

Sets are used to store multiples elements in a single variable within curly braces. They are unordered, unindexed and unchangeable.

`mySet= {"pineapple", "pawpaw", "apple"}

print(mySet)

{'apple', 'pineapple', 'pawpaw'}`

There are built-in set methods.

add() - Adds an element to the set
clear() - removes all the elements from the set
copy() - returns a copy of the set
discard() - removes the specified item
pop() - removes an element from the set
update() - updates the set with another set
remove() - removes the specified item

Algorithms Include:

Queues

A queue is an abstract data structure that is open on both sides hence use first in first out basis FIFO.
A queue has two ends; front and rear.The two operations involved with queues are enqueue and dequeue.
Enqueue involves inserting items in a queue while dequeue is the process of removing items.

Image description

Methods involved in queues include:
push(item) - used to insert an element to the queue.
pop(item) - used to remove an element from the queue.
get() - used to extract an element from the queue.
empty() - check whether a queue is empty or not.
full() - check whether a queue is full.

Stacks

Stacks is a linear data structure that stores data in a linear manner and an element is connected to the previous and next element. Elements are accessed in a Last In First Out order. The last element to be added is the first one to be retrieved.

Image description

Operations carried out in stacks are:
empty() – Returns whether the stack is empty – Time Complexity: O(1)
size() – Returns the size of the stack – Time Complexity: O(1)
top() – Returns a reference to the topmost element of the stack – Time Complexity: O(1)
push(a) – Inserts the element ‘a’ at the top of the stack – Time Complexity: O(1)
pop() – Deletes the topmost element of the stack – Time Complexity: O(1)

Linked Lists

A linked list is a sequence of data elements which are connected via links.Each link contains connection to another link.
Python does not have linked lists in its standard library.
The first node is also known as the HEAD. It is used as a reference to traverse the list.The last node points to NULL.

Image description

Types of Linked Lists

Singly linked list
Doubly linked list
Circular singly linked list
Circular Doubly linked list

Thank you for reading this article i hope it has been helpfull.

💖 💪 🙅 🚩
donaldtweede
Donaldtweede

Posted on February 21, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related