Python for Data Science : Data Structures
What is data structure?
A data structure is a way for arranging and storing data efficiently, so that operations can be performed easily.
As we have seen in previous blog that everything in python is object. All objects are either mutable or immutable.
So is the case with data structure some are mutable while others not.
Python data structures can be primarily divided into two categories primitive and non primitive .
Primitive data structures are :
- Integers
- Float
- Strings
- Boolean
Integer is used to represent mathematical integer numbers.
Float i.e floating point numbers represent any rational numbers
Strings is a collection of characters and need to be defined within quotes.
Boolean are objects of type true or false . This is quite useful in slicing list and Dataframe.
Operations on these primitive lists
String concatenation can be done by just adding two strings
Non Primitive : Python has inbuilt non primitive data structures.
- Lists
- Set
- Tuple
- Dictionary
- Array
- Files
Lists : They are used to store different objects may be int , float , string anything. [ ] with elements separated by comma is used to define a list
Select any element from list just pass its index ,in python index starts from zero.
Suppose we want second entry of list l ,then syntax for this will be
l[1] , output will be ‘banana’. Now if we want second and third element of the syntax will be list l[1:3]
Set is an unordered collections of unique elements it is defined using { } with elements separated by comma . The basic operations like deletion , length , pop is similar as list. If we want to add an element to given sets say s ,syntax is s.add(value to be added) , for adding multiple values s.update(list of values to be added).
Set union and intersections
Tuple is also an ordered collection of items . () with element separated by comma is used to define a tuple
Other operations are similar to lists ,leave it to the reader to explore.
Dictionary is collection of key value pair , where key has to be unique. {} is used to define dictionary with “key” : “value” separated by comma.