Consider a scenario where you are at your home looking for your socks in a pile of clothes. You can imagine how frustrating it would be if they decided to get a divorce and are now lying at the opposite ends of the pile. It would be damn difficult to find them. This is why you need a sock drawer to store them in a better way at a single place. It is the way of organizing our clothes efficiently. This is roughly what a data structure is. In programming languages, a data structure is the representation of data. If you go through the formal definition it's defined as "Mathematical or Logical representation of data". These generally optimize how a computing process manages information in memory.
These data structures, in particular, specify types of data, and thus which operations can be performed on them while eliminating the need for a programmer to keep track of memory addresses.
In Python the four major data structures are:
Lists are one of the most versatile, useful data types in Python. A list is an ordered collection of arbitrary objects. It is somewhat similar to an array but is a bit more flexible. In Python, lists are defined by enclosing a comma-separated sequence of objects in square brackets.
Some important points to keep in mind:
Lists are mutable in nature that is you can modify a list at any given time wherever the scope.
Lists can be accessed using the index value.
It can be nested to arbitrary depth.
Lists are dynamic. Since the list is mutable it implies items could be inserted and deleted from anywhere in the list.
Tuples resemble lists in many ways as they are sequences just like lists. The only difference is that the items of lists are mutable whereas tuples contain immutable elements. That implies you cannot modify a tuple once it's created. Tuples are represented using parenthesis.
Points to remember:
A tuple is a preferred data type when we do not want to change the data over time.
It is faster to iterate over the elements of a tuple than a list. This is because Tuples are stored in a single block of memory. As they are immutable, it doesn't require extra space to store new objects. Lists are allocated in two blocks: the one that is fixed with all the Python object information and another a variable-sized block for the data.
Elements of a tuple are enclosed in parenthesis.
The first thing that crosses our mind when we hear the word dictionary is a big book that contains the answer to every "difficult" word we ever come across. If you think about it a dictionary had words working as keys and their meanings were the values. But they were alphabetically ordered. In python, a dictionary is an unordered collection of items. The compound data types have only value as an element while a dictionary has a key: value pair.
Points to remember:
Each key is separated from its value by a colon (:), the items are separated by commas. The whole dictionary is enclosed in curly braces.
Keys are always unique within a dictionary while values may not be. (Just like the real one.
When the key is known, dictionaries are optimized to retrieve values.
Let us refresh our memories of high school where we were first introduced to sets and our first reaction was "does this even need a revision?" You might remember the Venn diagram.
So mathematically speaking, a set is a well-defined collection of distinct objects. It can be beneficial in programming as well to group the objects into a set. Python provides a built-in set type to do so. In Python, Set is a collection of unordered and unindexed items. Sets are written with curly brackets.
Points to remember:
Sets are un-ordered and unindexed
All the elements in a set are unique. Generally speaking, duplicate elements are not allowed.
The elements contained in the set must be of an immutable type while a set itself may be modified.