In-built Data Structures in Python

Python has some simple built-in types, e.g. int, float, complex, str, bool. It has also some complex built-in types, e.g. List, Dict, Tuple, Set.
List − List is one of the data types in Python. List is a collection of objects, it is ordered and mutable. In Python it is written within square brackets [].
my_list=["car","bus","truck"]
print(my_list)

We can access list items by referring to the index number:
Returns the item at position 1.
my_list=["car","bus","truck"]
print(my_list[1])

Using index number, we can change the item value.
my_list=["car","bus","truck"]
my_list[2] = "van"
# The values aremutable
print(my_list)

We can loop through the List items by using a for loop.
my_list=["car","bus","truck"]
for x in my_list:
   print(x)

Some methods on List
Python has some of built-in methods, we can use these methods on lists.
append()
This method is used to add an element at the end of the list
clear()
This method is used to remove all the elements from the list
copy()
This method is used to return a copy of the list
count()
This method is used to return the number of elements with the specified value
extend()
This method is used to add elements of a list (or any iterable), to the end of the current list
index()
This method is used to return the index of the first element with the specified value
insert()
This method is used to add an element at the specified position
pop()
This method is used to remove the element at the specified position
remove()
This method is used to remove the item with the specified value
reverse()
This method is used to reverse the order of the list
sort()
This method is used to sort the list
Dict − Dictionaries are unordered sets of element and dictionaries are used with their keys not their position. Dictionaries are abstract data type in python. Dictionaries have two parameter one is key and another one is value. Every key is associated with the value, so we can say that Dictionaries are Associative Array.
>>> student = {"Aadrika":001, "Adwaita":009, "Sakya":011, "Sanj":022}
Here we use the student record, all we have to do is to use the name of the student as an index.
>>> student = {"Aadrika":001, "Adwaita":009, "Sakya":011, "Sanj":022}
>>> student["Adwaita"]
009
In these examples, our dictionary is student and we have an ordering in our dictionary. Just like that first element is "Aadrika", the second element is "Adwaita" and so on. But there is no ordering in dictionaries. That is the reason, why the output of the student dictionary, doesn't reflect the "original ordering".
If we want to add element.
>>> student ["Krishna"] = 111
>>> student
student = {"Aadrika":001, "Adwaita":009, "Sakya":011, "Sanj":022,"Krishna":111}

So, initially dictionary is empty then it takes value one by one in incremental process.
Tuples − A tuple is a set of objects in Python. It is separated by commas(","). In terms of indexing, tuple is similar to List. Mainly tuple is immutable. These are also comparable and hashable so easily we can sort list of them and in Python dictionaries tuples are used as a key value.
my_tuple={"car","bus","truck"}
print(my_tuple)

We can access tuple items by referring to the index number.
Return the item in position 1.
my_tuple={"car","bus","truck"}
print(my_tuple[1])

If once a tuple is created, then we cannot change its values. Tuples are immutable.
We cannot change values in a tuple.
my_tuple={"car","bus","truck"}
my_tuple[3] = "van"
# The values are unchangeable
print(my_tuple)

We can loop through the tuple items by using a for loop.
my_tuple={"car","bus","truck"}
for x in my_tuple:
   print(x)

Python has two built-in methods count() and index(). We can use these methods in tuples.
Set − In mathematics, a set is a collection of distinct objects. For example, here suppose 3 numbers, the numbers are 2, 4, and 6 are distinct objects when we considered separately, but when they are considered collectively they form a single set of size three, written {2,4,6}.
In python, set is very useful because, it is a highly optimized method because any particular element is present in the set or not to check easily.
Methods for Sets
1. add(x) Method: If the element is not present in the list then it adds it to the list.
A = {"AA", "BB", "CC"}
A.add("DD") 
->  add DD in A set.

2. union(s) Method: This method returns a union of two sets. For union operation use  ‘|’ operator.
A = {"AA", "BB", "CC"}
B = {"MM", "NN"}
Z = A.union(B)
OR
Z = A|B
-> Set Z will have elements of both A and B

3. intersect(s) Method: This method returns the intersection of two sets. The ‘&’ operator can also be used in this case.
S = A.intersection(B)
-> Set S will contain the common elements of A and B

4. difference(s) Method:  This method returns a set elements which belong to the first set but not of the second set. We can use ‘-‘ operator here.
S = A.difference(B)
OR
S = A – B
-> Set S  will have all the elements that are in A but not B

5. clear() Method: clears the whole set.
B.clear()
-> Clears B set

Operators for Sets
Sets and frozen sets support the following operators