
1. Describe this Tomato?
A red vegetable, oval shaped, juicy and weighs around 40 grams.
Red is data, vegetable is data, oval is data, juicy is data and 40 is data. Simply put, any information is Data. Any facts or numbers is Data. We usually collect them so that we can analyze them using Logic to understand the world better or make decisions.
We cannot go to a grocery shop and buy a tomato if we cannot describe it using data in the first place. We directly or indirectly use it to make decisions.
We process data to make decisions. This is how logic and computing works.
Without Data there is no computer science and as the name implies, neither Data Science nor A.I. Data is the lifeblood of the A.I. Without data no algorithm works. In this article we will explore what Data is and its different types.
2. Data Types
To brush your teeth you pick up your toothbrush, not other types of brush. In the same way data type is derived from the operations we wish to perform.
If we wish to perform addition, subtraction, multiplication of division we need data types which are Numbers. Numbers come in two variety – integers or floats ( just a fancy name for decimal numbers.
A + B , A-B, A X B , A / B ~ these kind of operations need Numbers data type.
Consider this text : DaTa TYpes can ONLy be defined based on THE CONTEXT or oPErations.
Now as we can see there are lots of uppercase and lowercase mismatch in the above sentence. In order to process this we need a data type called String.
So, the whole sentence is a string : DaTa TYpes can ONLy be defined based on THE CONTEXT or oPErations.
After running the formatting operations we get:
Data types can only be defined based on the context or operations.
Hey will you come to the cinema with me? or say Aati kya Khandala? For questions such as these, the typical output we expect is YES or NO. In order to record such true or false outputs we need Boolean data types. If the answer is yes the value is 1 or when the answer is no the value is 0.
The three basic data types are hence: Numbers, String or Boolean.
Most importantly, Data types cannot be defined in isolation from the operations. We can perform on them hence, when thinking about data types it is important to keep in mind that-
- Data describes the information or defines a value
- They define operations which are allowed on those values.
For example, if the data type is Integers, it can only accept integers as input and following operations can be performed ( +,-,/,*).
3. Abstract Data Types
When we wish to watch a TV, we simply pick up the remote and push the ON button on it, followed by channel numbers and start enjoying. We do not care about the inner workings of the TV or Remote. We are only interested in the high level working of the TV.
At times, analyzing certain things from the distance helps us understand the core concepts without getting into the nitty-gritty. This is called Abstraction. In computer science in order to make things simple for programmers we abstract things. A programmer can simply write his code in human understandable syntax without worrying about machine language in which the processors finally execute it.
In order to process data in different scenarios or use case, we simply use Abstract data types, which are designed and defined for a particular kind of operations. We as programmers don’t need to worry about how the machine stores or processes the data in memory, we just do the implementation.
Below are types of ADT ( Abstract Data Types ) , we will discuss in this article:
- Stack
- Queue
- Lists
- Maps( Dictionaries )
- Sets
4. STACKS
Consider the above deck of cards. A, K, Q, J and 10. In order to arrange the cards like this you will first need to put A on the table followed by K and so on.
Now consider, which operations can we perform on this stack of cards? We can pick up 10 from the stack. But we cannot remove J without removing 10 from the stack. The cards which came before, in this case A, cannot be accessed without accessing all the cards which came after A. It’s kind of FIRST in LAST out.
Secondly, we can put another card say 9 on top of 10.
When we have data which is arranged linearly with operations defined as-
- Push – Push an item on top of the stack
- Pop – Retrieve and remove the item on top of the stack
We call it STACKS. Without going into details on how the values are stored in the memory etc, we simply say Stacks are a kind of Abstract data type that we can use in our applications.
5. QUEUES & PRIORITY QUEUES
Consider the above queue at a ticket counter. Let’s say we have the following operations which are allowed-
The first person in the queue will be served first and anyone joining has to be added towards the end of the queue. It’s FIRST in FIRST out. When we need data to be processed in such a manner we need the abstract data type called Queue. Queue is also a linear list on which we can perform the following operations-
- Enqueue (e) – Add an item e to the back of the queue.
- Dequeue () – Remove the item at the front of the queue.
Now consider the queue of patients at a hospital- Some of them are serious while some are there for a regular check up. In that case, we can define a particular patient with two numbers : (Position in the queue and Severity). For example, patient at position 5 with a severity of 8, assuming we indexed severity on a scale of 1 to 10 in an increasing order. So our patient will be (5,8) . In the light of this new information, we can change the rules of queue processing. We can say the hospital needs to attend to severe patients first despite their position in the queue.
What we did is we basically assigned a priority information to the queue and changed the operation rules. This kind of queue is called a Priority Queue. In such cases enqueued items must have a priority assigned to them and instead of enqueue (e), this type of data has (e,p) operations, where p is the priority assigned to the item e.
6. LISTS
Consider the basket above. It has Apples, Oranges, Onion, Grapes, Capsicum and other veggies. We can simply make a list of veggies in the basket as
[Apples, Oranges, Onion, Grapes, Capsicum,…. ] . Now imagine we can at wish pick up any fruit or veggie and eat it or we can add more veggies in the basket if needed.
If we are allowed to do such operations the data type which is called Lists. We can store any primitive data type in a list simultaneously. Unlike Stack and Queue this gives us the freedom to access, add and replace an item from the middle. We can perform the following operations on a List:
- insert(n, e): insert item e at position n
- remove(n): remove the item at position n
- get(n): get the item at position n
- sort(): sort the items in list
- reverse(): reverse the order of list
MAPS
Recall your school days, you were assigned a roll number and while taking attendance teachers used to call the roll number. Each of your classmates had a unique roll number assigned to them. Similarly, in the case of data, we have a data type which does that. It is called a Map ( dictionary or associated array ). Maps is used to store the mapping between two objects i..e. the key object and the object value. For example, they can be used to store a user ID as number and Key as username as value.
We can perform the following operations on maps:
- set(key, value): add the key-value mapping
- Delete (key): remove the key and its associated value.
- Get (key): retrieve the key that was associated to the value.
SETS
A Set allows you to store unique non-repeated values. They are like mathematical sets. When the order of the data is not important, you can use a set. For example, a set of prime numbers or a set of animals. Supported operations on sets are:
- add(e): add an item to the set or produce an error when the item is already in the set,
- delete(e): delete an item from the set,
- list (): list the item in the set.
summary
Do follow our LinkedIn page for updates: [ Myraah IO on LinkedIn ]