
Every one of us in our data science journey is inclined towards learning Python: The Python Programming Language. Why so? Majorly for the Predictive Analytics part of analytics which can be done in Python through 3 ways namely using the Statistical Methods, Machine Learning Methods and Deep Learning Methods depending upon the complexity of the business problem at hand. When it comes to learning to code in the language we can’t miss learning about the Data Structures here as they give us a way to store the data we will be using for our analysis. Every tool/language has its own set of data types and data structures and in the case of Python we have various data types and data structures however there are seven major data structures in Python. We will be covering all those 7 in this article. Let’s dive right into our discussion!
A data structure is a method of organizing, storing, and handling data in a computer to facilitate efficient access and manipulation. It establishes the connections between data and the actions that can be executed on that data. Commonly used data structures, such as arrays, linked lists, stacks, queues, trees, graphs, and hash tables, serve various purposes in computer science. They are categorized as linear data structures or built-in data structures, each offering distinct advantages and drawbacks based on the nature of the data and the required operations.
In python, we have Organizing data types and their programming languages like str, float, int, bool etc. which help us know the type of the variables we store our information/data in. Further, we can club and store these variables having the same or different data types into a structure known as a data structures. Now depending on the characteristics and the nature of these data structures, we have several of them.
While some data structures are inbuilt i.e. are available in the basic python(example: tuple), some require the installation of specific packages(example: DataFrame). Also, we can also create our own data structures. Such data structures are known as User Defined Data Structures. Some of the most common data structures we can create are Stack, Tree, Queue, Linked List, Graph etc.
In today’s article we will be talking about the following data structures in detail:
We would not be taking the User Defined Data Structures in today’s discussion.
Let us start our discussion with the basic data structures in Python which are inbuilt in Python and do not require any external libraries/packages to be installed first in order to use them. These are Tuple, List, Dictionary and Set. They have few properties in common.
Let’s clear up some of the confusion first!
In python, we store the individual values in variables of various data types.
For example, var1 = 20 so variable var1 contains an integer value 20 and var1 is known as an object of int type or int data type.
We store multiple values in data structures. In this case, the variables having multiple values are the objects of different classes.
For example: var2 = [1,2,3,4,5] so variable var2 contains a list(a data structure) and var2 is known as an object of the list class.
Don’t worry about it! These concepts will be clear by the end of this article.
In general, a tuple is a finite ordered list of elements. Since we are dealing with Tuple as a data structure in Python its elements are variables/values of different data types. We can also give a data structure as an element in a tuple. Let’s start slow and deal with the creation of tuples in Python.
We can use different ways to do so!
Method 1: Without using parentheses ()
tuple1 = 1,2,3,4 tuple1
type(tuple1)
Method 2 : Using parentheses ()
tuple2 = (1,2,3,4) tuple2
type(tuple2)
Here we took homogeneous data for creating a tuple i.e are integer elements. Let’s try with heterogeneous data as well taking elements with integer, float, string and bool data types.
tuple3= (1,5.675,'Tuple',False) tuple3
print(type(tuple3))
We can also pass a tuple inside a tuple. This process is known as the nesting of tuples.
tuple4= (1,5.675,'Tuple',False,tuple3) tuple4
Not just tuples we can also store other data structures like List, Set and Dictionary inside a tuple, however, we will discuss this later in the article.
Method 3 : Using a type conversion function – tuple()
In this method, we convert some other data structure or the range object into a tuple using a function.
tuple5 = tuple(range(10,51,10)) tuple5
print(type(tuple5))
Note: Whenever it comes to accessing the data stored in a data structure i.e taking out the elements of a data structure we use square brackets [ ]. Also here we need to know about a term called index. In all the data structures the index starts from 0.
To take out a