List DataType in Python with TensorFlow, list datatype python, python list data type, list datatype in python

Hey fellow! Welcome to the next episode of the Python series, where we are learning the basics of Python to implement them in deep learning. In the previous lecture, our focus was on string data types. With the practical implementation of TensorFlow, many interesting points were discussed in depth. I hope you completed the home task that I assigned you during that lecture. Today, we are moving forward with the next data type, which is a sequence. You will know the different sub-groups of this data type as well in the next lecture, but today, the focus will be totally on the list because, once you understand them well, other data types of the sequence will be at your fingertips. Yet before starting, it's time to look at the content that you will learn today:

  • What is a sequence?

  • How do you classify the sequence into different types?

  • How do you understand the list?

  • What are some characteristics of this list that make it useful and different from the others?

The workings and characteristics of the list are interesting, and these will be well understood when we start our TensorFlow for different examples taken from our daily lives. We hope you have a strong grip on the integers, strings, and floats because, in our example, we will use them and will try to make changes in the list to show the difference in detail. So, without using your time, I am going to start the learning phase in Python.

Introduction to Sequence in Python

In programming languages, we use a lot of items, objects, classes, and related concepts, and when we talk about the sequence, that is a data type that provides us with the concept of a collection of things. These are useful concepts that allow us to work with them in a useful way. We define the sequence in the following way:

"The sequence in Python is a generic term that defines the ordered set of items in such a way that any of the items can be easily referred to."

Here, the word “ordered set” is important to notice. We’ll talk about it in detail in just a bit, but before that, let me tell you that the string is also considered to be the sequence because it contains the sequence of the characters. We have read a lot about the string, and therefore, we know that the ordered sequence of the characters, or alphabets, is called the string. Yet, I had a lot of data to tell you about these concepts; therefore, I have discussed them separately. There are two natures of the sequence that are listed below:

  1. Homogeneous sequence

  2. Heterogeneous sequence

The difference between these two is that a homogeneous sequence contains a group of items of the same kind.

List DataType in Python with TensorFlow, list datatype python, python list data type, list datatype in python


Homogeneous Sequence

Heterogeneous Sequence

A homogeneous sequence contains a group of items of the same kind. 

A heterogeneous sequence contains a group of items of different kinds. 

{"Apple", "Banana", "Cherry"}

[Cup, knife, banana]


It is important to know this difference because, on the basis of it, you will get the further types of the sequence. 

Types of Sequence in Python

The collection of the data can be represented in different ways, and on the basis of these types, we can divide the sequence into six major types. All of these are important to understand because we use the sequence often while programming and when dealing with complex subjects such as deep learning (in which we are interested), and the sequence plays a vital role in organizing the data in an understandable way. So, have a look at the types of sequences:

  1. String

  2. List

  3. Tuple

  4. Byte sequence

  5. Byte array

  6. Range objects

Out of them, the strings have been discussed in detail in the previous lecture. So, we are skipping that for now. You will be familiar with the remaining ones in depth, and things will become clearer to you with the help of examples. Thus, have a look at them: 

List in Sequence

In some other programming languages, such as C++ and C#, there is the concept of arrays that we study a lot. Yet, in the Python programming language, this concept is not used; instead, lists are used. The list is indicated by the square brackets just like the arrays, but it is a little bit different from them. The list is the type of sequence that contains an ordered group of different items. More information about the list can be found in the table below:

List DataType in Python with TensorFlow, list datatype python, python list data type, list datatype in python


Name

Attribute

Representation

Square brackets

Nature

Heterogenous sequence

Example 

[‘cups’, ‘eggs’, ‘yolk’, ‘tea’]


Here, it is important to notice the representation of the list with the square bracket because the bracket is the only way to differentiate it from the other types of sequence that you will learn in the coming lectures. The operation of the list will be explained in simple steps in a moment, but first, let us open the Python workspace for practical implementation. For this, you simply have to follow the steps again that we always mention:

  • Search for the Anaconda Navigator on your personal computer. 

  • Go to the environment section and look for the “Jupyter lab” or “Jupyter notebook." For this tutorial, we are using the Jupyter lab. 

  • Go to the new cell and get ready for the coding.

The list's information does not end with the table mentioned above. Certain characteristics make the lists useful, and you will learn about them in the following section.

Mutable list in Python

One of the most significant features of Python is its mutable nature. To know the meaning of the line we have just said, you must know that when we declare a list, we define the size of that particular list by mentioning the number or directly feeding the elements in it. The list's advantage is that the programmer can change the elements at any time to meet the needs of the situation. In addition to this, if you want to change the individual value of the list, you can do so easily. Not only this, but you can also change the order of the elements while working on the code. It means if you want to swipe or change the position of elements 1 and 3, you can do so easily. In short, you can say that, while working with the list, all the controls are in your hands. 

MyIntList=[1,56,8,12,56,90,3,67]

print('The homogenous integer list is : ', MyIntList)

MyFloatList=[1.2,56.2,8.2,12.2,56.2,90.2,3.2,67.2]

print('The homogenous float list is : ', MyFloatList)

List DataType in Python with TensorFlow, list datatype python, python list data type, list datatype in python

You can see that all the elements belong to the same data type, so we are calling it the homogeneous list. In the second code, we want to do something different, so have a look at the code.

list=['eggs', 2.0, 34, 'plates']

print('The elements of the list are : ', list)

print('The element at position 2 is : ', list[2])

print('The elements before the second position are : ', list[:3])

list=[ 34,2.0, 'eggs', 'plates']

print('Now the new list is: ', list)

List DataType in Python with TensorFlow, list datatype python, python list data type, list datatype in python

By looking at the code given above, we can conclude the following points:

  • To declare more than one type in a single list, the programmer simply has to use the accurate way to declare them in the list, and nothing special has to be done during the declaration of the list. 

  • To get a specific element from the list, simply the position of the element is mentioned. 

  • The position in the elements start from the 0 just like the arrays and you have to declare the element accordingly. 

  • The order of the elements can be changed easily and to do this, the programmer have to merely change the elements according to the will with the same number. Here we have used two types of lists with the same name but by changing the order of the elements and the code is working well.

  • To get the elements of the list to a certain limit, there is a need of mentioning that number of position along with a colon. Keep in mind, here we specify the position 2 and all the elements before the third position are shown to us. Same can be done to get the elements after the mentioned number and it is your homework to know how can you do so. 

Dynamic Nature of List in Python

When you compare the list to the array, you can better understand Python's dynamic nature. Let me remind you that arrays have a fixed size and cannot be changed once declared in your code. But in the case of the list, once you start working on it and need to change its size, you can increase or decrease its size by merely mentioning it in just one line. In other words, your list can grow or shrink according to your choice. To understand this you must know about a built-in function in Python.

List Length Function 

The length is a built-in function in Python that provides the length of the functions made in the code. You simply have to input the name of the function in it and it will show you the number of elements specified in that particular function. The syntax of the length function is:

len(name)

By using this function in the list mentioned above, the number of elements can be obtained with the help of this code:

list=['eggs', 2.0, 34, 'plates']

print('The length of list is : ',len(list))

list=[34,2.0, 'eggs']

print('The new length of list is : ',len(list))

The output of the code is given next:

List DataType in Python with TensorFlow, list datatype python, python list data type, list datatype in python

Heterogeneous List in Python

Keep this in mind the next time you go grocery shopping and want to buy something else; make a list for it. It will contain vegetables, meat, cloth, spices, and other things of the natural world. Because of its heterogeneous nature, the list is the same. It feels like a relief that you can make a list that contains strings, integers, and any other data types in one place. In this way, you can deal with multiple types of data at once. In arrays, this is not possible. You can also add functions to the list that contain elements of different types. You have understood it well with the previous examples in TensorFlow, but I want to show you this by mentioning a built-in function in it. 

Mylist=[23,'decoration', False ]

print(Mylist)

List DataType in Python with TensorFlow, list datatype python, python list data type, list datatype in python

Yet, there is a bit of difference between the keywords and loops; therefore, you can not mention the loops in the list; otherwise, you will face the same error as given next:

List DataType in Python with TensorFlow, list datatype python, python list data type, list datatype in python

Here, the error is demanding that you write the code in the valid syntax. It is because the compiler is not able to understand that the for loop is an element of the list, but it demands the proper syntax for the for loop and is expecting an iterative procedure. Another thing to notice here is the color of the word “False” in the list. All the keywords, while coding, are shown in green, showing that it is a built-in function or keyword and that the compiler has understood the default functionality of that particular keyword.

Hence, it was an interesting tutorial, through which we learned a lot about the list data type. We initially understood what list data types were and why we were learning them. After that, a detailed overview of the list was discussed, through which we found the characteristics of the list. It was easy to understand as we practically performed each step of the TensorFlow with the help of different examples that were related to the general examples, so we were able to understand it well. If you have a clear understanding of the list, the following lectures will be simple to grasp because they are related concepts. So, stay tuned with us to get more information about Python.