Data Types in Python – Palin Analytics

We all know the various data types namely:

String, Numeric, DateTime, and Boolean. We shall now be discussed here about the complex Data types.

Why do we need complex data types when the earlier ones can do any task? Supposing there are 100 employees in a company.

We will have to aggrevate the data types in a balloon, this is known as complex data types. We shall be learning about 4 such data types:

  1. List
  2. Dictionary
  3. Sets
  4. Tuples

List: List has the capacity to store hundreds of values at the same time, a very important property of List is that it always stores data in the order in which it has been entered and will never change that order. You can, however, use the function by FIFO or LIFO method, FIFO stands for first in first out whereas LIFO means last in first out.

Dictionary: Dictionary also has the capacity to store unlimited values within itself, a dictionary is always a set of two items. Word and meaning. All the information is stored in pairs. A key and a value. In a dictionary, there can’t be two keys having the same value, in a dictionary the values are mutable, which means they can be used in any order.

Sets: Sets are a collection of objects having no hierarchy or relationship between them. Whatever is in the set is individual and equal objects. You can have a union of two sets which means that they can be merged together. Sets can only have nonrepeating values.

Tuples: Tuples is nonmutable and most suitable for data where you do not want any changes to take place, for example, account numbers, bank statements, medical parameters etc.

The memory of the server also plays a crucial role in data storage. Memory consumption in List is very low but also it is very slow. Dictionary consumes a lot of data but is very fast.

The syntax for list would be this

 

The above is an example of the syntax in Dictionary.

You will notice that all three key values are in pairs. What is written before the colon is the key and what is written after the colon is the value.

In this case, Siblings is the key and what is written within the inverted commas is List.

We had discussed that List supports ordering. This is done with the help of the indexing concept. The first item in any list has zero as its index number. The list will be written as: l=[‘a’,’b’,’c’,’d’,’e’]. Index will always start with a zero, which means the second item will have an index 1, the third item will have an index 2 and so on. So in case you have n items, the index no. for the last item will be n-1.

Index will always start with a zero, which means the second item will have an index 1, the third item will have an index 2 and so on. So incase you have n items, the index no. for the last item will be n-1.

In Python, there are forward indexes as well as reverse indexes. In case you are asked to print the first item from this list, it will be written like this print (l[0]). To define List we use square brackets. So if we print the above we will get the result as….a, but the last value in any list will be n-1 as that is the boundary. If you write n+1, the system will show out of bound error.

In the above screen, there are 5 items in the List as the nos.5, 6, and 7 are counted as one item, and they collectively define the third item in the List. And if you are asked to print the List of 2, the command would be : l[2] [5,6,7]

So if you are asked to print the no.6 what will be the command? No.6 is the second item in its List, therefore it carries an index which is no.1.

This will be written as l=[2] [1] this will give the result as 6.

Now suppose we want to print 5 from the screen below. What will be the command?

The command will be l2[1][2][1][0]

Reverse indexes work in the opposite direction and start with -1.

In this case, if you want to print the first value, you can take it as 0 or -5 both will give the same result.

 

As mentioned earlier all key values are in pairs. What is written before the colon is the key and what is written after the colon is the Value. Different key values are written on separate lines. Suppose we are asked to determine the age. This will be written as my-d[“Age”].

Whenever we are taking a small part from a big container, the square brackets will always be used. Whether we are taking the small part from the List container or from Dictionary, the square brackets will always be used.

I wish you like this post. If you have any questions then feel free to comment below

Related Courses:

Do check out data science courses

Please rate this

Comments (0)

Leave a comment

Quick Enquiry
close slider