Welcome Python-ers !! 😀 Lets get started with Python Data Structures. Here we will talk about what are Data Structures in Python ? How these works ? and how are these used ? Also, If you haven’t seen the previous post, you should surely give it a Look !!
1. List : (This could help you make your shopping list too ;D )
Imagine you have to purchase an article, what will you use to store it ? A variable will solve your problem. But what if you have 10 articles to be purchased ? Well, in that case will you use 10 variables ? Also, if the number of articles becomes 100 ? Thus, List gives us a new way to store these sought of large count of articles, under a same roof.
List is a collection that is used to store a number of values.(this could be int, float or any other data type). It is as simple as creating an Array and storing values in it. Let’s see the way to use it.
my_shopping_list=["clothes","milk","eggs"]
It could be a simple list of Integers. like –
integers=[1,2,3,4,5]
Now, how to get the values from these lists, for that we use Index. The first element has the index 0, the second one has its index 1 and so on.
The code in Python used to get the elements of the List is :
my_shopping_list=["clothes","milk","eggs"]
print(my_shopping_list[0]) #clothes
print(my_shopping_list[2]) #eggs
Also, to add the elements in the List, we have a method append, this is used in Python as :
my_shopping_list.append("vegetables") #my_shopping_list=["clothes","milk","eggs","vegetables"]
Here the “vegetables” is added to the my_shopping_list, after the last element of the given list. It is super simple to use append by giving the element to be added as the parameter to the append method.
2. Dictionary : (give me the Key, I will give you the Value 😀 )
We have learned about indexing on Lists via numbers but what if we don’t have to use numbers, like if we want to store the quantity of the element along with the name of the element of my_shopping_list; for these purposes we can use the Dictionary Data Structure.
Dictionary is a collection of key – value pairs. This can be visualized as :
dictionary_ex={
"key1" : "value1",
"key2" : "value2"
}
#Example of Dictionary
details_dict={
"name":"Sheena",
"nickname":"Shiya"
}
#to print dictionary element
print("My name is %s" %(details_dict["name"])) #this prints the name saved in dictionary
The dictionary values are accessed by the keys and the advantage is; it can have any data type element as its value.
3. Tuple : (You can’t change me, because I am a Tuple 😀 )
Tuples are similar to the Python lists except they are immutable i.e. they can’t be changed. Tuples are represented by Parenthesis. Growing and Shrinking of Tuples doesn’t take place as they can’t be changed.
Tuples are used where functions require a fixed set of values which are not to be changed. Tuples can be visualized as :
integer_tuple=(0,1,2,3)
text_tuple=("mango","Sheena","eggs")
#to print element of tuple
print("I like %s"%(text_tuple[0]))
here the tuple element is printed, this is done same as the list elements are addressed and printed; by the help of Index.
4. Set : (I am always unordered 😀 )
Sets are unordered collection of unique objects. Sets are iterable, mutable and supports no duplicate elements. Sets use parenthesis, for it’s representation. It also uses operations like Union(|), intersection(&), difference(-) etc.
Sets are used over lists whenever we need to check whether a specific element is present in it or not. Sets can be visualized as :
set_1=(set(["a","b","c"]))
but we cannot address elements of Sets through index, as there is no order in a Set.
However, we can loop through in every built-in Data Structure of python. These Data Structures are quite useful in building Python Applications, Competitive Programming etc. and are also used to write Program with a limit to lines of code.
So, That’s it for the day !! 😀 Till then have fun keep learning and keep coding !! 😀

