what are Data structures
Data Structures are a remarkable way of organizing, managing and storing data in computers in such a way that we can perform operations on the saved data very efficiently.
In this article, I will briefly explain the commonly used data structures in python every programmer must know.
There are four types of built-in data structures in Python offers: list, tuple, set, and dictionary.
List
A list is well defined using square brackets which holds data that is separated by commas. A list is mutable and ordered. It can contain a mix of different data types.
# declaring an empty lists
lists = []
# initializing and defining lists with different data types
list_Numbers = [1, 2, 3, 4, 5]
# initializing and defining lists with string data types
list_string = ["Bale", "Kane", "Sterling"]
# initializing and defining lists with mixed data types
mix_list = [9, 4, "Anne"]
Now lets try to access the data stored in the list using their index numbers, which begins count from 0 i.e. the first element in the list.
open your python shell console
>>> print the first element
>>> lists = [1, 2, 3, 4, 5]
>>> print(lists[0])
1
>>>
>>> print(lists[0:3])
[1, 2, 3]
>>>
>>> # prints the last element in the list
>>> print(lists[-1])
>>> # sorting the list in ascending order
>>> lists = [2, 7, 9, 3, 1]
>>> lists.sort()
>>> print(lists)
[1, 2, 3, 7, 9]
>>>
>>> # sorting the list in descending order
>>> lists.sort(reverse=True)
>>> print(lists)
[9, 7, 3, 2, 1]
>>>
since list are mutable, we can perform addition and deletion of element in list.
to add more elements we can use these functions: append(), extend(), and insert().
to delete elements, we can use del(), pop() and remove() function
Tuples
A tuple is a data type for storing immutable ordered sequences of elements. tuples are Immutable because you can neither add nor remove elements from tuples, or sort them in place unlike lists which are mutable.
>>> # declaring an empty tuple
>>> emptY_tuple = ()
>>> print(empty_tuple)
()
Set
Set is a mutable and unordered collection of unique elements. It can permit us to remove duplicate quickly from a list.
Dictionaries
Dictionary is a mutable and unordered data structure, used to store data. It permits storing a pair of items which are in keys and values.
>>> Dict1 = {1: 'Hello', 2: 'To', 3: 'You'}
>>> print(Dict1)
{1: 'Hello', 2: 'To', 3: 'You'}
User-Defined Data structure
stacks
A stack is a LIFO (Last In First Out — the element placed at last can be accessed at first) data structure which can be commonly found in many programming languages. This structure is named as “stack”. example of stack is in
Operations carried out on stack are push and pop.
push is used in inserting an element into the stack
pop is used in deleting an element in the stack
Queues
Queues is a also a linear data structure which uses First-In/First Out(FIFO) to store items.
Operations that can be performed on the queue are:
Enqueue - which is used to add item to the queues
Dequeue - It removes an item from the queue
Front - the front item from the queue
Rear - the last item from the queue
Binary Trees
Trees are also non-linear data structures that represent nodes connected by edges. Each Tree consists of a root node from which we can access each element of the tree.
There are other data structures which can be implemented linked list, graphs and hash tables.
Algorithms
Algorithm is a step-by-step course of action, which defines a set of instructions to be executed in a certain order to get the desired output. Algorithms are generally created independent of underlying languages
the common operations that can be done are:
- Search
- Sort
- Insert
- Delete
- Update
There are three main approaches in solving an algorithm
Greedy Algorithm
Dynamic Algorithm
Divide and conquer Algorithm
References
- https://www.google.com/url?sa=i&url=https%3A%2F%2Fintellipaat.com%2Fblog%2Ftutorial%2Fpython-tutorial%2Fdata-structures-with-python-cheat-sheet%2F&psig=AOvVaw3cDRoqhfQgVk_oGdzqPnJl&ust=1645563970045000&source=images&cd=vfe&ved=2ahUKEwiFyLHQ2ZH2AhU8_rsIHenHDpcQr4kDegUIARCnAQ
- https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.programiz.com%2Fdsa%2Fstack&psig=AOvVaw2T5OGqPtqkDe7BuB325GD6&ust=1645568362317000&source=images&cd=vfe&ved=2ahUKEwi2yeT-6ZH2AhU9i_0HHVIMAX0Qr4kDegUIARC7AQ
- https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.delftstack.com%2Fhowto%2Fpython%2Ftrees-in-python%2F&psig=AOvVaw1Nzy6BTVm4IlHi13UlVBGX&ust=1645569306571000&source=images&cd=vfe&ved=2ahUKEwjIn4XB7ZH2AhWSD-wKHYXTC58Qr4kDegUIARCGAg
Top comments (3)
thanks for helping
Thanks for sharing!
nice contents! thanks