This post only contains an introduction to the built in data structures within python.The rules for defining and the methodology for using it will be defined in other posts.
Before moving forward with the details we need to understand what data structure is and why it is important in programming.
Formal definition of a data structure:
Why data structure is important in programming?
A solution to a problem in programming involves taking an input and providing the solution of the output. The intermediate process where the problem resolution is done involves the use of algorithms and data structure. Efficient use of the memory to store and retrieve the data is realized with the help of data structure.
If we compare with real world, we do employ similar strategies like using stack to store the dishes in the shelves, stacks of goods in warehouses, list of items or task in to do lists, queue of people at a bank or a bus stop.
Data structure are every where, but today we going going to limit our discussion to the built-in data structure within python only.
List of built-in data structures within python are:
1.List
2.Dictionary
3.Sets
4.Tuples
5.String
6.Frozensets
Lets see the definition of each data structure.
1.List
Definition:
"Lists are mutable ordered and indexed collections of objects. The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.)"[3]
Example of list in python:
In the snippet above it can be seen that the lists in python are created using square brackets. The list can contain different types of objects in one list too.
Next, we have
2.Dictionary
Definition:
"A dictionary is a sequence of items. Each item is a pair made of a key and a value. Dictionaries are not sorted. You can access to the list of keys or values independently."[3]
Example of dictionary in python:
From the code snippet it can be seen that the dictionary is a key value pair and is declared in python using curly brackets. Also keep in mind that the keys in python should be unique otherwise there will be problems while inserting duplicate keys.
3.Sets
Definition:
" A set is an unordered collection with no duplicate elements."[3]
Example of Sets in python:
Curly braces or the set()
4.Tuple
Definition:
"Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the string and integer together). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dictionary instance). "[3]
Example of tuple in python:
Tuples in python can be declared in python using round brackets. The objects inside tuples are immutable. If you try to change the objects inside the tuple you will get error during the process.
5.String
Definition:
"A string is a collection of characters enclosed within quotes"[3]
Example of String in python:
The above given code snippet contains a string which contains a sequence of characters enclosed within quotes,which can be single or double.
6.Frozenset
Definition:
Sets are mutable, and may therefore not be used, for example, as keys in dictionaries.
Another problem is that sets themselves may only contain immutable (hashable) values, and thus may not contain other sets.
Because sets of sets often occur in practice, there is the frozenset type, which represents immutable (and, therefore, hashable) sets.[2][3]
Example of frozensets:
Frozenset helps to create a set inside set. Also the dictionary can contain these frozenset as a key.
For more details refer the reference.
That's all for this post!!
Thank you for reading this post.
If you have any suggestions regarding the post contents or if you need some more details on any other topic, please post it in the comments section.
Your suggestions are too valuable so they should not be missed.
References:
1. https://www.quora.com/Why-are-data-structures-and-algorithms-so-important-in-computer-science
2. https://docs.python.org/3/tutorial/datastructures.html
3. https://python-reference.readthedocs.io/en/latest/docs/list/
4. https://www.tutorialspoint.com/execute_python_online.php
5. https://en.wikipedia.org/wiki/Data_structure
Before moving forward with the details we need to understand what data structure is and why it is important in programming.
Formal definition of a data structure:
"In computer science, a data structure is a data organization, management and storage format that enables efficient access and modification.More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data."[5]
Now we know what data structure is.Next, we will see its importance in programming.
A solution to a problem in programming involves taking an input and providing the solution of the output. The intermediate process where the problem resolution is done involves the use of algorithms and data structure. Efficient use of the memory to store and retrieve the data is realized with the help of data structure.
If we compare with real world, we do employ similar strategies like using stack to store the dishes in the shelves, stacks of goods in warehouses, list of items or task in to do lists, queue of people at a bank or a bus stop.
Data structure are every where, but today we going going to limit our discussion to the built-in data structure within python only.
List of built-in data structures within python are:
1.List
2.Dictionary
3.Sets
4.Tuples
5.String
6.Frozensets
Lets see the definition of each data structure.
1.List
Definition:
"Lists are mutable ordered and indexed collections of objects. The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.)"[3]
Example of list in python:
In the snippet above it can be seen that the lists in python are created using square brackets. The list can contain different types of objects in one list too.
Next, we have
2.Dictionary
Definition:
"A dictionary is a sequence of items. Each item is a pair made of a key and a value. Dictionaries are not sorted. You can access to the list of keys or values independently."[3]
Example of dictionary in python:
From the code snippet it can be seen that the dictionary is a key value pair and is declared in python using curly brackets. Also keep in mind that the keys in python should be unique otherwise there will be problems while inserting duplicate keys.
3.Sets
Definition:
" A set is an unordered collection with no duplicate elements."[3]
Example of Sets in python:
Curly braces or the set()
function can be used to create sets. The set contains no duplicate entries which is much evident in the code snippet when we declare the set using set function. The word alacazam contains repeating alphabets but when the set is printed it contains only unique alphabets.4.Tuple
Definition:
"Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the string and integer together). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dictionary instance). "[3]
Example of tuple in python:
Tuples in python can be declared in python using round brackets. The objects inside tuples are immutable. If you try to change the objects inside the tuple you will get error during the process.
5.String
Definition:
"A string is a collection of characters enclosed within quotes"[3]
Example of String in python:
The above given code snippet contains a string which contains a sequence of characters enclosed within quotes,which can be single or double.
6.Frozenset
Definition:
Sets are mutable, and may therefore not be used, for example, as keys in dictionaries.
Another problem is that sets themselves may only contain immutable (hashable) values, and thus may not contain other sets.
Because sets of sets often occur in practice, there is the frozenset type, which represents immutable (and, therefore, hashable) sets.[2][3]
Example of frozensets:
Frozenset helps to create a set inside set. Also the dictionary can contain these frozenset as a key.
For more details refer the reference.
That's all for this post!!
Thank you for reading this post.
If you have any suggestions regarding the post contents or if you need some more details on any other topic, please post it in the comments section.
Your suggestions are too valuable so they should not be missed.
References:
1. https://www.quora.com/Why-are-data-structures-and-algorithms-so-important-in-computer-science
2. https://docs.python.org/3/tutorial/datastructures.html
3. https://python-reference.readthedocs.io/en/latest/docs/list/
4. https://www.tutorialspoint.com/execute_python_online.php
5. https://en.wikipedia.org/wiki/Data_structure

Comments
Post a Comment