Introduction to Data Structures and Algorithms With Python
LAMBERT KWIZERA
Posted on February 21, 2022
This article deals with introducing data structures and algorithms with Python. Data structure is simply a place or location where data are stored and organized. It is a way of arranging data on a computer to facilitate the access and updating of those data. There are two types of data structure: Linear data structure and non-linear data structure. Algorithm is a collection of ways, steps or stages to go through to solve a particular problem. Algorithms are instructions designed and well established or set of inputs created to produce a desired output.
Let us have an example of data structures with Python
Generally, for storing and access to data, these structures called list, dictionary, tuple and set are very helpful. For full control over their functionality in python, the most important data structures are Stack, Queue, Tree, Linked List and others. These structures can be found in other programming languages.
It is important to recognize that there are two kinds of data structures designed to implement in Python:
Built-in Data structures and User defined data structures.
Built-in data structures include: List, Tuple, dictionary and set
User defined data structures: Stack, Queue, Tree, linked list, graph and hashMap.
Built-in data structures
These structures help make programming easier because they were constructed in Python. They help find a solution quickly.
1. Lists
There are designed to store data of different kinds in a sequential way. They have indexes, that can positive or negative, to facilitate the access.
Example:
To create a list, we use a square brackets and put any elements accordingly. If not, you get an empty list as the output
If you want to add an element, you use using the append(), extend() and insert() functions.
Example:
- The append() function adds all the elements passed to it as a single element.
- The extend() function adds the elements one-by-one into the list.
- The insert() function adds the element passed to the index value and increase the size of the list too.
If you want to delete elements:
- To delete elements, use the del keyword which is built-in into Python but this does not return anything back to us.
- If you want the element back, you use the pop() function which takes the index value.
- To remove an element by its value, you use the _remove() _function.
If you want to ACCESS the elements: It is similar to strings in python. You use the index values and hence can obtain the values as needed.
- The len() function returns to us the length of the list.
- The index() function finds the index value of value passed where it has been encountered the first time.
- The count() function finds the count of the value passed to it.
- The sorted() and sort() functions do the same thing, that is to sort the values of the list. The sort() has a return type whereas the sort() modifies the original list.
*2. Dictionaries *
The dictionaries store key-value pairs. To create a dictionary, you use the flower braces or use the dict() function. You will have to add the key-value pairs whenever you work with dictionaries.
To change the values of the dictionary, you use the keys. You access the key and then change the value accordingly. To add values, you add another key-value pair as indicated below.
- To delete the values, you use the pop() function which returns the value that has been deleted.
- To retrieve the key-value pair, you use the popitem() function which returns a tuple of the key and value.
- To clear the entire dictionary, you use the clear() function.
*How to access elements *
You use keys only. You can use either the get() function or just pass the key values and you will be retrieving the values.
There are other Functions
These are the: keys(), values(), items() _functions
3.Tuples
Tuples are similar to lists are with the exception that the data once entered into the tuple cannot be changed no matter what.
To create the tuple, you use the _tuple() function
To access the elements is similar to accessing values in lists.
To append the values: To append the values, use the ‘+’ operator which will take another tuple to be appended to it.
There are other functions that are similar to those of the lists.
4.Sets
Sets are a collection of unordered elements that are unique. To create the sets, using the flower braces but instead of adding key-value pairs, you just pass values to it.
With sets, you can operations, such as union, intersection and so on. You do use these following functions:
- The union() function combines the data present in both sets.
- The intersection() function finds the data present in both sets only.
- The difference() function deletes the data present in both and outputs data present only in the set passed.
- The symmetric_difference() does the same as the difference() function but outputs the data which is remaining in both sets.
User-Defined Data Structures
This type of data is not supported by python but can be programmed to reflect the same functionality using concepts supported by python are user-defined data structures.
Data structure that can be implemented this way: Linked list, Stack, Queue, Tree, Graph and Hashmap
Linked list
It is a linear data structure, in which the elements are not stored at contiguous memory locations.
An example:
Stack
It is the linear structure that permits data to be inserted and removed from the same end thus follows a last in first out (LIFO) system. Insertion and removal or deleting is known as push() and pop() respectively.
** Queue**
It is a linear structure that allows insertion of elements from one end and deletion from the other. It follows, First In First Out(FIFO) methodology. The end is called the front of the queue. The other end is the rear end of the queue.
Tree
A tree is a non-linear but hierarchical data structure. Trees are useful for storing data that aren’t linearly connected to each other but form a hierarchy.
Example:
Graph
A Graph is a non-linear data structure consisting of nodes and edges. The nodes called as vertices and the edges are lines or arcs that connect any two nodes in the graph. A Graph consists of a finite set of vertices(or nodes) and set of Edges which connect a pair of nodes.
Hashmap
Hash maps are indexed data structures. They use a hash function to compute an index with a key into an array of buckets or slots.
Categories of algorithms in python
From the data structure point of view, following are some important categories of algorithms
Search − Algorithm to search an item in a data structure.
Sort **− Algorithm to sort items in a certain order.
_Insert*_ − Algorithm to insert item in a data structure.
*Update** − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.
Conclusion
Data Structures help store and organize data in python and algorithms are instructions designed to produce desirable outputs.
Posted on February 21, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.