What Is Data Structure In Python?

Balasaheb Garule

8 months ago

Data Structure in Python | insideAIML
Table of Content
What is Data Structure in Python?
Numbers
  • int
  • float
  • complex
Boolean
String
List
Tuple
Dictionary
Set
Summary

What is Data Structure in Python?

          The data structure is very important to improve the performance and runtime of a program. To update or modify data in the memory is an important part of performance improvement. Selecting the appropriate data structure for the program is very important. In this article we discuss all these data structures.
The data structure is needed for organizing, managing and storing data for easy access and perform operations.
following data structures in python is available:
They are built-in data structure in python.
We can also use a user-defined data structure in python. These data types are defined using built-in data structures. Those are as follows
  • Array 
  • Stack
  • Queue
  • Hash Map
  • Tree
  • Linked list
  • Graph

Numbers

          The numbers data structure in Python is immutable.In python, there are following numeric data types are available
  • int
  • float
  • complex

int

          The integer data type represents whole numbers. A number is positive or negative, the length of the number is unlimited. We can declare a very large integer value using int type.
e.g.    
n = 10000000000000000001

print(type(n))
Output 
<class 'int'>

float

          float is a decimal data type. It will be used for declaring floating-point values. Values may be positive or negative.
e.g. 
fp = 3.14

print(type(fp))
Output
<class 'float'>

complex

          The complex number contains two parts: real and imaginary parts with j.  We can access the real and imaginary parts with  a.real and  a.imag , respectively.
e.g.
c = 3+4j

print(type(c))

print(c.real)

print(c.imag) 
Output
<class 'complex'>

3.0

4.0

Boolean

          The boolean data structure in python has two values. Those are ‘True’ and ‘False’. It is used to check the conditions.
e.g.
b = 0 > 1

print(type(b))

print(b)
Output
<class 'bool'>

False
bool() method is used to convert value into Boolean data type.
e.g.
x = "AI"

y = -0

print(bool(x))

print(bool(y))
Output
True

False

String

          A string data structure in python is a Sequences data type. It is immutable i.e. we do not modify the string content. A string is a sequence of characters. We can define a string using single quotation marks (‘) or double quotation marks (“). 
e.g.
s1 = 'insideaiml'

s2 = "insideAIML"

print(type(s1))

print(type(s2))

print(s1)

print(s2)
Output
<class 'str'>

<class 'str'>

insideaiml

insideAIML
String is a set of characters so we can access string characters as
e.g.
s1 = “insideaiml”

s1[4]
Output
d

List

          The list is a sequence data type of python. We can store different types of items in the list. The list is ordered, mutable, and duplicate values are allowed in the list. To access list items use indexing which starts from 0.
e.g.
list1 = [1, 'two', 3.0, True]

print(list1)

Output

[1, 'two', 3.0, True]
We can also declare a list using the list() method.
e.g.
charlist = list(('i','n','s','i','d','e','a','i','m','l'))

print(charlist)
Output
['i', 'n', 's', 'i', 'd', 'e', 'a', 'i', 'm', 'l']

Tuple

          Tuple in python is a sequence data type. It stores different types of data. It allows duplicate values. Tuple in Python is immutable.
Tuples are written with round brackets. Tuples are hashable, which means we can sort lists of them and they can be used as keys to the dictionary. A Tuple example in python is:
e.g.
month = ("Jan","Feb","March","April",”Jan”) #tuple example in python 

print(type(month))

print(month)
Output
<class 'tuple'>

('Jan', 'Feb', 'March', 'April',’Jan’)

Dictionary

          Dictionary is an ordered data type. The data is stored in the form of key: value pairs. Key is used as an index.
Note: As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
Dictionary is different from python tuples and lists, it use indexing start from 0 and dictionary uses key as index 
Dictionary is mutable and does not allow a duplicate key.
To define a dictionary use curly brackets “{}”.
e.g.
mydict={1:2,2:4,3:9}

print(type(mydict))

print(mydict)
Output
<class 'dict'>

{1: 2, 2: 4, 3: 9}
To access the dictionary use the key as an index.
e.g.
print(mydict[1])
Output
2

Set

          A set is a collection of mutable, unordered unique items. i.e. Does not allow duplicate items. Set is different from python tuples and lists, they allow duplicate values.
Sets are written with curly brackets. Set is different from lists and tuples.
e.g.
numberset = {1,2,3,4,5,6,7,8,9,9,9,8}

print(type(numberset))

print(numberset)
Output
<class 'set'>

{1, 2, 3, 4, 5, 6, 7, 8, 9}
We can declare a set using the set() method.
e.g.
numberset = set((1,2,3,4,5,6,7,8,9))

print(numberset)	
Output
{1, 2, 3, 4, 5, 6, 7, 8, 9}

Summary

          In this blog, we learned about the built-in data types of python like numbers, lists, tuples, sets, and dictionaries. Lists and tuples are ordered data types.
All of these have their pros and cons. It is highly recommended to refer to our blogs on each of those data types to sharpen your skills. 
If you have any doubt about insideAIML’s data structure in the python blog, drop a comment below and we will get back to you.
     
Like the Blog, then Share it with your friends and colleagues to make this AI community stronger. 
To learn more about nuances of Artificial Intelligence, Python Programming, Deep Learning, Data Science and Machine Learning, visit our InsideAIML blog page
Keep Learning. Keep Growing. 
    

Submit Review