Data Structure in python
The data structure is one of the different and a special type of formate that is used for storing, processing, retrieving, and, organizing the data. In other words, we can say that data structure is one of the techniques used to hold the data and use the data in an effective way, it is about rendering the data elements in terms of relationship, for better organizing and storing the data.
Data structures are used in different field like
1) Artificial Intelligence
2)Graphics
3)Compiler design
4)Numerical Analysis
5)Simulations
6)Database Management System
7)Statistical Analysis Package
8)Opertating System
Types of Data Structure and their uses
In Python it has four data structures, they are
1) List
2) Tuple
3) Dictionary and
4) Set.
List
A ‘list’ one of the data structure in Python, which holds different data in order for example: Just imagine that when you are going to a store to get at least ten or twenty different products, so if you memorized all the name of different products in the mind, you might get confused or forget in-store while shopping, So instead of getting forget or confused you would store all the in a list, whereas in Python List is used to store data in a sequence manner.
In Python, while writing a list you have to write the value inside the square bracket “[]”, which makes the Python interpreter understand that you are specifying a type ‘list’. After creating a list, you can add, remove, and search for any items in the list. Due to the capability of adding and removing the data in a list, it is also known as “Mutable data type”, which means this data can be altered.
Input:
This_is_my_list = []
type(this_is_my-list)
Output:
list
Here you can use “append” to add the data, and you can use “pop” to remove the data.
For example
Here we can write code from the described above example shopping list, we can make a shopping list and add numbers, names of the products for better understanding, we can also store, retrieve and remove data, from the ‘list’.
Here in the below code, we are trying to create a list and find its type.
Input:
shopping_list =[]
type(shopping_list)
Output:
list
Here shopping list is a variable, and [] is denoted to the data structure ‘list’, and ‘type’ is a function that is used to find the data type of the variable.
2) Here we are inserting a value inside the variable ‘shopping_list’
Input:
shopping_list.append(30)
shopping_list.append(‘Mango’)
shopping_list
Output:
[20, 30 , ’Mango’ ]
Here ‘append’ is used to insert an item from the variable ‘shopping_list’. In the list, we can enter the data or an item one by it can’t enter all the items or data at a time.
3) Here we are removing an item from the variable ‘shopping_list’
Input:
shopping_list.append()
Output:
‘Mango’
Input:
shopping_list
Output:
[20, 30]
Important point while inserting a value in the variable in the ‘list’, it will assign the value one by one from the beginning by the indexing value, and while you ‘pop’ a value, it will remove the value from the end to first that why in the above code while you pop the last value ‘mango’ has removed, in the next code you can learn about indexing.
4)Here I will teach you how the computer index is assign and the length of the value.
Input:
shopping_list
Output:
[20, 30]
Input:
Shopping_list[0]
Output:
20
Input:
shopping_list[1]
Output:
30
Input:
shopping_list.pip(0)
Output:
20
Here in the above code for every value, the computer assigns the index from ‘0’ as 0,1,2,3,4,5……….. for each value, you can ‘retrieve’ the data from the middle by using the index number and you can even ‘pop or remove’ the data from the middle by using the index number, And even you can change or replace the value by using the index number, because of its ability to remove the data and retrieve and alter the value in between. ‘len’ is used to measure the length of the value.
Input:
A = [1, 2, 3, 4, 5, 8]
A
Output:
[1, 2, 3, 4, 5, 8]
Input:
len(A)
Output:
6
Input:
A[1]
Output:
2
Input:
A[4] = ‘Hello’
A
Output:
[1, 2, 3, 4, ’Hello’, 8]
Here in the below code, we are trying to create a list and find its type.
Comments
Post a Comment